# rsyslog configuration file # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html # If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html #### MODULES #### # 加载模块 # The imjournal module bellow is now used as a message source instead of imuxsock. # 加载 imuxsock 模块,为本地系统登陆提供支持(例如通过logger命令) $ModLoad imuxsock # provides support for local system logging (e.g. via logger command) # 提供对systemd日志的访问 $ModLoad imjournal # provides access to the systemd journal # 加载 imklog,为内核登陆提供支持 #$ModLoad imklog # reads kernel messages (the same are read from journald) # 提供标记信息功能 #$ModLoad immark # provides --MARK-- message capability # 加载 UDP 模块,允许使用UDP的514端口接收采用UDP协议转发的日志 # Provides UDP syslog reception #$ModLoad imudp #$UDPServerRun 514 # 加载 TCP 模块,允许使用TCP的514端口接收采用TCP协议转发的日志 # Provides TCP syslog reception #$ModLoad imtcp #$InputTCPServerRun 514 #### GLOBAL DIRECTIVES #### # 全局指令的配置 # Where to place auxiliary files # 辅助文件目录 $WorkDirectory /var/lib/rsyslog # Use default timestamp format # 使用默认的时间戳格式 $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat # File syncing capability is disabled by default. This feature is usually not required, # not useful and an extreme performance hit # 文件同步功能,通常是被禁用的,这个功能通常不是必要的,会大幅影响性能 #$ActionFileEnableSync on # Include all config files in /etc/rsyslog.d/ # 包含配置文件的目录,这个目录下的配置文件也是生效的 $IncludeConfig /etc/rsyslog.d/*.conf # Turn off message reception via locallog socket; # local messages are retrieved through imjournal now. # 通过本地消息日志套接字关闭消息接收,本地消息可以用imjournal检索 $OmitLocalLogging on # File to store the position in the journal # 在日志中文件用于存储的位置 $IMJournalStateFile imjournal.state #### RULES #### #RULES部分主要是对各种facility和level进行设置 #设置你想要接收的facility的何种level的日志消息 # Log all kernel messages to the console. # Logging much else clutters up the screen. # 将将所有的内核日志记录在界面,会造成屏幕混乱 #kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! # 将 info或者更高级别的消息送到这个文件,除了mail以外 # 其中 * 是通配符,代表任何设备,none表示不对任何级别的信息进行记录 # 同一行配置中,允许出现多个选择条件,多个选择条件使用 ; 隔开 *.info;mail.none;authpriv.none;cron.none /var/log/messages # The authpriv file has restricted access. # 将 authpriv 设备的任何级别信息记录到这个文件中,这主要是一些权限相关的信息 authpriv.* /var/log/secure # Log all the mail messages in one place. # 将 mail 设备中的任何级别的信息记录到这个文件,这主要是和电子邮件相关的信息 # 文件前的前缀表示可以避免日志的同步写入,某些时候对提升系统的性能有很大的帮助 mail.* -/var/log/maillog # Log cron stuff # 将 cron 设备中的任何级别的信息记录到这个文件,这主要是系统中定期执行的任务相关信息 cron.* /var/log/cron # Everybody gets emergency messages # 将任何级别的 emerg 级别的信息发送给所有正在系统上的用户 *.emerg :omusrmsg:* # Save news errors of level crit and higher in a special file. # uucp 和 news 设备的 crit 级别的信息记录到这个文件 uucp,news.crit /var/log/spooler # Save boot messages also to boot.log # 将系统启动相关的信息记录在这个文件 local7.* /var/log/boot.log # ### begin forwarding rule ### # 转发规则 # The statement between the begin ... end define a SINGLE forwarding # rule. They belong together, do NOT split them. If you create multiple # forwarding rules, duplicate the whole block! # 远程日志可以使用 TCP 保持可靠 # Remote Logging (we use TCP for reliable delivery) # # 创建一个磁盘上的队列,如果远程的主机是关闭的状态,消息会先存储在磁盘中并且在远程主机启动时发送过去(TCP) # An on-disk queue is created for this action. If the remote host is # down, messages are spooled to disk and sent when it is up again. # spool 文件 唯一的前缀 #$ActionQueueFileName fwdRule1 # unique name prefix for spool files # 1gb的空间限制(尽量使用) #$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible) # 在关机的时候把信息存储在磁盘中 #$ActionQueueSaveOnShutdown on # save messages to disk on shutdown # 异步运行 #$ActionQueueType LinkedList # run asynchronously # 如果远程主机宕机,将无限次重试 #$ActionResumeRetryCount -1 # infinite retries if host is down # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional # 远程的主机和端口,注意:若用UDP传输,则将@@改为@即可。 #*.* @@remote-host:514 # ### end of the forwarding rule ###
LOG_AUTHPRIV security/authorization messages (private) LOG_CRON clock daemon (cron and at) LOG_DAEMON system daemons without separate facility value LOG_FTP ftp daemon LOG_KERN kernel messages (these can’t be generated from user processes) LOG_LOCAL0 through LOG_LOCAL7 reserved for local use LOG_LPR line printer subsystem LOG_MAIL mail subsystem LOG_NEWS USENET news subsystem LOG_SYSLOG messages generated internally by syslogd(8) LOG_USER (default) generic user-level messages LOG_UUCP UUCP subsystem
LOG_EMERG system is unusable LOG_ALERT action must be taken immediately LOG_CRIT critical conditions LOG_ERR error conditions LOG_WARNING warning conditions LOG_NOTICE normal, but significant, condition LOG_INFO informational message LOG_DEBUG debug-level message
linux C 中提供了一套系统日志写入的接口,主要涉及三个函数openlog,syslog,closelog。
打开一个syslog连接
1 2
#include<syslog.h> voidopenlog(constchar *ident, int option, int facility);
参数:
ident 指定的字符串会放入到相应的消息日志中
option有如下选项
1 2 3 4 5 6
LOG_CONS Write directly to system console if there is an error while sending to system logger. LOG_NDELAY Open the connection immediately LOG_NOWAIT Don’t wait for child processes that may have been created while logging the message. LOG_ODELAY The converse of LOG_NDELAY; opening of the connection is delayed until syslog() is called. LOG_PERROR (Not in POSIX.1-2001.) Print to stderr as well. LOG_PID Include PID with each message.打印的每一条日志信息包含当前进程的PID