![journalctl:如何在其输出中显示颜色?](https://linux22.com/image/690742/journalctl%EF%BC%9A%E5%A6%82%E4%BD%95%E5%9C%A8%E5%85%B6%E8%BE%93%E5%87%BA%E4%B8%AD%E6%98%BE%E7%A4%BA%E9%A2%9C%E8%89%B2%EF%BC%9F.png)
没有颜色的输出journalctl
看起来真的很乱。是否可以为其输出添加颜色?在我的系统上,似乎只有重要的行被突出显示。甚至dmesg
可以通过添加开关输出颜色--color
!
答案1
颜色支持journalctl
记录在man journalctl
:
输出到 tty 时,行会根据优先级别进行着色:级别 ERROR 及更高级别的行会着色为红色;级别 NOTICE 及更高级别的行会突出显示;其他行则正常显示。
这是来自 systemd 229 的。从手册页中可以看出,没有其他内置颜色控件journalctl
。
答案2
如果原始守护进程输出已着色,但使用时颜色丢失journalctl
,则该--output cat
选项将恢复它们。请参阅手动的更多细节。
答案3
安装ccze,然后像这样使用它:
journalctl -b -k -f | ccze [-A] [-o nolookups]
解释:
-A, --raw-ansi
If one wants to enable raw ANSI color sequences instead of using curses, this option does just that.
-o, --options OPTIONS...
However, lookups is an option that might be better to disable. When on, ccze will try to look up usernames, service
names, and lots of other stuff, which will slow down coloring a great deal. If one is piping a long log through ccze,
this option might be turned off [nolookups] to speed up the process.
答案4
在长期寻找答案之后,我实际上找到了适合我的方法(使用 Rails 日志和 journalctl)——只需添加 --all 选项。
journalctl -u puma --all
来自手册页:
-a, --all Show all fields in full, even if they include unprintable characters or are very long. By default, fields with unprintable characters are abbreviated as "blob data". (Note that the pager may escape unprintable characters again.)