Reputation: 427
I have an application that uses python logging. All INFO messages are printed to console and DEBUG are saved to a file. I use a library that is too verbose on INFO level, but I still want those messages in my log file. How do I intercept log messages from the library and change their level from INFO to DEBUG?
Upvotes: 2
Views: 385
Reputation: 589
If the library is one that you wrote (I suspect this is not the case) you can adjust the logging level with python's own logging system:
https://docs.python.org/3/library/logging.html#logging-levels
But, it is far more common (and not python language specific) to have to handle tons of crazy logs to debug some issue. Often you did not write the code or don't have time to alter a working library. Logs are always cluttered with stuff you are not immediately interested in. So here is a work around to only see what you are interested in. One great way I have found to reduce the clutter is to filter the output of the tail command:
tail -F ./my_apps.log | grep -iHn "error"
the tail command shows the last part (the tail) of a log. The -F option tells it to aggressively try to follow the log (even if it is deleted and recreated). The pipe "|" joins the two commands.
The grep command uses powerful regular expressions (learn them now!) to find patterns. It does not have to be "error" it can be any pattern and/or wildcards and categories.
If you are working on video game AI, for instance, you could have two terminals tailing the same log:
tail -F ./my_game.log | grep -iHn "monster1" and tail -F ./my_game.log | grep -iHn "human"
and see what each one is up to (so long as the code prefaces the two objects logging calls with the right string). You want to write the logs to the same file so you can debug order of operation issues.
tail and grep are unix tools but they have windows ports (GnuWin32 I think).
you can also filter the console/terminal output:
python myapp.py | grep -iHn "some_pattern"
Upvotes: 1