在 Linux 中动态查看日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:41:30  来源:igfitidea点击:

View a log file in Linux dynamically

linuxlogging

提问by Ashwin

I have a log file in .csv format in Linux, that is being updated continuously. I want to view the log file as it is being updated. Is there any Linux command(s) to do that?

我在 Linux 中有一个 .csv 格式的日志文件,该文件正在不断更新。我想查看正在更新的日志文件。是否有任何 Linux 命令可以做到这一点?

回答by Pete

tail -f foo.csv

tail -f foo.csv

回答by teerapap

tail -f yourlog.csv

tail -f yourlog.csv

Newly appended lines will continuously show.

新添加的行将不断显示。

回答by gareth_bowles

Just in case you want to monitor multiple files, there is a nice tool called multitailthat lets you merge the output from two or more files and track them in real time. multitail also lets you navigate back and forth in the monitored file(s).

万一您想监控多个文件,有一个名为multitail的好工具,可以让您合并两个或多个文件的输出并实时跟踪它们。multitail 还允许您在受监视的文件中来回导航。

回答by Noufal Ibrahim

tail -fand all its friends are old school. multitaillooks better but the realway to burn CPU watching your log files is to use glTail.

tail -f它所有的朋友都是老派。multitail看起来更好,但燃烧 CPU 观察日志文件的真正方法是使用glTail

回答by Boolean

tail -lf logfile.csv.

tail -lf 日志文件.csv。

If you logged on to GUI, you can use mousepad to view the log dynamically.

如果您登录到 GUI,您可以使用鼠标垫动态查看日志。

回答by Brian Campbell

As others have pointed out, tail -f fileis the most common solution. The problem is that the results just scroll by, and you can't go back and search them unless your terminal supports it and you have enough lines buffered in your terminal.

正如其他人指出的那样,是最常见的解决方案。问题是结果只是滚动,除非您的终端支持它并且您的终端中缓冲了足够的行,否则您无法返回并搜索它们。tail -f file

A less known solution that I like is to use less; if you type Shift-Fwhile viewing a file with less, it will start following the end of the file just like tail -f. Alternatively, you can start lesswith less +Fto enter this mode on startup. At any time, you can type Ctrl-Cto stop following the file, and then page up and down, search using /, and use lessjust like normal. This can be really helpful if you see something interesting in the log, but it scrolls off screen, or if you want to go back a bit to check on something you might have missed. Once you're done searching around, hit Shift-Fagain to start following the file again.

我喜欢的一个鲜为人知的解决方案是使用less; 如果您键入Shift-F在查看文件时使用less,它将在文件末尾开始,就像tail -f. 或者,你就可以开始lessless +F在启动时进入该模式。您可以随时输入Ctrl-C停止关注该文件,然后上下翻页,使用 搜索/,然后less像往常一样使用。如果您在日志中看到一些有趣的内容,但它会滚动到屏幕外,或者如果您想返回以检查您可能错过的某些内容,这会非常有用。完成搜索后,再次点击Shift- 以F再次开始关注该文件。

multitaillooks like a nice solution for following multiple files in separate windows; if you view multiple files with tail -f, they will each be interleaved with each other (with headers to distinguish them), which may not be the way you want to watch them.

multitail在单独的窗口中跟踪多个文件看起来是一个不错的解决方案;如果您查看多个文件tail -f,它们将相互交错(使用标题来区分它们),这可能不是您想要的观看方式。

tail -F(that is capital -F, as opposed to lowercase -f) is a non-standard flag (available on Linux, Cygwin, MacOS X, FreeBSD and NetBSD), that works better for watching log files, which may be rotated occasionally; it's common for a process to rename a log file, and then create a new log file in its place, in order to avoid any one log file getting too big. tail -fwill keep following the old file, which is no longer the active log file, while tail -Fwill watch for a new file being created, and start following that instead. If you're using lessto follow the file, you can use the --follow-nameflag to make lessact this way as well.

tail -F(即大写-F,而不是小写-f)是一个非标准标志(在 Linux、Cygwin、MacOS X、FreeBSD 和 NetBSD 上可用),它更适合查看日志文件,日志文件可能会偶尔轮换;一个进程重命名一个日志文件,然后在它的位置创建一个新的日志文件是很常见的,以避免任何一个日志文件变得太大。tail -f将继续关注不再是活动日志文件的旧文件,同时tail -F将观察正在创建的新文件,并开始关注该文件。如果您使用的less是跟随文件,您也可以使用该--follow-name标志来less执行此操作。

(thanks to ephemient for the tips on less +Fand less --follow-name)

(感谢ephemient 提供关于less +F和的提示less --follow-name

回答by Paul

vsConsole FileView may help if you prefer to monitor your logs via a web application. See the demo at http://demo.vamonossoftware.com/

如果您更喜欢通过 Web 应用程序监控日志,vsConsole FileView 可能会有所帮助。请参阅http://demo.vamonossoftware.com/ 上的演示

Requires you run a java app server, deploy vsConsole to it, and run agents on the server containing the logs - so I'm guessing its a more heavy weight solution than what you need here. (Its good for dev/testing teams who just want to click on a log file to see it rather than ssh, cd, tail etc)

需要您运行一个 java 应用程序服务器,将 vsConsole 部署到它,并在包含日志的服务器上运行代理 - 所以我猜它是一个比您在这里需要的更重的解决方案。(对于只想点击日志文件而不是 ssh、cd、tail 等的开发/测试团队来说,这是很好的)

回答by Oskar Skog

less -S '-#' 4 /var/log/logfile

-Swill stop the annoying line-wrapping.

-S将停止烦人的换行。

-#4will set the horizontal scrolling step to four columns, instead of the default of half the screen.

-#4将水平滚动步长设置为四列,而不是默认的一半屏幕。

Press the End key to refresh.

按结束键刷新。