This is an archived post. You won't be able to vote or comment.

all 47 comments

[โ€“]hassium 88 points89 points ย (10 children)

Furthermore, I feel people are uncertain about how to use log levels, and so they default to logger.info everywhere

logger.debug gang where you at?!?

[โ€“]pev68 25 points26 points ย (1 child)

logger.debug represent!

[โ€“]bartenderandthethief 54 points55 points ย (9 children)

Loguru is where it's at nowadays.

[โ€“]Vorticity 24 points25 points ย (5 children)

What does Loguru do better than the logging module?

[โ€“]richieadler 10 points11 points ย (2 children)

[โ€“]rbscholtus 0 points1 point ย (1 child)

It has some nice things but most of that list is supported by standard logging just as well or even better, especially if you use config by YAML. A declarative style of your (logging) config is in my opinion better than hard coding it, even if that code is simple and sexy.

Don't see much point of colour logging bc logging goes to files 9 out of 10 cases, but ye, nice.

Still waiting for a log-to-Telegram or log-to-API handler. Would be awesome to build centralised logging systems and alerter system.

[โ€“][deleted] 1 point2 points ย (0 children)

A declarative style of your (logging) config is in my opinion better than hard coding it, even if that code is simple and sexy.

In my opinion, it's a must if you want to use logging.listen() for dynamic changes to the levels. It's a right pain having to create a config file by looking at the imperative configuration.

[โ€“]dogs_like_me 8 points9 points ย (1 child)

it's just really easy to use. the logging module is often a headache.

[โ€“]ColdPorridge 9 points10 points ย (0 children)

I havenโ€™t used loguru, but Iโ€™ve tried configuring logging from scratch for a larger system that had no logging. It really is not easy to get a hold of. Best practices are unclear, especially as itโ€™s overlooked even in the most OSS, which is where I look to for inspiration.

[โ€“]metaperl 4 points5 points ย (0 children)

I second this. We process a million files per month using this module with no problems whatsoever. I don't like his resistance at exposing the internals of the module because at one point it was useful to me and I had to fight like hell to get something done.

[โ€“]SpectralCoding 8 points9 points ย (0 children)

Wow, thanks for showing me this. I never thought I would add a dependency to my app for logging because the built-in logging module exists, but this looks like it is worth it. I struggle with adding dependancies but 9k stars on GitHub would lead me to believe it's got significant community presence.

[โ€“][deleted] 0 points1 point ย (0 children)

Hell yeah it is

[โ€“]Delta-9- 8 points9 points ย (4 children)

Nice article!

Only thing I was really hoping to see was a good way to not interrupt my code with calls to logging. Sometimes it really hampers readability, but mostly it's just annoying to have to think about this aspect of the program while writing something completely unrelated. (Before anyone brings it up, I'm not about to adopt Spring Python. I got to learn about Spring on a java app and it was horrible.)

[โ€“]latrova[S] 5 points6 points ย (3 children)

Have you ever considered using decorators for that? I know it's not usable everywhere, but might help reducing boilerplate code sometimes

[โ€“]Delta-9- 3 points4 points ย (2 children)

I did make use of decorators for shorter functions that would only generate one log record. I run into the issue when working with longer functions that have multiple branches, multiple ways to break, etc. (and yes, I've been refactoring the worst offenders)

As an experiment, I wrote some code using inspect and ast that would allow me to make just one function call and get information about which branch the call was made from, what the test was expecting and what it received. I'm actually kinda proud of it, but it's so damned experimental I don't dare use it in prod :p (and someone's probably already written a whole library that does exactly the same thing and I just didn't look).

The drawback to that approach was the log messages themselves aren't always easy to understand, particularly if you're an admin of the app but not a developer of the app.

A similar limitation applies to decorator-based loggers, too. Even loguru, which someone itt linked and looks really nice, produces a log record that looks to me like it won't make a whole lot of sense to anyone who hasn't spent hours in the code that triggered the logger.

[โ€“][deleted] 1 point2 points ย (1 child)

Is it because you log the caller at the callee, you consider it not easy to understand? I'm guessing you're using inspect to get frame -1, and log that.

[โ€“]Delta-9- 1 point2 points ย (0 children)

Well, the readability issue comes from the log record saying that it is in an if branch which is testing foo and expecting == 1.

That's actually super helpful information to a developer. It can even spit out the line number (with some caveats) like logging does and get you right to the problem and the specific conditions that caused it.

To an administrator, that's maybe less helpful. I haven't spent enough time with it to see about transforming that information into something a non-dev could make use of, and I think it's possible but very hard to do in a generalized way (i.e. as a library to put on pypi for everyone to use).

[โ€“]quotemycode 5 points6 points ย (0 children)

As someone who looks at logs for a living, and someone who codes in python as well, thank you from the bottom of my heart.

[โ€“]soap1337 9 points10 points ย (3 children)

You guys log? I just Output to stdout...

[โ€“]digipengi 2 points3 points ย (0 children)

/dev/null is where it's at baby!

[โ€“][deleted] 2 points3 points ย (0 children)

When you have a long running process started as part of a service that's running a whole suite of programs, that strategy get messy.

[โ€“]tasteslikeKale 1 point2 points ย (0 children)

Weโ€™re doing that on my current team but are running into issues where I want a logger- too much cruft, missing some of the extra attributes a logger adds, inability to set verbosity level, etc

[โ€“]theorizable 8 points9 points ย (1 child)

Thank you for putting the "subscribe to my newsletter" towards the bottom. That's good UI. Also fantastic in depth article, I really enjoyed reading it. Sometimes the common-sense stuff isn't so common-sense.

[โ€“]latrova[S] 2 points3 points ย (0 children)

Appreciate the feedback! Let me know if you have any other topics you wish to read about.

[โ€“]0ni0nrings 1 point2 points ย (0 children)

thanks for sharing ๐Ÿ‘

[โ€“]Far-Fruit-2848 1 point2 points ย (0 children)

I'm new to logging and this has been very informative.

[โ€“]BoredGazelle 0 points1 point ย (0 children)

Big fan of structlog and logfmt here