1、日志分类
在Linux下日志可以分为以下三类:
- 内核及系统日志:内核和系统运行中产生的日志,由syslog/rsyslog统一管理(从CentOS 6开始使用rsyslog,配置文件为/etc/syslog.conf)
- 用户日志:记录系统用户登录退出等相关信息,包括用户名、登录的终端、登录时间、来源主机、正在进行的操作等
- 应用程序日志:应用程序运行中产生的日志,一些应用程序会自己管理日志而不是使用syslog/rsyslog
2、rsyslog
rsyslog是syslog的一个多线程增强版日志管理系统,大部分的Linux发行版都已经改用rsyslog进行日志管理。
rsyslog配置文件:
/etc/rsyslog.conf
配置文件内容及详解:
# 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. $ModLoad imuxsock # provides support for local system logging (e.g. via logger command) $ModLoad imjournal # provides access to the systemd journal #$ModLoad imklog # reads kernel messages (the same are read from journald) #$ModLoad immark # provides --MARK-- message capability # 允许本机514端口接收其他主机通过UDP协议发送的日志 # Provides UDP syslog reception #$ModLoad imudp #$UDPServerRun 514 # 允许本机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 local log socket; # local messages are retrieved through imjournal now. $OmitLocalLogging on # File to store the position in the journal $IMJournalStateFile imjournal.state #### RULES #### #### 记录日志的规则 #### # 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;authpriv.none;cron.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 # 路径前的-表示使用异步传输模式,隔一段时间向硬盘写入数据,以防止大量的日志造成硬盘I/O负担 # Log cron stuff cron.* /var/log/cron # Everybody gets emergency messages *.emerg :omusrmsg:* # 调用omusrmsg模块 # Save news errors of level crit and higher in a special file. 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! # Remote Logging (we use TCP for reliable delivery) # # 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. #$ActionQueueFileName fwdRule1 # unique name prefix for spool files #$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。@@表示使用TCP协议,@表示使用UDP协议 # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional #*.* @@remote-host:514 # ### end of the forwarding rule ###
2.1、日志记录规则详解
记录规则遵循如下格式:
facility.level action # facility.level字段也被称为seletor(选择条件)
facility:设备(也称日志类型)
- auth:由PAM产生的用户认证日志
- authpriv:包括特权登录在内的用户认证,如ssh登录
- cron:任务计划产生的日志
- daemon:某些守护进程产生的日志
- kern:系统内核产生的日志
- lpr:与打印机活动有关的日志
- mail:邮件日志
- mark:rsyslog生成时间戳,格式类似 May 26 11:17:09 rs2 -- MARK --,用于推断事件产生的大概时间
- news:网络新闻传输协议nntp产生的日志
- ntp:网络时间协议ntp产生的日志
- user:用户进程产生的日志
- uucp:UUCP子系统产生的日志
- local0-local7:用户自定义的日志类型,可以在应用程序里使用
注意:facility中可以使用通配符*,代表除mark外的所有类型
level:日志级别
- emerg:紧急情况,系统不可用,一般会通知所有用户
- alert:需要立即修复
- crit:危险情况,可能会阻碍程序的部分功能
- err:一般错误消息
- warning:警告
- notice:不是错误,但可能需要处理
- info:通用性消息,一般用来提供有用信息
- debug:调试程序产生的消息
- none:没有优先级,不记录任何日志消息
seletor:选择条件
- ;:同时定义多个选择条件
- .:表示大于等于某个级别的信息
- *:所有日志信息
- =:仅包含本优先级的日志信息
- !:不等于,本优先级日志信息除外
action:动作
- file:指定日志文件的绝对路径
- terminal 或print:发送到串行或并行设备标识符,例如/dev/ttyS2
- @host:远程主机,@后可以是IP也可以是域名
- username:指定用户,可以使用,指定多个用户,这些用户必须已登录到主机
- *:所有用户,所有已登录的用户都将收到信息
- ^:^后跟可执行脚本或程序的绝对路径,可以将日志内容作为脚本的第一个参数
3、常用日志文件
系统自身的日志及大部分应用程序的日志文件默认都保存在/var/log目录下。在Linux下有以下一些常用的日志文件(均保存在/var/log目录):
- messages:公共日志文件,记录Linux内核消息及各种应用程序的公共日志消息,包括启动、IO错误、网络错误、程序故障等。对于未使用独立日志文件的应用程序或服务,一般都可以从该文件获得相关的事件记录信息
- cron:记录crond计划任务产生的事件消息
- dmesg:包含内核缓冲信息(kernel ring buffer)。在系统启动时,屏幕上显示许多与硬件有关的信息,此日志文件保存系统上次启动时产生的这些信息。而dmesg命令可以查看本次系统启动时的信息。
- maillog:记录进入或发出系统的电子邮件活动
- boot.log:记录系统启动时的软件日志信息,及系统启动时屏幕上滚动的信息
- secure:记录用户远程登录,认证过程中的事件信息
- wtmp:记录系统所有登录和退出信息。该文件为二进制文件,通过last命令查看
- btmp:记录错误登录系统的信息。该文件为二进制文件,通过lastb命令查看
- lastlog:记录每一个用户最后一次成功和不成功的登录事件。该文件为二进制文件,通过lastlog命令查看