解析日志文件并记住上次运行的情况。

huangapple go评论98阅读模式
英文:

Parse a log file remembering last run

问题

我有一个简单的脚本,它打开一个文件(日志文件),并解析其中的特定日志条目/关键字。对于每个匹配的条目,它会触发一个警报。

我想要解决的问题是,我希望修改脚本,以便在上次运行时记住已发送的警报,这样如果脚本重新运行,它就不会为已发送的警报再次发送警报。

编程语言是Golang,有哪些有效的方法可以实现这个功能?使用数据库似乎有点过头了,但我不知道还有其他什么选择?

英文:

I have a simple script that opens a file (log file), parses through it looking for specific log entries/keywords and for each entry that matches it triggers an alert.

The problem that I am trying to solve is that I would like to modify the script to remember the alarms that were already sent when it was last run, so that if the script re-runs it won't keep sending an alert for previously sent alerts.

The coding language is Golang, what are the a valid approach to do this? A database sounds like overkill, but I don't know what other alternatives are out there?

答案1

得分: 1

这取决于日志文件的性质:服务器日志(经典)还是事务日志。即使假设是前者,也要考虑其日志管理(长期保留、轮换等)。

假设是追加数据而不是覆盖的经典日志文件,一个简单的方法是在一个文件中生成找到警报的行。在下一次运行时,如果该行与存储在特殊的“标志”文件中的行匹配,那么警报将不会再次发送。

英文:

It depends on the nature of the log file: server log (classic) or transation log.
Even assuming the former, it depends on its Log Management (long term retention, rotation, ...)

Assuming a classic log files whose data are appended (not overwritten), a simple approach would be to generate in a file the line where the alert is found.
At the next run, if that line matches one stored in that special "flag" file, the alert would not be sent again.

huangapple
  • 本文由 发表于 2017年5月21日 13:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/44093551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定