首页 > 其他分享 >/var/log/message 归档探究

/var/log/message 归档探究

时间:2023-01-28 18:23:22浏览次数:57  
标签:systemd log journald rsyslog var message 日志

转自:https://blog.csdn.net/u012986012/article/details/105453545

背景

由于项目要收集/var/log/messages的日志到es中,发现messages日志按天切割,但归档的时间却不一致,于是查了点资料探究下。

介绍

/var/log/messages是由journald生成的,流程如下
systemd --> systemd-journald --> ram DB --> rsyslog -> /var/log
当systemd启动后,systemd-journald也会立即启动。将日志存入RAM中,当rsyslog启动后会读取该RAM并完成筛选分类写入目录/var/log。

相关服务

涉及的相关服务有:

  • systemd-journald.service:最主要的信息收受者,由systemd提供;
  • logrotate:主要进行日志的轮替功能
  • Centos7使用systemd提供的journalctl管理日志,那所有经由systemd启动服务的日志,会将该日志信息由systemd-journald.service以二进制的方式记录下来,之后再将信息发送到rsyslog.service作进一步的处理。
  • systemd-journald.service的记录主要都放置与内存中,因此性能较好,可以通过journalctl及systemctl status unit.service来查看各个服务的日志。

配置文件

  • 目前,centos log由rsyslog管理,设置文件/var/lib/rsyslog并兼容syslog的配置文件,其中messages文件记录系统日志,包括mail、定时任务、系统异常等(*.info;mail.none;authpriv.none;cron.none /var/log/messages)
  • Logrotate实现日志切割,具体由CRON实现

journald配置文件


cat /etc/systemd/journald.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See journald.conf(5) for details.

[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitInterval=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes #默认转向syslog
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg

rsyslog配置

配置文件/etc/rsyslog.conf主要有3个部分

MODULES :模块
GLOBAL DRICTIVES :全局设置
RULES:规则

  #rsyslog v3 config file   [兼容版本号]

  #### MODULES ####    加载模块
  $ModLoad imuxsock             # 本地系统日志 (e.g. via logger command) 
  $ModLoad imklog               # provides kernel logging support (previously done by rklogd)

  # 允许514端口接收使用UDP协议转发过来的日志
  #$ModLoad imudp.so
  #$UDPServerRun 514
  # 允许514端口接收使用TCP协议转发过来的日志
  #$ModLoad imtcp.so
  #$InputTCPServerRun 514

  #### GLOBAL DIRECTIVES ####
  # 定义日志格式默认模板  
  $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

  #### RULES ####
  # 关于内核的所有日志都放到/dev/console(控制台)
  #kern.*                                                 /dev/console
  # 记录所有日志类型的info级别以及大于info级别的信息到 /var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
  *.info;mail.none;authpriv.none;cron.none                /var/log/messages  
  # authpriv验证相关的所有信息存放在/var/log/secure
  authpriv.*                                              /var/log/secure  
  # 邮件的所有信息存放在/var/log/maillog; 这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
  mail.*                                                  -/var/log/maillog  
  # 计划任务有关的信息存放在/var/log/cron
  cron.*                                                  /var/log/cron  
  # 记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人(* 代表所有在线用户 )
  *.emerg                                                 * 
  # 记录uucp,news.crit等存放在/var/log/spooler
  uucp,news.crit                                          /var/log/spooler  
  # Save boot messages also to boot.log     启动的相关信息
  local7.*                                                /var/log/boot.log

  #  转发规则
  # remote host is: name/ip:port,  port optional (e.g. 192.168.0.1:514)
  *.*                                                     @@remote-host:514                    
  # @@表示通过tcp协议发送    @表示通过udp进行转发

logrogate 轮转

https://blog.csdn.net/qq_34246164/article/details/89065310

标签:systemd,log,journald,rsyslog,var,message,日志
From: https://www.cnblogs.com/AllenWongFly/p/17070923.html

相关文章