【问题标题】:Where does linux store my syslog?linux 在哪里存储我的系统日志?
【发布时间】:2012-06-14 07:38:46
【问题描述】:

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

tail -n 100 /var/log/messages

但是文件消息不存在,既不测试也不存在。您可以在下面找到我的代码。也许我做错了什么,文件没有存储在那里,或者我需要在 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;
}

【问题讨论】:

  • 这取决于您使用的系统记录器。它应该在/etc/ 的某个位置有一个配置文件,您必须为您的标识符(在您的情况下为"test")和设施进行更改。

标签: c linux logging


【解决方案1】:

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

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

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

【讨论】:

  • 如果我的 /etc/syslog.conf 只包含 cmets 怎么办?是否有日志存放的默认位置?
  • 链接已损坏...
【解决方案2】:

默认日志位置(rhel)是

一般信息:

/var/log/messages

身份验证消息:

/var/log/secure

邮件事件:

/var/log/maillog

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

例子:

$ 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

【讨论】:

    【解决方案3】:

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

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

    如果您运行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
    

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

    这是 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]
    

    要阅读上面的运行

    $man man 
    

    因此,如果您运行 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);
    

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

    【讨论】:

      【解决方案4】:

      在我的 Ubuntu 机器上,我可以在 /var/log/syslog 看到输出。

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

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

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

      更多详情:这里是interesting link

      【讨论】:

      【解决方案5】:

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

      还要查看/var/log/ 下的文件(也许运行dmesg 以获得内核日志)。

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

          所以我做的是这样的:

          $ service rsyslog start
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-17
            • 2011-06-04
            • 1970-01-01
            • 1970-01-01
            • 2017-07-18
            • 2019-07-27
            • 1970-01-01
            相关资源
            最近更新 更多