Closed cwerner closed 4 years ago
Hi @cwerner. :)
I guess the 3rd party package you're importing is using standard logging
and not loguru
, right?
There is a snippet in the Readme which describes how to intercept such logs.
The configuration may differ based on the handler the 3rd party package is using. What you want is to replace the handler it uses (which currently send logs to sys.stderr
with another format) with your own InterceptHandler()
(which will send logs to loguru
where they will be formatted as expected).
Thanks for the quick response.
I briefly added the example code after setting up my own loguru handler and importing the library, but this does not seem to work. As far as I can tell the library (https://github.com/earthobservations/wetterdienst) uses the python logging lib. Maybe I'd need to customise as you suggested...
Unfortunately, this seems a bit complicated so I might do this at a later stage. But thanks again for the effort!
I think dealing with standard logging
is a bit tricky.
You may try to add this during loguru
configuration:
wetterdienst_logger = logging.getLogger("wetterdienst")
wetterdienst_logger.setLevel(1)
wetterdienst_logger.addHandler(InterceptHandler())
Great! Thanks - this works 👍
Now I would need to silence the original logger though (since I see the original plus the loguru-ified one)...
Maybe by adding wetterdienst_logger.propagate = False
. :thinking:
Otherwise, just remove all handlers. :grin:
for log in [logging.root, wetterdienst_logger]:
for handler in log.handlers:
log.removeHandler(handler)
Excellent! wetterdienst_logger.propagate = False
seems to do the trick 👍
Thanks for taking the time to guide me through the depths of python logging 😄
Hi there.
I hope you don't mind me posting another issue I have? I currently import a 3rd party package which produces quite a bit of log outputs. Is it possible to pipe this into Loguru and make it adhere to my defined log formatting? I don't want to filter or disable it but change appearance...
Here are a couple of lines to illustrate it (last one is from my script and the desired format for all logs):