python - 如何从Linux日志文件中只获取新行?

标签 python linux python-3.x unix logging

我有两个想法。首先,我以为我读取了该文件,删除了它,并且日志记录最终会重新创建它(我错了)。第二次尝试时,我想我应该制作该文件的副本,并在一段时间后将副本与原始文件进行比较。你怎么认为?第二种方法是一个好的选择吗?如果是这样,有效比较文件的好方法是什么?在我看来,这会非常低效,因为我必须读取一个大日志文件两次并逐行比较......

我也对其他方法感兴趣。

带有轮询的东西并不理想,这应该是我用 crontab 调用的工作。

感谢您的帮助。

最佳答案

如果您运行的平台使用 systemd,您可以使用 journalctl 命令。

journalctl 命令有一个非常强大的 --since 选项

您可以使用它来获取特定时间后的日志:

journalctl --since "2018-10-08 13:00:00"

查看不同时间之间的日志

journalctl --since "2018-10-08 13:00:00" --until "2018-10-08 13:30:00"

或者N次前

journalctl --since "10min ago"

要查看特定应用程序日志,请使用-u选项

journalctl -u tomcat.service --since "1 hr ago"

引用文献:

https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs

https://www.loggly.com/ultimate-guide/using-journalctl/

关于python - 如何从Linux日志文件中只获取新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52743879/

相关文章:

python - 为什么我在 pip 安装模块时得到这些 "WARNING: Target Directory <directory> already exists."?

python - Python : prohibit instantiation 中没有抽象方法的抽象数据类

python - AWS Lambda Function无法访问其他服务

python - 子图 : tight_layout changes figure size

python - 导入 pycurl : Library not loaded: @rpath/libcrypto. 1.1.dylib:原因:找不到图像

c++ - 从 Linux 中的非子进程获取退出代码

linux - 在 curl 获取的 2 个文件之间添加一个新行

python - 插入很多产品opencart脚本

python - QT python连接传递参数给函数

c - 为什么权限参数会影响我的小型测试程序中写入文件的内容?