如何在Ubuntu中查看系统日志?

19

关于这个问题的后续,LogBack Syslog not working java

我在 Ubuntu 16.04 中使用以下命令查看 syslog ,但是得到了以下结果。这是否是查看的正确方式?

user@xxx:~$ tail -f /var/log/syslog Jun  6 23:08:50 xxx systemd[1]:
Starting Hostname Service... Jun  6 23:08:50 xxx dbus[889]: [system]
Successfully activated service 'org.freedesktop.hostname1' Jun  6
23:08:50 xxx systemd[1]: Started Hostname Service. Jun  6 23:09:41 xxx
gnome-session[2645]: (nautilus:2860): Gtk-WARNING **: Attempting to
read the recently used resources file at
'/home/xxx/.local/share/recently-used.xbel', but the parser failed:
Failed to open file '/home/xxx/.local/share/recently-used.xbel':
Permission denied. Jun  6 23:09:41 xxx org.gtk.vfs.Daemon[2508]:
(gvfsd-recent:15282): Gtk-WARNING **: Attempting to read the recently
used resources file at '/home/xxx/.local/share/recently-used.xbel',
but the parser failed: Failed to open file
'/home/xxx/.local/share/recently-used.xbel': Permission denied. Jun  6
23:09:41 xxx gnome-session[2645]: (zeitgeist-datahub:3069):
Gtk-WARNING **: Attempting to read the recently used resources file at
'/home/xxx/.local/share/recently-used.xbel', but the parser failed:
Failed to open file '/home/seng/.local/share/recently-used.xbel':
Permission denied. Jun  6 23:09:49 xxx gnome-session[2645]:
(nautilus:2860): Gtk-WARNING **: Attempting to read the recently used
resources file at '/home/xxx/.local/share/recently-used.xbel', but the
parser failed: Failed to open file
'/home/xxx/.local/share/recently-used.xbel': Permission denied. Jun  6
23:09:49 xxx org.gtk.vfs.Daemon[2508]: (gvfsd-recent:15282):
Gtk-WARNING **: Attempting to read the recently used resources file at
'/home/xxx/.local/share/recently-used.xbel', but the parser failed:
Failed to open file '/home/xxx/.local/share/recently-used.xbel':
Permission denied. Jun  6 23:09:49 xxx gnome-session[2645]:
(zeitgeist-datahub:3069): Gtk-WARNING **: Attempting to read the
recently used resources file at
'/home/seng/.local/share/recently-used.xbel', but the parser failed:
Failed to open file '/home/xxx/.local/share/recently-used.xbel':
Permission denied. Jun  6 23:17:01 xxx CRON[18877]: (root) CMD (   cd
/ && run-parts --report /etc/cron.hourly)

尝试运行cat /var/log/syslog命令(输出中的一小部分)

Jun  6 23:37:26 xxx whoopsie[1040]: [23:37:26] online
Jun  6 23:37:26 xxx avahi-daemon[1023]: Registering new address record for 2001:e68:4424:afab:c31f:c843:2351:c58 on wlp6s0.*.
Jun  6 23:37:28 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 4340ms.
Jun  6 23:37:32 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 9080ms.
Jun  6 23:37:41 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 17540ms.
Jun  6 23:37:59 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 34190ms.
Jun  6 23:38:09 xxx NetworkManager[1013]: <warn>  [1496763489.9447] dhcp6 (wlp6s0): request timed out
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9448] dhcp6 (wlp6s0): state changed unknown -> timeout
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): canceled DHCP transaction, DHCP client pid 19397
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): state changed timeout -> done
user@xxx:/$ 
尝试运行 cat /var/log/syslog | tail -f 命令。
user@xxx:/$ cat /var/log/syslog | tail -f
Jun  6 23:37:26 xxx whoopsie[1040]: [23:37:26] online
Jun  6 23:37:26 xxx avahi-daemon[1023]: Registering new address record for 2001:e68:4424:afab:c31f:c843:2351:c58 on wlp6s0.*.
Jun  6 23:37:28 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 4340ms.
Jun  6 23:37:32 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 9080ms.
Jun  6 23:37:41 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 17540ms.
Jun  6 23:37:59 xxx dhclient[19397]: XMT: Solicit on wlp6s0, interval 34190ms.
Jun  6 23:38:09 xxx NetworkManager[1013]: <warn>  [1496763489.9447] dhcp6 (wlp6s0): request timed out
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9448] dhcp6 (wlp6s0): state changed unknown -> timeout
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): canceled DHCP transaction, DHCP client pid 19397
Jun  6 23:38:09 xxx NetworkManager[1013]: <info>  [1496763489.9456] dhcp6 (wlp6s0): state changed timeout -> done
user@xxx:/$ 

如果您尝试使用 tail /var/log/syslog > /tmp/log 命令,然后使用 gedit 读取 /tmp/log 文件,是否遇到了同样的问题? - romaric crailox
1
为什么这被标记为“Java”? - slim
1
然后再次变成 user@xxx:/$,没有任何提示出现。 - Tony
@slim是相同的。 - Tony
“cat”和“cat | tail”的输出都看起来正确。你认为这有什么问题吗? - slim
显示剩余12条评论
2个回答

31

看起来你正在尝试从Java中读取syslog,而不是从交互式终端。这段文本看起来像是正确的输出,但格式被破坏了。

tail -f 适用于交互式终端。

尝试使用 cat /var/log/syslog或者直接将/var/log/syslog作为文件打开(如果您的进程有足够的权限)。


你是想从控制台手动读取文件,还是通过Java编程来读取它?如果是前者,请使用cat /var/log/syslog - Dmitriusan
然后只需像这样从Java打开文件 https://www.mkyong.com/java/how-to-read-file-in-java-fileinputstream/ (请确保将文件路径替换为“/ var / log / syslog”) - Dmitriusan
@slim 我遵循这个教程,但不知道如何在系统日志中读取从 Java 发送的消息! - Tony
@Tony,除非您想使用Java读取消息,否则消息的来源与您提出的问题无关。 - slim
3
你尝试过使用 cat /var/log/syslog 或者 less /var/log/syslog 命令吗? - Dmitriusan
显示剩余3条评论

9
在正常的终端窗口(在Ubuntu中,通常是Gnome终端),您所做的- sudo tail /var/log/syslog 应该显示为新行,并且日期/时间戳对齐在左侧。
要么您没有使用正常的终端窗口,要么某些控制字符已将您的终端转换为新行无法正确显示的状态。
如果您使用的不是终端窗口(也许是IDE中的某些东西?),那么请改用普通终端窗口。 tail -f /var/log/syslog 是用于跟随文件增长的。
您可以使用cat /var/log/syslog 将整个文件转储到终端。
您可以使用less /var/log/syslog 交互式地滚动文件。
如果您更喜欢使用GUI编辑器,则可以像gEdit或Atom这样打开 /var/log/syslog 中的文本编辑器,尽管您必须以root身份运行这些编辑器-负责任的做法是复制文件,使其可读,并打开该副本。

journalctl -f 实时查看日志文件 - Time Killer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接