Modern Python logging

159,464
0
Published 2024-01-22
A logging tutorial.

At some point, print statements aren't enough. When that time comes in Python, you should reach for the builtin logging package. It may be old (committed in 2002!), but it is the standard in Python. Unfortunately though, it being so old means that it's full of stuff you shouldn't use, it doesn't follow modern conventions like PEP8 coding style, and many tutorials are vastly outdated. If you'd like to understand the modern picture of logging in Python, then this video is for you.

― mCoding with James Murphy (mcoding.io/)

Source code: github.com/mCodingLLC/VideosSampleCode
logging docs: docs.python.org/3/library/logging.html
Make your own diagrams: excalidraw.com/

Local Elevator by Kevin MacLeod is licensed under a Creative Commons Attribution 4.0 license. creativecommons.org/licenses/by/4.0/

SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!
patreon.com/mCoding

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJ…

Want to donate crypto? Check out the rest of my supported donations on my website!
mcoding.io/donate

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information, Pi

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------
Discord: discord.gg/Ye9yJtZQuN
Github: github.com/mCodingLLC/
Reddit: www.reddit.com/r/mCoding/
Facebook: www.facebook.com/james.mcoding

CHAPTERS
---------------------------------------------------
0:00 Intro
0:34 Self-sponsorship
0:48 logging is the standard
1:21 basicConfig
2:08 dictConfig
2:44 the complete logging picture
7:51 Example: log to stdout
9:37 logging JSON/YAML config
10:44 Example: errors to stdedd and all to file
12:37 JSON logs
15:46 Extra context with the extra param
16:13 Custom filter
17:10 A glaring flaw
17:44 Log off the main thread with QueueHandler
19:15 Success!
19:26 Library logging?
20:16 logging4p
21:02 Thanks

All Comments (21)
  • @megaing1322
    That "include the timezone, *trust me*" has some history... xD
  • @thisoldproperty
    Jeans down, this is the best python logging explanation I've ever seen. Brilliant work.
  • @atrus3823
    I’ve always found Python logging super opaque. After hours of reading and playing around, I still never really understood it. Somehow, in 20 minutes, you covered way more than I even knew was possible and it all makes so much sense now! Thank you! PS: Still waiting on your C++ value category video 🤞
  • This breakdown on the logger is truly amazing. One comment regarding the queue handler implementation 18:11 . This implementation only works, as is, on Python 3.12 as prior versions don't have that available for dictConfig.
  • @yxh
    This is my favorite format of your videos so far. No memey images, just solid information with dry humor and guiding text peppered helpfully throughout. Great content as usual.
  • As someone who owns a library, and rolled my own logger (in JavaScript), I appreciate the deep dive in proper logger design and methodology. I may not be able to use the examples here, but the lesson is well-received. As a self-taught developer, I missed a lot of the history in why things are written the way they are.
  • @djl3009
    The "complete logging picture" @02:44 is what I've been missing. Thank you!
  • @draufunddran
    Wow... every python video from you is pure Gold. I'm only aware of one "recent" async video, but maybe you could do a more detailed video on async and await in python. I and certainly thousands of others would appreciate this. Keep up the awesome work!
  • @PBJYM
    I've literally been trying to understand python logging for the past few days. This is easily the best video I've come across!
  • @pablofueros
    A video like this was much needed in YouTube, thanks so much! I've learn some new things I can now work on
  • @AzinFiro
    For anyone interested in quick and dirty logging for debug purposes, there's also watchpoints, which lets you monitor any variable for changes.
  • @ChrisJones-hv7mo
    This is a great deal better than other Python logging videos and tutorials that I have seen. They might cover the basics, but miss out pretty much entirely about information you need to know when you really want to get into logging with packages, subpackages, and user libraries, Thanks.
  • @SenukiQ
    Great video on standard library logging, but one thing to add is that I can recommend using library called structlog (for structured logs as name suggests), which are very useful especially for bigger applications, where we want to aggregate, store, parse and analyze logs on the system level. Structured logs are way easier to parse, thus analyze, so I think it's nice to have this library in your Python dev toolkit.
  • @Mathymagical
    I appreciate that you emphasized that the built-in logging library is the standard. Since there are some other competitors out there, it can be a bit confusing trying to evaluate whether to use the built-in or not. Thank you.
  • Very very good. The explanation of the mental model is Gold! Finally a video that makes it simpler by showing the whole picture and what to skip instead of just showing the bare minimum which would leave you too ignorant to see both the the forrest and the trees.
  • @CaptainCsaba
    Finally I understand Python logging. I really hope one day they actually create a better logging package. Every project needs logging and it should not be this difficult and outdated, especially when it comes to Python.
  • @69k_gold
    The reason I love this channel and learned a lot from it is, James is one of the few programming content creators that have experience. Him saying "You know what works best for you" is an ironclad example for that
  • @omgwtfafterparty
    When I started working with the logging module it seemed difficult to understand. But actually, after 2-3 hours I admired how powerful yet simple it turns out to be. Really, you plug it in and it just works.