linux 在哪里存储我的系统日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10979435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:46:59  来源:igfitidea点击:

Where does linux store my syslog?

clinuxlogging

提问by opc0de

I wrote a simple test application to log something in a log file. I am using linux mintand after the application executes I try to view the log using this command:

我写了一个简单的测试应用程序来在日志文件中记录一些东西。我正在使用linux mint,在应用程序执行后,我尝试使用以下命令查看日志:

tail -n 100 /var/log/messages

but the file messages does not exist neither tested or something. Below you can find my code. Maybe I am doing something wrong, the file isn't stored there or I need to enable logging in linux mint.

但文件消息既不存在,也不存在测试或其他东西。您可以在下面找到我的代码。也许我做错了什么,文件没有存储在那里,或者我需要启用登录 linux mint。

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

void init_log()
{
    setlogmask(LOG_UPTO(LOG_NOTICE));
    openlog("testd",LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
}

int main(void) {

    init_log();
    printf("Session started!");
    syslog(LOG_NOTICE, "Session started!!");
    closelog();

    return EXIT_SUCCESS;
}

采纳答案by TheCottonSilk

On my Ubuntu machine, I can see the output at /var/log/syslog.

在我的 Ubuntu 机器上,我可以在/var/log/syslog.

On a RHEL/CentOS machine, the output is found in /var/log/messages.

在 RHEL/CentOS 机器上,输出位于/var/log/messages.

This is controlled by the rsyslogservice, so if this is disabled for some reason you may need to start it with systemctl start rsyslog.

这是由rsyslog服务控制的,因此如果由于某种原因禁用了它,您可能需要使用systemctl start rsyslog.

As noted by others, your syslog()output would be logged by the /var/log/syslogfile.
You can see system, user, and other logs at /var/log.

正如其他人所指出的,您的syslog()输出将由/var/log/syslog文件记录。
您可以在 中查看系统、用户和其他日志/var/log

For more details: here's an interesting link.

有关更多详细信息:这是一个有趣的链接

回答by Clément

syslog() generates a log message, which will be distributed by syslogd.

syslog() 生成一条日志消息,该消息将由 syslogd 分发。

The file to configure syslogd is /etc/syslog.conf. This file will tell your where the messages are logged.

配置 syslogd 的文件是 /etc/syslog.conf。此文件将告诉您消息记录的位置。

How to change options in this file ? Here you go http://www.bo.infn.it/alice/alice-doc/mll-doc/duix/admgde/node74.html

如何更改此文件中的选项?给你 http://www.bo.infn.it/alice/alice-doc/mll-doc/duix/admgde/node74.html

回答by rkosegi

Default log location (rhel) are

默认日志位置 (rhel) 是

General messages:

一般讯息:

/var/log/messages

Authentication messages:

身份验证消息:

/var/log/secure

Mail events:

邮件事件:

/var/log/maillog

Check your /etc/syslog.confor /etc/syslog-ng.conf(it depends on which of syslog facility you have installed)

检查您的/etc/syslog.conf/etc/syslog-ng.conf(这取决于您安装了哪个系统日志工具)

Example:

例子:

$ cat /etc/syslog.conf
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none         /var/log/messages

# The authpriv file has restricted access.
authpriv.*                             /var/log/secure

# Log all the mail messages in one place.
mail.*                                 /var/log/maillog

#For a start, use this simplified approach.
*.*                                     /var/log/messages

回答by Rob Kielty

In addition to the accepted answer, it is useful to know the following ...

除了公认的答案外,了解以下内容也很有用......

Each of those functions should have manual pagesassociated with them.

这些功能中的每一个都应该有与之相关的手册页

If you run man -k syslog(a keyword search of man pages) you will get a list of man pages that refer to, or are about syslog

如果您运行man -k syslog(对手册页进行关键字搜索),您将获得引用或关于syslog的手册页列表

$ man -k syslog
logger (1)           - a shell command interface to the syslog(3) system l...
rsyslog.conf (5)     - rsyslogd(8) configuration file
rsyslogd (8)         - reliable and extended syslogd
syslog (2)           - read and/or clear kernel message ring buffer; set c...
syslog (3)           - send messages to the system logger
vsyslog (3)          - send messages to the system logger

You need to understand the manual sections in order to delve further.

您需要了解手册部分才能进一步深入研究。

Here's an excerpt from the man page for man, that explains man page sections :

这是 man 手册页的摘录,它解释了手册页部分:

The table below shows the section numbers of the manual followed  by
the types of pages they contain.

   1   Executable programs or shell commands
   2   System calls (functions provided by the kernel)
   3   Library calls (functions within program libraries)
   4   Special files (usually found in /dev)
   5   File formats and conventions eg /etc/passwd
   6   Games
   7   Miscellaneous  (including  macro  packages and conven‐
       tions), e.g. man(7), groff(7)
   8   System administration commands (usually only for root)
   9   Kernel routines [Non standard]

To read the above run

阅读上面的运行

$man man 

So, if you run man 3 syslogyou get a full manual page for the syslogfunction that you called in your code.

因此,如果您运行,man 3 syslog您将获得syslog您在代码中调用的函数的完整手册页。

SYSLOG(3)                Linux Programmer's Manual                SYSLOG(3)

NAME
   closelog,  openlog,  syslog,  vsyslog  - send messages to the system
   logger

SYNOPSIS
   #include <syslog.h>

   void openlog(const char *ident, int option, int facility);
   void syslog(int priority, const char *format, ...);
   void closelog(void);

   #include <stdarg.h>

   void vsyslog(int priority, const char *format, va_list ap);

Not a direct answer but hopefully you will find this useful.

不是直接的答案,但希望您会发现这很有用。

回答by Basile Starynkevitch

Logging is very configurable in Linux, and you might want to look into your /etc/syslog.conf(or perhaps under /etc/rsyslog.d/). Details depend upon the logging subsystem, and the distribution.

日志记录在 Linux 中是非常可配置的,您可能想要查看您的/etc/syslog.conf(或可能在 下/etc/rsyslog.d/)。细节取决于日志子系统和分布。

Look also into files under /var/log/(and perhaps run dmesgfor kernel logs).

还要查看下的文件/var/log/(并且可能运行dmesg内核日志)。

回答by david tooke

You have to tell the system what information to log and where to put the info. Logging is configured in the /etc/rsyslog.conffile, then restart rsyslog to load the new config. The default logging rules are usually in a /etc/rsyslog.d/50-default.conffile.

您必须告诉系统要记录哪些信息以及将信息放在哪里。在/etc/rsyslog.conf文件中配置日志记录,然后重新启动 rsyslog 以加载新配置。默认的日志记录规则通常在一个/etc/rsyslog.d/50-default.conf文件中。

回答by Rain

I'm running Ubuntu under WSL(Windows Subsystem for Linux) and systemctl start rsyslogdidn't work for me.

我在 WSL(Linux 的 Windows 子系统)下运行 Ubuntu,systemctl start rsyslog但对我不起作用。

So what I did is this:

所以我所做的是这样的:

$ service rsyslog start

Now syslogfile will appear at /var/log/

现在syslog文件将出现在/var/log/