Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.62k stars 695 forks source link

Having logs printed on console with a different formatting and additional content but that should not affect the logs in file. #1082

Open AvinashPrasad12345 opened 7 months ago

AvinashPrasad12345 commented 7 months ago

I am working on a project where i have to implement loguru at every juncture, to do that i have been asked to store the logs in a specific file in syslog rfc5424 format, i am able to do that successfully till now, but now i have been tasked with printing logs on console as well but their format should be different and some additional content needs to be present on the console, and the console logs format should not affect the format of logs in the file which are in syslog format.

I tried using bind() method but that is not helping me out, every single time i run the code but it adds the additional info to the file as well which i dont want, how to fix this?

@Delgan please help me out

Delgan commented 7 months ago

@AvinashPrasad12345 Do you have a more concrete example please?

You can configure two different handlers with two different formats:

logger.add("file.log", format="{time} {level} {message}")
logger.add(sys.stderr, format="{time} {level} {extra} {message}")
AvinashPrasad12345 commented 7 months ago

@AvinashPrasad12345 Do you have a more concrete example please?

You can configure two different handlers with two different formats:

logger.add("file.log", format="{time} {level} {message}")
logger.add(sys.stderr, format="{time} {level} {extra} {message}")

Hey!, well thanks a ton and yes the issue was resolved, gratified for the assitance

AvinashPrasad12345 commented 7 months ago

actually this is a huge project i am working on, right now I have been tasked with sending these logs created using loguru to opentelemetry collector, how is that done?

please help @Delgan

Delgan commented 7 months ago

For integration with OpenTelemetry, you can take a look at this thread: https://github.com/Delgan/loguru/issues/674#issuecomment-1874389387

AvinashPrasad12345 commented 7 months ago

For integration with OpenTelemetry, you can take a look at this thread: #674 (comment)

Thank you for the clarification, using this methodology, shall i be able to view my logs on opentelemetry collector? along with traces?

Delgan commented 7 months ago

Not being an OpenTelemetry user myself, I can't confirm that with certitude.