Capturing and filtering all logs locally in terminal

227 Views Asked by At

I have a service that typically runs in a hosted production environment, spitting out logs and whatnot.

I've written a script to test different pieces of the service locally, and i want to capture all the logs being output by the different libraries and files, and only print out the relevant logs to my terminal console.

I can't seem to figure out how to make use of the logger.addFilter or addHandler methods, and im not really sure how to make use of the StreamHandler either.

I've noticed that when i pass an instantiated StreamHandler to my root_logger handlers, it removes all top level logs, but doesn't capture loggers with nested names still.

ex.

root_logger = logging.getLogger()
stream_handler = logging.StreamHandler()
root_logger.addHandler(stream_handler)

...

run_script()

> {'name': 'service.library.loggerA'} < still logged
> {'name': 'service.library.loggerB'} < still logged
> {'name': 'ServiceClass'} < NOT logged

Would anyone be able to help me underestand what all these pieces do and how i can use them to filter out all logs in my tool?

0

There are 0 best solutions below