journalctl とは
jounarld.service が記録してくれているログをみるためのコマンド。
linux でアプリケーションを動かす場合は、systemd に登録し、journalctl でログを見るというのがベストプラクティスになっている。
新しいもの順に表示するようにする
journalctl -r
最新のログをフォローする場合は、
journalctl -f
特定の時間から調べる
障害が起きた特定の時間からのログを調べたいということがある。
journalctl --since="2020-10-13 22:15:00"
# もちろん、until もある
journalctl --until="2020-10-14 22:15:00"
障害が起きた間のみのログに絞りたい場合は
journalctl --since="2020-10-13 22:15:00" --until="2020-10-14 22:15:00"
何時間前などで絞りたい場合は、date コマンドと組み合わせると便利
journalctl --since="$(date -d '12 hour ago' '+%Y-%m-%d %H:%M:%S')" --until="$(date -d '11 hour ago' '+%Y-%m-%d %H:%M:%S')"
複数の unit のログを見る
問題となっていそうな複数のコンポーネントにしぼってみる
journalctl -u fluentd -u block-storage
カーネルのログを見るときは -k をつける。OS が突然死した、CPU の異常などが起きた場合はよく使う
journalctl -k
問題となっていそうなキーワードから絞る
journalctl --grep "kube"
systemd が grep をサポートしてコンパイルされていないと下記のエラーが出る
Compiled without pattern matching support
#890265 - systemd: journalctl compiled without pattern matching support - Debian Bug report logs
ファイルがローテートされた古いログを確認する
データサイズが大きくなると、ローテートされてしまうが保存されている fileから読める
journalctl --file=/var/log/journal/hogehoge.log
ディレクトリ内の journal ログをまとめて見ることもできる
journalctl -D /var/log/journal/b1eae0134
表示されるログが wrap されるようにする
そのまま journalctl だと、一行が長くて画面に収まらないことがある。
systemd - journalctl: how to prevent text from truncating in terminal - Unix & Linux Stack Exchange
journalctl -xn | less
# あるいはそもそも pager を利用しない手もある
journalctl --no-pager
デフォルトでは PAGER が less に設定されているため、これを例えば vim に変えたい場合は
PAGER=vim journalctl
とできる
参考
journalctl —help
下記は変わる可能性があるので、手元の環境で確認してください。
journalctl [OPTIONS...] [MATCHES...]
Query the journal.
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
-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
-p --priority=RANGE Show entries with the specified priority
-g --grep=PATTERN Show entries with MESSAGE matching 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, verbose, export,
json, json-pretty, json-sse, json-seq, cat,
with-unit)
--output-fields=LIST Select fields to print in verbose/export/json modes
--utc Express time in Coordinated Universal Time (UTC)
-x --catalog Add message explanations where available
--no-full Ellipsize fields
-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
--verify-key=KEY Specify FSS verification key
--force Override of the FSS key pair with --setup-keys
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
--rotate Request immediate rotation of the journal files
--header Show journal header information
--list-catalog Show all message IDs in the catalog
--dump-catalog Show entries in the message catalog
--update-catalog Update the message catalog database
--setup-keys Generate a new FSS key pair
comments powered by Disqus