简体   繁体   English

在终端中仅显示新添加的日志文件行

[英]Show only newly added lines of logfile in terminal

I use tail -f to show the contents of a logfile.我使用tail -f来显示日志文件的内容。

What I want is when the logfile content changes, instead of appending the new lines to my screen, only the newly added lines should be shown on my screen.我想要的是当日志文件内容发生变化时,而不是将新行附加到我的屏幕上,只应在我的屏幕上显示新添加的行。

So as if a clearscreen was made every time before printing the new lines.因此,就好像每次在打印新行之前都制作了一个清晰的屏幕。

I tried to find a solution by web search but couldn't find anything useful.我试图通过 web 搜索找到解决方案,但找不到任何有用的东西。

edit: In my case it happens that several lines will be added at once (it is a php error logfile).编辑:在我的情况下,会同时添加几行(这是一个 php 错误日志文件)。 So I am looking for a solution where more than the single last line can be shown on screen.所以我正在寻找一种解决方案,可以在屏幕上显示多于最后一行。

The watch command in combination with the tail command shows the last line of a log file with the intervall of every 2 seconds. watch命令与tail命令结合使用以每 2 秒为间隔显示日志文件的最后一行。 Basically it doesn't refresh whenever a new line is appended to the log file but since you could specifiy an intervall it might help you for your use case.基本上,只要将新行附加到日志文件,它就不会刷新,但由于您可以指定一个间隔,它可能会帮助您解决您的用例。

watch -t tail -1 <path_to_logfile>

If you need a faster intervall like every 0.5 seconds, then you could specify it with the 'n' option ie:如果您需要更快的间隔,例如每 0.5 秒,那么您可以使用 'n' 选项指定它,即:

watch -t -n 0.5 tail -1 <path_to_logfile>

Try尝试
$ watch 'tac FILE | grep -m1 -C2 PATTERN | tac' $ watch 'tac FILE | grep -m1 -C2 PATTERN | tac' watch 'tac FILE | grep -m1 -C2 PATTERN | tac'
where在哪里
PATTERN is any keyword (or regexp) to identify errors you seek in the log, PATTERN是任何关键字(或正则表达式),用于识别您在日志中寻找的错误,
tac prints the lines in reverse, tac反向打印行,
-m is a max count of matching lines to grep, -m是与 grep 匹配的最大行数,
-C is any number of lines of context (before and after the match) to show (optional). -C是要显示的任意数量的上下文行(匹配之前和之后)(可选)。

That would be similar to那将类似于
$ tail -f FILE | grep -C2 PATTERN $ tail -f FILE | grep -C2 PATTERN tail -f FILE | grep -C2 PATTERN
if you didn't mind just appending occurrences to the output in real-time.如果您不介意将事件实时附加到 output。

But if you don't know any generic PATTERN to look for at all,但是,如果您根本不知道要查找的任何通用 PATTERN,
you'd have to just follow all the updates as the logfile grows:随着日志文件的增长,您只需关注所有更新:
$ tail -n0 -f FILE $ tail -n0 -f FILE

Or even, create a copy of the logfile and then do a diff:甚至,创建日志文件的副本,然后进行比较:

  1. Copy: cp file.log{,.old}复制: cp file.log{,.old}
  2. Refresh the webpage with your.php code (or whatever, to trigger the error)使用您的.php 代码(或其他任何内容以触发错误)刷新网页
  3. Run: diff file.log{,.old}运行: diff file.log{,.old}
    (or, if you prefer sort to diff: $ sort file.log{,.old} | uniq -u ) (或者,如果您更喜欢排序而不是差异: $ sort file.log{,.old} | uniq -u

The curly braces is shorthand for both filenames (see Brace Expansion in $ man bash )花括号是两个文件名的简写(参见 $ man bash中的大括号扩展

If you must avoid any temp copies, store the line count in memory:如果您必须避免任何临时副本,请将行数存储在 memory 中:

  1. z=$(grep -c ^ file.log)
  2. Refresh the webpage to trigger an error刷新网页触发错误
  3. tail -n +$z file.log

The latter approach can be built upon, to create a custom scripting solution more suitable for your needs (check timestamps, clear screen, filter specific errors, etc).可以建立后一种方法,以创建更适合您需求的自定义脚本解决方案(检查时间戳、清除屏幕、过滤特定错误等)。 For example, to only show the lines that belong to the last error message in the log file updated in real-time:例如,仅显示属于实时更新的日志文件中最后一条错误消息的行:

$ clear; z=$(grep -c ^ FILE); while true; do d=$(date -r FILE); sleep 1; b=$(date -r FILE); if [ "$d";= "$b" ]; then clear; tail -n +$z FILE; z=$(grep -c ^ FILE); fi; done $ clear; z=$(grep -c ^ FILE); while true; do d=$(date -r FILE); sleep 1; b=$(date -r FILE); if [ "$d";= "$b" ]; then clear; tail -n +$z FILE; z=$(grep -c ^ FILE); fi; done clear; z=$(grep -c ^ FILE); while true; do d=$(date -r FILE); sleep 1; b=$(date -r FILE); if [ "$d";= "$b" ]; then clear; tail -n +$z FILE; z=$(grep -c ^ FILE); fi; done
where在哪里
FILE is, obviously, your log file name; FILE显然是您的日志文件名;
grep -c ^ FILE counts all lines in a file (that is almost, but not entirely unlike cat FILE|wc -l that would only count newlines); grep -c ^ FILE计算文件中的所有行(几乎,但不完全不同于cat FILE|wc -l只计算换行);
sleep 1 sets the pause/delay between checking the file timestamps to 1 second, but you could change it to even a floating point number (the less the interval, the higher the CPU usage). sleep 1将检查文件时间戳之间的暂停/延迟设置为 1 秒,但您甚至可以将其更改为浮点数(间隔越短,CPU 使用率越高)。
To simplify any repetitive invocations in future, you could save this compound command in a Bash script that could take a target logfile name as an argument, or define a shell function, or create an alias in your shell, or just reverse-search your bash history with CTRL+R. To simplify any repetitive invocations in future, you could save this compound command in a Bash script that could take a target logfile name as an argument, or define a shell function, or create an alias in your shell, or just reverse-search your bash CTRL+R 历史记录。 Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM