0

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:".

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. Any Ideas?

  • Hello, and welcome to StackOverflow. Please make your question more specific and easier for others to answer by adding examples of the code you are trying to run and exactly how it "kinda doesn't really work". For example, are you getting error messages, or just behavior you don't want? Also, consider https://stackoverflow.com/questions/1073274/in-java-what-is-the-best-safest-pattern-for-monitoring-a-file-being-appended-to – Kukanani Jun 05 '20 at 14:12

1 Answers1

0

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. 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 - i.e. 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.

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 ?

Tooster
  • 322
  • 3
  • 14
  • If the answer solved your problem or is somehow useful, remember to set it as accepted or upvote it. – Tooster Jun 07 '20 at 22:25