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