Linux-Journal

Linux-Journal 

1. 日志简介
2. 日志的优先级和分类
2.1 优先级
2.2 设施分类
3. 命令帮助
4. 日志查看示例
5. 日志大小限制
6. 手动清理日志文件

1. 日志简介

https://wiki.archlinux.org/index.php/Systemd/Journal
systemd有自己的日志记录系统; 因此,syslog不再需要运行守护程序。要阅读日志,请使用:#journalctl

在Arch Linux中,该目录/var/log/journal/是systemd包的一部分,并且日志(当Storage=设置为auto到/etc/systemd/journald.conf时)将写入/var/log/journal/。
如果删除该目录,systemd将不会自动重新创建它,而是/run/systemd/journal以非持久方式写入其日志。
然而,如果文件夹将被重新创建Storage=persistent被添加到journald.conf和systemd-journald.service被重新启动(或重新引导系统)。

Systemd Journal 的优点如下:
  • * 简单性:代码少,依赖少,抽象开销最小。
  • * 零维护:日志是除错和监控系统的核心功能,因此它自己不能再产生问题。举例说,自动管理磁盘空间,避免由于日志的不断产生而将磁盘空间耗尽。
  • * 移植性:日志 文件应该在所有类型的 Linux 系统上可用,无论它使用的何种 CPU 或者字节序。
  • * 性能:添加和浏览 日志 非常快。
  • * 最小资源占用:日志 数据文件需要较小。
  • * 统一化:各种不同的日志存储技术应该统一起来,将所有的可记录事件保存在同一个数据存储中。所以日志内容的全局上下文都会被保存并且可供日后查询。例如一条固件记录后通常会跟随一条内核记录,最终还会有一条用户态记录。重要的是当保存到硬盘上时这三者之间的关系不会丢失。Syslog 将不同的信息保存到不同的文件中,分析的时候很难确定哪些条目是相关的。
  • * 扩展性:日志的适用范围很广,从嵌入式设备到超级计算机集群都可以满足需求。
  • * 安全性:日志 文件是可以验证的,让无法检测的修改不再可能。

2. 日志的优先级和分类

系统日记按(优先级Priority level)和(设施Facility)对信息进行分类。日志分类对应于经典的Syslog协议(RFC 5424)。
注:下面表格最后一列 (wc -l) 是统计的记录数比例,总数是3个月的日志,大约100万条数据。

2.1 优先级

Value (Key)Severity Description Examples wc -l
0 (emerg)ency
紧急
System is unusable Severe Kernel BUG, systemd dumped core. 
This level should not be used by applications.
 
1 alert
警报
Should be corrected immediately Vital subsystem goes out of work. Data loss.
kernel: BUG: unable to handle kernel paging request at ffffc90403238ffc.
0.001%
2 (crit)ical
危急
Critical conditions Crashes, coredumps. Like familiar flash:
systemd-coredump[25319]: Process 25310 (plugin-containe) of user 1000 dumped core
Failure in the system primary application, like X11.
1%
3 (err)or
错误
Error conditions Not severe error reported:
kernel: usb 1-3: 3:1: cannot get freq at ep 0x84,
systemd[1]: Failed unmounting /var.,
libvirtd[1720]: internal error: Failed to initialize a valid firewall backend
1%
4 warning
警告
May indicate that an error will occur if action is not taken. A non-root file system has only 1GB free.
org.freedesktop. Notifications[1860]: (process:5999): Gtk-WARNING **: Locale not supported by C library. Using the fallback 'C' locale.
71%
5 notice
注意
Events that are unusual, but not error conditions. systemd[1]: var.mount: Directory /var to mount over is not empty, mounting anyway,
gcr-prompter[4997]: Gtk: GtkDialog mapped without a transient parent. This is discouraged
2%
6 (info)rmational
信息
Normal operational messages that require no action. lvm[585]: 7 logical volume(s) in volume group "archvg" now active 25%
7 debug
调试
Information useful to developers for debugging the application. kdeinit5[1900]: powerdevil: Scheduling inhibition from ":1.14" "firefox" with cookie 13 and reason "screen" 1%


2.2 设施分类

Facility code Keyword Description Info wc -l
0 kern Kernel messages   15%
1 user User-level messages   1%
3 daemon System daemons All daemons, including systemd and its subsystems 18%
4 auth Security/authorization messages Also watch for different facility 10 1%
10 authpriv Security/authorization messages Also watch for different facility 4 1%
9   Clock daemon systemd-timesyncd 0%

