如何使用LNAV查看Linux中的日志文件

时间:2020-02-23 14:40:05  来源:igfitidea点击:

在Linux系统中查看/读取日志文件的最佳方式是什么?
LNAV是一个功能强大的开源和命令行增强型日志文件导航器和用于Linux和UNIX系统的查看器。
它创建了帮助Sysadmins浏览 /var/log目录和易于查看基于时间戳和日志级别过滤器的日志。

LNAV可以从不同文件中交互交互,并能够随着时间的推移生成消息的直方图。
通过使用其热键来导航,挖掘掉期应用和基础设施问题应该是一个快速而无痛的过程。

如何在Linux上安装LNAV?

LNAV可以使用简单的步骤安装在基于Red Hat基于Debian的系统上。

要在Ubuntu/debian系统上安装lnav,请使用sudo权限运行以下命令。

$sudo apt-get install lnav
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following additional packages will be installed:
   libpcrecpp0v5
 The following NEW packages will be installed:
   libpcrecpp0v5 lnav
 0 upgraded, 2 newly installed, 0 to remove and 90 not upgraded.
 Need to get 672 kB of archives.
 After this operation, 2,404 kB of additional disk space will be used.
 Do you want to continue? [Y/n] y

如果我们正在运行CentOS或者RHEL,请从EPEL存储库安装LNAV。

sudo yum -y install epel-release
sudo yum -y install lnav

在Fedora Server /桌面上,可以使用包装包 dnf包管理器。

sudo dnf -y install lnav

如何在Linux上使用lnav?

每个安装都伴随着使用教程。
要开始LNAV,基本使用语法是:

lnav [options] [logfile1 logfile2 …]

看到以下几个例子。 1.打开 /var/log/messages文件或者文件 /var/log/syslog对于Ubuntu。

$lnav

将显示一个带有类似下面类似的日志消息的输出。

2.加载所有文件 /var/log

$lnav  /var/log

3.要加载特定的日志文件,请提供其绝对路径。

$lnav /var/log/syslog

4.要使用时间戳查看日志文件的

$lnav -t /var/log/nginx/access.log

5.加载旋转日志 - 存档

$lnav /var/log/nginx/access.log-20190128.gz

6.获取帮助

$lnav --help

lnav关键绑定:?
查看/留下在线帮助文本。
q退出程序。

所有lnav选项

-h         Print this message, then exit.
  -H         Display the internal help text.
  -I path    An additional configuration directory.
  -i         Install the given format files and exit.  Pass 'extra'
             to install the default set of third-party formats.
  -u         Update formats installed from git repositories.
  -C         Check configuration and then exit.
  -d file    Write debug messages to the given file.
  -V         Print version information.
  -a         Load all of the most recent log file types.
  -r         Load older rotated log files as well.
  -t         Prepend timestamps to the lines of data being read in
             on the standard input.
  -w file    Write the contents of the standard input to this file.
  -c cmd     Execute a command after the files have been loaded.
  -f path    Execute the commands in the given file.
  -n         Run without the curses UI. (headless mode)
  -q         Do not print the log messages after executing all
             of the commands or when lnav is reading from stdin.

如果我们正在寻找中央日志管理工具,我建议Graylog。