87

我编写了一个简单的测试应用程序来在日志文件中记录一些内容。我正在使用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;
}
4

7 回答 7

86

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

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

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

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

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

于 2012-06-11T12:06:01.470 回答
24

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

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

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

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

于 2012-06-11T12:01:14.560 回答
24

默认日志位置 (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
于 2012-06-11T12:00:51.443 回答
9

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

于 2014-07-09T14:13:09.963 回答
4

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

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

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

于 2012-06-11T11:57:08.273 回答
4

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

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

于 2012-06-11T13:04:13.193 回答
1

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

所以我做的是这样的:

$ service rsyslog start

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

于 2019-12-03T23:36:10.217 回答