简体   繁体   English

如何阅读 a.logs 文档的最后一行?

[英]How to read the last line in a .logs document?

basically i have a logs file which gets edited by a Program all the time.基本上我有一个日志文件,它一直被程序编辑。 I want to check if the Last Line of the Chatlogs includes a "ONLINE:" all the time and if it does, i'd like to save it as a String.我想检查聊天日志的最后一行是否一直包含“在线:”,如果有,我想将其保存为字符串。 So everytime if the last sent message in the logs includes the Word "ONLINE:" the string gets edited to the line including "ONLINE:".因此,每次如果日志中最后发送的消息包含单词“ONLINE:”,则字符串将被编辑为包含“ONLINE:”的行。

I tried creating an ArrayList in a while(true)-Statement all the time (cause the file gets edited) with the whole.logs file and checking the last message but that kinda doesn't really work.我尝试使用整个.logs 文件在一段时间内创建一个 ArrayList(真的)语句(因为文件被编辑)并检查最后一条消息,但这有点不起作用。 Any Ideas?有任何想法吗?

IF this is your code that writes to the log file, then you could just change the method where you log to also do some other work if the current message meets your criteria.如果这是您写入日志文件的代码,那么如果当前消息符合您的标准,您可以更改您登录的方法以执行其他一些工作。 If that is indeed the case, then I also see some code smell - basically program's logic shouldn't depend on logging - so if you have (I assume) some kind of 'handleReceivedNetworkMessage()`, you do your work (save it, filter, whatever you want) and then as the last step log it.如果确实如此,那么我还会看到一些代码异味——基本上程序的逻辑不应该依赖于日志记录——所以如果你有(我假设)某种“handleReceivedNetworkMessage()”,你就可以做你的工作了(保存它,过滤器,无论你想要什么),然后作为最后一步记录它。 Think about logging as just another mean of debugging.将日志记录视为调试的另一种方式。

If on the other hand this isn't your program that writes to the log file - ie the only thing you have access to is a .log file somewhere on filesystem, you could use the WatchService from java.nio.file , to register events that get fired when a file is edited.另一方面,如果这不是您写入日志文件的程序 - 即您唯一可以访问的是文件系统上某处的.log文件,您可以使用WatchService中的java.nio.file来注册事件编辑文件时触发。

It may also be a good idea to think what do you really want to do with that kind of thing.想想你真的想用这种东西做什么也是一个好主意。 What does "save it as a String" means in your context?在您的上下文中,“将其另存为字符串”是什么意思?

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

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