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)SeverityDescriptionExampleswc -l
0(emerg)ency
紧急
System is unusableSevere Kernel BUG, systemd dumped core. 
This level should not be used by applications.
 
1alert
警报
Should be corrected immediatelyVital subsystem goes out of work. Data loss.
kernel: BUG: unable to handle kernel paging request at ffffc90403238ffc.
0.001%
2(crit)ical
危急
Critical conditionsCrashes, 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 conditionsNot 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%
4warning
警告
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%
5notice
注意
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 active25%
7debug
调试
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 codeKeywordDescriptionInfowc -l
0kernKernel messages 15%
1userUser-level messages 1%
3daemonSystem daemonsAll daemons, including systemd and its subsystems18%
4authSecurity/authorization messagesAlso watch for different facility 101%
10authprivSecurity/authorization messagesAlso watch for different facility 41%
9 Clock daemonsystemd-timesyncd0%

下面是不常用或已弃用的分类:
Facility codeKeywordDescription
2mailMail system
5syslogMessages generated internally by syslogd
6lprLine printer subsystem
7newsNetwork news subsystem
8uucpUUCP subsystem
11ftpFTP daemon
12-NTP subsystem
13-Log audit
14-Log alert
15cronScheduling daemon
16 - 23local0 - 7Local use 0 (local0) - Local use 7 (local7)

3. 命令帮助

 journalctl -h  
 Options:  
 --systemShow the system journal显示系统日志
 --userShow the user journal for the current user显示当前用户的用户日志
-M--machine=CONTAINEROperate on local container在本地容器上操作
-S--since=DATEShow entries not older than the specified date显示不早于指定日期的条目
-U--until=DATEShow entries not newer than the specified date显示不比指定日期更新的条目
-c--cursor=CURSORShow entries starting at the specified cursor显示从指定光标开始的条目
 --after-cursor=CURSORShow entries after the specified cursor在指定的光标后显示条目
 --show-cursorPrint the cursor after all the entries在所有条目之后打印光标
 --cursor-file=FILEShow entries after cursor in FILE and update FILE在FILE中显示光标后的条目并更新FILE
-b--boot[=ID]Show current boot or the specified boot显示当前引导或指定的引导
 --list-bootsShow terse information about recorded boots显示有关录制的靴子的简洁信息
-k--dmesgShow kernel message log from the current boot显示当前引导的内核消息日志
-u--unit=UNITShow logs from the specified unit显示指定单位的日志
 --user-unit=UNITShow logs from the specified user unit显示指定用户单元的日志
-t--identifier=STRINGShow entries with the specified syslog identifier显示具有指定syslog标识符的条目
-p--priority=RANGEShow entries with the specified priority显示具有指定优先级的条目
-g--grep=PATTERNShow entries with MESSAGE matching PATTERN显示MESSAGE匹配PATTERN的条目
 --case-sensitive[=BOOL]Force case sensitive or insenstive matching强制区分大小写或不区分匹配
-e--pager-endImmediately jump to the end in the pager立即跳到寻呼机的末尾
-f--followFollow the journal关注期刊, 最新的
-n--lines[=INTEGER]Number of journal entries to show要显示的日记帐分录数
 --no-tailShow all lines, even in follow mode即使在跟随模式下也显示所有行
-r--reverseShow the newest entries first首先显示最新的条目
-o--output=STRINGChange 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=LISTSelect fields to print in verbose/export/json modes选择要以详细/导出/ json模式打印的字段
 --utcExpress time in Coordinated Universal Time (UTC)协调世界时(UTC)的快车时间
-x--catalogAdd message explanations where available添加消息说明(如果有)
 --no-fullEllipsize fieldsEllipsize字段
-a--allShow all fields, including long and unprintable显示所有字段,包括长字段和不可打印字段
-q--quietDo not show info messages and privilege warning不显示信息消息和权限警告
 --no-pagerDo not pipe output into a pager不要将输出传输到寻呼机
 --no-hostnameSuppress output of hostname field禁止输出主机名字段
-m--mergeShow entries from all available journals显示所有可用期刊的条目
-D--directory=PATHShow journal files from directory显示目录中的日志文件
 --file=PATHShow journal file显示日志文件
 --root=ROOTOperate on files below a root directory对根目录下的文件进行操作
 --interval=TIMETime interval for changing the FSS sealing key更改FSS密封键的时间间隔
 --verify-key=KEYSpecify FSS verification key指定FSS验证密钥
 --forceOverride of the FSS key pair with --setup-keys使用--setup-keys覆盖FSS密钥对
    
 Commands:  
-h--helpShow this help text显示此帮助文本
 --versionShow package version显示包版本
-N--fieldsList all field names currently used列出当前使用的所有字段名称
-F--field=FIELDList all values that a specified field takes列出指定字段所需的所有值
 --disk-usageShow total disk usage of all journal files显示所有日志文件的总磁盘使用情况
 --vacuum-size=BYTESReduce disk usage below specified size将磁盘使用量降低到指定大小以下
 --vacuum-files=INTLeave only the specified number of journal files只保留指定数量的日志文件
 --vacuum-time=TIMERemove journal files older than specified time删除早于指定时间的日志文件
 --verifyVerify journal file consistency验证日志文件一致性
 --syncSynchronize unwritten journal messages to disk将未写入的日志消息同步到磁盘
 --flushFlush all journal data from /run into /var将/ run中的所有日志数据刷新到/var
 --rotateRequest immediate rotation of the journal files请求立即轮换日志文件
 --headerShow journal header information显示日记标题信息
 --list-catalogShow all message IDs in the catalog显示目录中的所有消息ID
 --dump-catalogShow entries in the message catalog在消息目录中显示条目
 --update-catalogUpdate the message catalog database更新消息目录数据库
 --setup-keysGenerate 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
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值