Linux-Journal
1. 日志簡介
2. 日志的優先級和分類
2.1 優先級
2.2 設施分類
3. 命令幫助
4. 日志查看示例
5. 日志大小限制
6. 手動清理日志文件
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 的優點如下:
注:下面表格最后一列 (wc -l) 是統計的記錄數比例,總數是3個月的日志,大約100萬條數據。
下面是不常用或已棄用的分類:
$ 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
例如本機/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
$ sudo journalctl --vacuum-size=100M
使所有日記文件不包含超過2周的數據。
$ sudo journalctl --vacuum-time=2weeks
2. 日志的優先級和分類
2.1 優先級
2.2 設施分類
3. 命令幫助
4. 日志查看示例
5. 日志大小限制
6. 手動清理日志文件
1. 日志簡介
https://wiki.archlinux.org/index.php/Systemd/Journalsystemd有自己的日志記錄系統; 因此,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 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