Linux provides a lot of different types of logs by default. These files are generally located at /var/log . There may be some exceptions like third party applications but the configuration of log location can be changed to the /var/log directory. In this post, we will look at default log files and how to list, tail, search, filter these logs.
Linux默认提供许多不同类型的日志。 这些文件通常位于/ var / log 。 可能有一些例外,例如第三方应用程序,但是日志位置的配置可以更改为/ var / log目录。 在本文中,我们将研究默认日志文件以及如何列出,尾部,搜索,过滤这些日志。
列出日志文件 (List Log Files)
Logs files can be simply listed by using ls command but keep in mind there are directories they contain different files for logs.
可以使用ls命令简单地列出日志文件,但是请记住,有一些目录包含用于日志的不同文件。
$ ls /var/log/

We can list in a recursive manner to get files and folder under /var/log directory like below.
我们可以递归方式列出,以在/ var / log目录下获取文件和文件夹,如下所示。
$ ls -R /var/log/

读取日志文件(Reading Log Files)
There are different methods to read log file but we will use less which have practical solutions while reading the log file.
有多种读取日志文件的方法,但是在读取日志文件时,我们将使用较少的具有实际解决方案的方法。
$ less auth.log

Space will skip to the next page also page up / page down will work too.
空格将跳至下一页,也可以向上翻页/向下翻页。
搜索日志文件 (Searching Log File)
Less have the functionality to search a text file were in this situation a log file. After opening log files with less use /auth to search “auth” term down to the file pages.
在这种情况下,很少有具有搜索文本文件的功能的日志文件。 在使用较少的日志文件打开日志后,使用/ auth搜索“ auth ”一词到文件页面。
/auth

To continue to search term without entering, again and again, press n for the next match or p for the previous search. After arriving at the file end if no match exists we will get a message like below at the end of the terminal.
要继续搜索词条而无需一次又一次输入,请按n进行下一个匹配,按p进行上一次搜索。 到达文件末尾后,如果不存在匹配项,我们将在终端末尾收到如下消息。

过滤日志文件(Filtering Log File)
Searching is a way to see occurrences in a log file and previous and next events. An alternative is filtering log files. Grep is a very capable tool to filter log files. We will filter for “auth” for all files named auth.log* . We named files auth.log* because old auth.log files are gzipped and have gz extension.
搜索是查看日志文件中的事件以及上一个和下一个事件的一种方式。 一种替代方法是过滤日志文件。 Grep是一个非常强大的工具,可以过滤日志文件。 我们将过滤所有名为auth.log *的文件的“ auth” 。 我们将文件命名为auth.log *,因为旧的auth.log文件已压缩并具有gz扩展名。
$ zgrep "authen" auth.log*

If we want to colorize findings we can use normal grep with the same filter term as below.
如果我们想给发现着色,我们可以使用具有以下相同过滤条件的普通grep。
$ zgrep "authen" auth.log* | grep "auth"

筛选所有日志文件(Filter All Log Files)
Actually filtering or search all files are not different but as an example, we can look at it by specifying and IP address.
实际上,过滤或搜索所有文件没有什么不同,但是作为示例,我们可以通过指定IP地址来进行查看。
$ zgrep "192.168.122.1" * | less

We can use less for search other terms like username “ismail”
我们可以减少搜索其他术语,例如用户名“ ismail”
/ismail
