0

Please point to the existing answer I couldn't find.

I have subdirectories with a lot of active *.log files that being modified by several python scripts. What I'm trying to achieve is to catch the moment when a log entry about python exception appears in one of the log files (let's say when the row "Traceback (most recent call last):" is being written) and then get/read the full exception message.

   Traceback (most recent call last):
      File "test.py", line 236, in main
        if response == None:
    AttributeError: 'NoneType' object has no attribute 'status_code'

Here is an example part that I want to monitor and catch.

martineau
  • 119,623
  • 25
  • 170
  • 301
smoke
  • 67
  • 1
  • 6
  • I guess you'll have to use some kind of multithreading, 1 per log file you monitor, and open the log as a stream, which checks each new line, when it comes, and so on... – DevLounge Mar 27 '21 at 22:58
  • Does this answer your question? [How can I tail a log file in Python?](https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python) – DevLounge Mar 27 '21 at 22:59
  • I would recommend doing an os.walk then just do a line by line read of each file and do a `'Traceback (most recent call last):' in line` then just continue reading until `'[Timestamp/Format of next log entry]' in line'` – tayler6000 Mar 27 '21 at 23:00
  • Your question is too vague—which may be why you can't find any existing answers. So (guessing about many details) one way to do this might be to use the [watchdog](https://pypi.org/project/watchdog/) module to monitor changes in the subdirectories where the log files are being written. Here's its current [documentation](https://python-watchdog.readthedocs.io/en/v0.10.3/). – martineau Mar 27 '21 at 23:17
  • @DevLounge The problem is log files are being added/deleted, so I have to monitor wildcard filename *.log. – smoke Mar 27 '21 at 23:29
  • @martineau I looked into the watchdog and I can get file modification events, but don't know how to proceed from there. Also, don't know how to watch for wildcard filenames like *.log – smoke Mar 27 '21 at 23:29
  • smoke: Maybe the information and examples in the article [Using Python's Watchdog to monitor changes to a directory](https://www.michaelcho.me/article/using-pythons-watchdog-to-monitor-changes-to-a-directory) would help. You can use the [`fnmatch`](https://docs.python.org/3/library/fnmatch.html#module-fnmatch) module to do simple wildcard filename matching. – martineau Mar 28 '21 at 07:31

0 Answers0