Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.82k stars 697 forks source link

Modify logging format from imported module? #328

Closed cwerner closed 4 years ago

cwerner commented 4 years ago

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):

[2020-09-18 12:43:59,032][wetterdienst.data_collection][INFO] - No files found for precipitation/hourly/recent/station_id_2542. Station will be skipped.
[2020-09-18 12:43:59,065][wetterdienst.api][INFO] - Combination for precipitation/hourly/PeriodType.NOW does not exist and is skipped.
20-09-18 12:43:59.3 | WARNING  | preprocess.py.main:89 - Only using 2 stations for now (target, source): [2290, 2542]
Delgan commented 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).

cwerner commented 4 years ago

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!

Delgan commented 4 years ago

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())
cwerner commented 4 years ago

Great! Thanks - this works 👍

Now I would need to silence the original logger though (since I see the original plus the loguru-ified one)...

Delgan commented 4 years ago

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)
cwerner commented 4 years ago

Excellent! wetterdienst_logger.propagate = False seems to do the trick 👍

Thanks for taking the time to guide me through the depths of python logging 😄