What is logging in Python?

What is logging in Python?

I have been asked by a Senior to understand a function line by line.

My other Senior helped me to meta-approach it yesterday - we talked about how we might approach it, how we might try and get a bigger picture and an overview first, and then how we might try and delve into things we don't understand. Now I tend to go to deeply sometimes.

Sometimes going deep down is a good thing.

This might be one of those instances

Of course it's generally okay to go deep. Just not so deep you get lost in things and don't make progress. My colleague joked that you can't understand your code all the way down to the ones and the zeroes (or maybe he was not joking. I'm not sure). But sometimes you come across a concept that's a good concept to learn, and so then maybe I hope that that's okay to do things.

So what even is logging?

What is logging...

My first concern was to compare it console.log in JavaScript, well after I'd understood it a little bit. It is most certainly similar - the idea of logging output to the console is still there. 

The idea of putting something out to the console for monitoring and debugging your code is still there.

But apparently Python's logging module is more sophisticated

I think it's like a "robust framework". For example, I've noticed in my codebase that it has methods like .write() or .error(). (I wonder what other methods it may have? I noticed a lot when I was scrolling through the docs).

Oh right I see yes. There is also 
  • logging.info()
  • logging.warning() 
  • and logging.critical() 
- amongst others.

So therefore Python's logging module supports different severity levels...

Configurability

Python's logging module also allows you to send logs in different directions...

You can also have third party integration...

From ChatGPT: "It is designed to work with multiple modules and libraries, so you can see logs from your own code as well as third-party logs in a unified way."

Or from the docs:

"This module defines functions and classes which implement a flexible event logging system for applications and libraries."

And then:

"The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules." wow! wow wow wow wow wow wow wow wow wow!!

p.s. I wrote this on the train today but the internet would not support making an infographic so I made it quickly at work.
Red images of red code. Title reads: logging in python. Text reads: Is designed to work with both your won code and third-party integrations Supports different severity levels Helps with monitoring and debugging



Comments