下面是不常用或已弃用的分类:
Facility code Keyword Description
2 mail Mail system
5 syslog Messages generated internally by syslogd
6 lpr Line printer subsystem
7 news Network news subsystem
8 uucp UUCP subsystem
11 ftp FTP daemon
12 - NTP subsystem
13 - Log audit
14 - Log alert
15 cron Scheduling daemon
16 - 23 local0 - 7 Local use 0 (local0) - Local use 7 (local7)

3. 命令帮助

  journalctl -h    
  Options:    
  --system Show the system journal 显示系统日志
  --user Show the user journal for the current user 显示当前用户的用户日志
-M --machine=CONTAINER Operate on local container 在本地容器上操作
-S --since=DATE Show entries not older than the specified date 显示不早于指定日期的条目
-U --until=DATE Show entries not newer than the specified date 显示不比指定日期更新的条目
-c --cursor=CURSOR Show entries starting at the specified cursor 显示从指定光标开始的条目
  --after-cursor=CURSOR Show entries after the specified cursor 在指定的光标后显示条目
  --show-cursor Print the cursor after all the entries 在所有条目之后打印光标
  --cursor-file=FILE Show entries after cursor in FILE and update FILE 在FILE中显示光标后的条目并更新FILE
-b --boot[=ID] Show current boot or the specified boot 显示当前引导或指定的引导
  --list-boots Show terse information about recorded boots 显示有关录制的靴子的简洁信息
-k --dmesg Show kernel message log from the current boot 显示当前引导的内核消息日志
-u --unit=UNIT Show logs from the specified unit 显示指定单位的日志
  --user-unit=UNIT Show logs from the specified user unit 显示指定用户单元的日志
-t --identifier=STRING Show entries with the specified syslog identifier 显示具有指定syslog标识符的条目
-p --priority=RANGE Show entries with the specified priority 显示具有指定优先级的条目
-g --grep=PATTERN Show entries with MESSAGE matching PATTERN 显示MESSAGE匹配PATTERN的条目
  --case-sensitive[=BOOL] Force case sensitive or insenstive matching 强制区分大小写或不区分匹配
-e --pager-end Immediately jump to the end in the pager 立即跳到寻呼机的末尾
-f --follow Follow the journal 关注期刊, 最新的
-n --lines[=INTEGER] Number of journal entries to show 要显示的日记帐分录数
  --no-tail Show all lines, even in follow mode 即使在跟随模式下也显示所有行
-r --reverse Show the newest entries first 首先显示最新的条目
-o --output=STRING Change journal output mode (short, short-precise, short-iso, short-iso-precise, short-full, short-monotonic, short-unix, (precise精确,monotonic单调) 更改日志输出模式: verbose, export, 
json, json-pretty, json-sse, json-seq, 
cat, with-unit)
  --output-fields=LIST Select fields to print in verbose/export/json modes 选择要以详细/导出/ json模式打印的字段
  --utc Express time in Coordinated Universal Time (UTC) 协调世界时(UTC)的快车时间
-x --catalog Add message explanations where available 添加消息说明(如果有)
  --no-full Ellipsize fields Ellipsize字段
-a --all Show all fields, including long and unprintable 显示所有字段,包括长字段和不可打印字段
-q --quiet Do not show info messages and privilege warning 不显示信息消息和权限警告
  --no-pager Do not pipe output into a pager 不要将输出传输到寻呼机
  --no-hostname Suppress output of hostname field 禁止输出主机名字段
-m --merge Show entries from all available journals 显示所有可用期刊的条目
-D --directory=PATH Show journal files from directory 显示目录中的日志文件
  --file=PATH Show journal file 显示日志文件
  --root=ROOT Operate on files below a root directory 对根目录下的文件进行操作
  --interval=TIME Time interval for changing the FSS sealing key 更改FSS密封键的时间间隔
  --verify-key=KEY Specify FSS verification key 指定FSS验证密钥
  --force Override of the FSS key pair with --setup-keys 使用--setup-keys覆盖FSS密钥对
       
  Commands:    
-h --help Show this help text 显示此帮助文本
  --version Show package version 显示包版本
