博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Ubuntu上查看和写入系统日志文件
阅读量:2513 次
发布时间:2019-05-11

本文共 4559 字,大约阅读时间需要 15 分钟。

image

Linux logs a large amount of events to the disk, where they’re mostly stored in the /var/log directory in plain text. Most log entries go through the system logging daemon, syslogd, and are written to the system log.

Linux将大量事件记录到磁盘上,这些事件通常以纯文本格式存储在/ var / log目录中。 大多数日志条目都通过系统日志记录守护程序syslogd写入系统日志。

Ubuntu includes a number of ways of viewing these logs, either graphically or from the command-line. You can also write your own log messages to the system log — particularly useful in scripts.

Ubuntu提供了多种以图形方式或从命令行查看这些日志的方式。 您还可以将自己的日志消息写入系统日志-在脚本中特别有用。

以图形方式查看日志 (Viewing Logs Graphically)

To view log files using an easy-to-use, graphical application, open the Log File Viewer application from your Dash.

要使用易于使用的图形应用程序查看日志文件,请从Dash中打开“日志文件查看器”应用程序。

image

The Log File Viewer displays a number of logs by default, including your system log (syslog), package manager log (dpkg.log), authentication log (auth.log), and graphical server log (Xorg.0.log). You can view all the logs in a single window – when a new log event is added, it will automatically appear in the window and will be bolded. You can also press Ctrl+F to search your log messages or use the Filters menu to filter your logs.

日志文件查看器默认显示许多日志,包括系统日志(syslog),程序包管理器日志(dpkg.log),身份验证日志(auth.log)和图形服务器日志(Xorg.0.log)。 您可以在一个窗口中查看所有日志–添加新的日志事件后,该事件将自动出现在窗口中并以粗体显示。 您也可以按Ctrl + F来搜索日志消息,或使用“过滤器”菜单过滤日志。

image

If you have other log files you want to view – say, a log file for a specific application – you can click the File menu, select Open, and open the log file. It will appear alongside the other log files in the list and will be monitored and automatically updated, like the other logs.

如果您要查看其他日志文件(例如,特定应用程序的日志文件),则可以单击“文件”菜单,选择“打开”,然后打开日志文件。 它会与列表中的其他日志文件一起显示,并且会像其他日志一样受到监视和自动更新。

image

写入系统日志 (Writing to the System Log)

The logger utility allows you to quickly write a message to your system log with a single, simple command. For example, to write the message Hello World to your system log, use the following command:

logger实用程序使您可以通过一个简单的命令将消息快速写入系统日志。 例如,要将消息“ Hello World”写到系统日志中,请使用以下命令:

logger “Hello World”

记录器“ Hello World”

image

You may also wish to specify additional information – for example, if you’re using the logger command within a script, you may want to include the name of the script:

您可能还希望指定其他信息–例如,如果在脚本中使用logger命令,则可能要包括脚本名称:

logger –t ScriptName “Hello World”

记录器–t ScriptName“ Hello World”

image

在终端中查看日志 (Viewing Logs in the Terminal)

The dmesg command displays the Linux kernel’s message buffer, which is stored in memory. Run this command and you’ll get a lot of output.

dmesg命令显示Linux内核的消息缓冲区,该消息缓冲区存储在内存中。 运行此命令,您将获得大量输出。

image

To filter this output and search for the messages you’re interested in, you can pipe it to grep:

要过滤此输出并搜索您感兴趣的消息,可以将其通过管道传递给grep

dmesg | grep something

dmesg | grep的东西

You can also pipe the output of the dmesg command to less, which allows you to scroll through the messages at your own pace. To exit less, press Q.

您还可以将dmesg命令的输出传递给less ,这使您可以按自己的步调滚动消息。 要少退出,请按Q。

dmesg | less

dmesg | 减

image

If a grep search produces a large amount of results, you can pipe its output to less, too:

如果grep搜索产生大量结果,则也可以将其输出传递给以下内容:

dmesg | grep something | less

dmesg | grep的东西| 减

In addition to opening the log files located in /var/log in any text editor, you can use the cat command to print the contents of a log (or any other file) to the terminal:

除了在任何文本编辑器中打开/ var / log中的日志文件之外,您还可以使用cat命令将日志(或任何其他文件)的内容打印到终端:

cat /var/log/syslog

猫/ var / log / syslog

Like the dmesg command above, this will produce a large amount of output. You can use the grep and less commands to work with the output:

像上面的dmesg命令一样,这将产生大量输出。 您可以使用grepless命令来处理输出:

grep something /var/log/syslog

grep的东西/ var / log / syslog

less /var/log/syslog

少/ var / log / syslog

Other useful commands include the head and tail commands. head prints the first n lines in a file, while tail prints the last n lines in the file – if you want to view recent log messages, the tail command is particularly useful.

其他有用的命令包括headtail命令。 head打印文件的前n行,而tail打印文件的后n行–如果要查看最近的日志消息,tail命令特别有用。

head -n 10 /var/log/syslog

头-n 10 / var / log / syslog

tail -n 10 /var/log/syslog

尾-n 10 / var / log / syslog

image

Some applications may not write to the system log and may produce their own log files, which you can manipulate in the same way – you’ll generally find them in the /var/log directory, too. For example, the Apache web server creates a /var/log/apache2 directory containing its logs.

某些应用程序可能不会写入系统日志,并且可能会生成它们自己的日志文件,您可以用相同的方式对其进行操作-通常您也可以在/ var / log目录中找到它们。 例如,Apache Web服务器创建一个包含其日志的/ var / log / apache2目录。

翻译自:

转载地址:http://eytwd.baihongyu.com/

你可能感兴趣的文章
rsync远程同步的基本配置与使用
查看>>
第二天作业
查看>>
访问属性和访问实例变量的区别
查看>>
Spring MVC 异常处理 - SimpleMappingExceptionResolver
查看>>
props 父组件给子组件传递参数
查看>>
【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
查看>>
十二种获取Spring的上下文环境ApplicationContext的方法
查看>>
UVA 11346 Probability 概率 (连续概率)
查看>>
linux uniq 命令
查看>>
Openssl rand命令
查看>>
HDU2825 Wireless Password 【AC自动机】【状压DP】
查看>>
BZOJ1015: [JSOI2008]星球大战starwar【并查集】【傻逼题】
查看>>
HUT-XXXX Strange display 容斥定理,线性规划
查看>>
mac修改用户名
查看>>
一道关于员工与部门查询的SQL笔试题
查看>>
Canvas基础
查看>>
[Hive - LanguageManual] Alter Table/Partition/Column
查看>>
可持久化数组
查看>>
去除IDEA报黄色/灰色的重复代码的下划波浪线
查看>>
Linux发送qq、网易邮件服务配置
查看>>