0

I want to stream log file using python. each log will be checked, if the log record contain any word (e.g. Error) it'll triggered an event. It'll be like this...

python script load a log file -> checking every log (triggered an event if defined condition occurs) -> keep running and waiting for new log -> repeat checking for new log

as my explore about this, i still cant get it about how to waiting new log and checking the new log (if come up a new log). is it possible to implement this with python? Any suggestions to do this? Thanks.

1 Answers1

0

check this link to get the latest file - How to get the latest file in a folder using python You should be able to build your logic on top of it to check if the latest file in the directory is same as the last time you checked or not. You may have to tweak slightly to make sure you do not miss any log files in case more than one log file is created while your program waits.

  • Ah thank you, you're right! It's give me more another preventive case. I work on laravel and lumen logs, which means generate new log everyday. Still in the same case of my question, how to check record that added when the program waits? – Dhody Rahmad Hidayat Mar 05 '20 at 03:53
  • Sorry, I could not reply to you sooner. I was thinking you need to look for a new log file on every check, but it looks like you want to check for a new record in the same log file. I cannot fully answer that without knowing how your log file looks but if records are appended to the log file, you may use 'seek' or 'readlines' methods to serve your purpose. Following links may help - https://www.daniweb.com/programming/software-development/threads/24544/how-do-i-read-the-last-line-of-a-text-file https://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail – RAVI KIRAN VELAMA Mar 10 '20 at 08:38