-N --fields List all field names currently used 列出当前使用的所有字段名称
-F --field=FIELD List all values that a specified field takes 列出指定字段所需的所有值
  --disk-usage Show total disk usage of all journal files 显示所有日志文件的总磁盘使用情况
  --vacuum-size=BYTES Reduce disk usage below specified size 将磁盘使用量降低到指定大小以下
  --vacuum-files=INT Leave only the specified number of journal files 只保留指定数量的日志文件
  --vacuum-time=TIME Remove journal files older than specified time 删除早于指定时间的日志文件
  --verify Verify journal file consistency 验证日志文件一致性
  --sync Synchronize unwritten journal messages to disk 将未写入的日志消息同步到磁盘
  --flush Flush all journal data from /run into /var 将/ run中的所有日志数据刷新到/var
  --rotate Request immediate rotation of the journal files 请求立即轮换日志文件
  --header Show journal header information 显示日记标题信息
  --list-catalog Show all message IDs in the catalog 显示目录中的所有消息ID
  --dump-catalog Show entries in the message catalog 在消息目录中显示条目
  --update-catalog Update the message catalog database 更新消息目录数据库
  --setup-keys Generate a new FSS key pair 生成新的FSS密钥对

4. 日志查看示例

Show all messages from this boot: -b 启动信息
$ sudo journalctl -b //启动信息23565
$ sudo journalctl --list-boots //引导列表
$ sudo journalctl -b -0 //
$ sudo journalctl -b -1 //前一次启动信息... 通过查询引导列表可看到最多能查看前几次启动信息

Show all messages from date (and optional time): -S 显示不早于指定日期的条目
$ sudo journalctl --since="2019-06-13 16:42:34"

Show all messages since 20 minutes ago: 最近20分钟
$ sudo journalctl --since "20 min ago"
$ sudo journalctl -S "20 min ago"

Follow new messages:
$ sudo journalctl -f

Show all messages by a specific executable:特定可执行文件
$ sudo journalctl /usr/lib/systemd/systemd

Show all messages by a specific process:特定进程
$ sudo journalctl _PID=1

Show all messages by a specific unit: -u 特定单元
$ sudo journalctl -u man-db.service

Show kernel ring buffer:-k 显示当前引导的内核消息日志(--dmesg)
$ sudo journalctl -k

Show only error, critical and alert priority messages:-p 显示具有指定优先级的条目(0-7)
$ sudo journalctl -p err..alert
$ sudo journalctl -p 3..1 //3-1
$ sudo journalctl -p 3 //3-0
$ sudo journalctl -p 3 -r //3-0; 加-r选项,首先显示最新的条目

Show auth.log equivalent by filtering on syslog facility:
$ sudo journalctl SYSLOG_FACILITY=10
0 kern 内核;1 user 用户;3 daemon 守护进程;
4 auth 授权;10 authpriv 授权;

$ sudo journalctl SYSLOG_FACILITY=0 -r
$ sudo journalctl -k -r

$ sudo journalctl SYSLOG_FACILITY=4 |wc -l
14516
$ sudo journalctl SYSLOG_FACILITY=10 |wc -l
9049

If the journal directory (by default located under /var/log/journal) contains a large amount of log data then journalctl can take several minutes to filter output. It can be sped up significantly by using --file option to force journalctl to look only into most recent journal:
$ sudo journalctl --file /var/log/journal/*/system.journal -f

5. 日志大小限制

默认为基础文件系统的10%,但上限为4GB。
例如本机/var/log/journal/位于30Gb分区上,日志最多需要3Gb。超过40Gb的分区,日志文件需要最大值都为4Gb。

可以通过取消注释和更改以下内容来控制持久日志的最大大小:
/etc/systemd/journald.conf
SystemMaxUse=50M

也可以使用drop-in snippets配置覆盖机制,而不是编辑全局配置文件。在这种情况下,将覆盖置于[Journal]标题下:
/etc/systemd/journald.conf.d/00-journal-size.conf
[Journal]
SystemMaxUse=50M

修改后重新启动日志系统 systemd-journald.service

6. 手动清理日志文件

删除已归档的日志文件,直到它们使用的磁盘空间低于100M:
$ sudo journalctl --vacuum-size=100M

使所有日记文件不包含超过2周的数据。
$ sudo journalctl --vacuum-time=2weeks
原文地址:https://www.cnblogs.com/sztom/p/11046121.html