【问题标题】:How to view syslog in ubuntu?如何在 ubuntu 中查看系统日志?
【发布时间】:2017-11-07 16:59:41
【问题描述】:

跟进这个问题,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 &gt; /tmp/log然后使用gedit读取/tmp/log,你有同样的问题吗?
  • 为什么这个标签是“java”?
  • @slim 我想从 java 中读取。我已将我之前的问题链接到此处。
  • @Tony 我们不需要参考另一个问题来解决您的问题。
  • @romariccrailox 我试过了。 user@xxx:~$ cd /

标签: linux ubuntu logging sys


【解决方案1】:

看起来您正试图从 Java 中读取 syslog,而不是从交互式终端。文本看起来像是正确的输出,但格式混乱。

tail -f 适合交互终端。

试试cat /var/log/syslog,或者直接打开/var/log/syslog作为一个文件(如果你的进程有足够的权限)。

【讨论】:

  • 您是尝试从控制台手动读取文件,还是从 Java 以编程方式读取文件?在第一种情况下,使用cat /var/log/syslog
  • 然后像mkyong.com/java/how-to-read-file-in-java-fileinputstream那样从Java打开文件(确保用/var/log/syslog替换文件路径)
  • 您的问题与 Java 无关。如果您使用ProcessBuilder 或类似方法来执行tail,请将您的代码添加到问题中。
  • @slim 我按照本教程进行操作,但不知道如何在 syslog 中读取从 Java 发送的消息!
  • 你试过cat /var/log/syslog还是less /var/log/syslog
【解决方案2】:

在普通终端窗口(在 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 身份运行这些编辑器——负责任的做法是复制文件,使其对您的用户可读,然后打开它。

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2018-12-20
    • 2021-12-18
    • 1970-01-01
    • 2011-08-29
    • 2014-04-30
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多