Geek Logbook

Tech sea log book

Logger In python – First Approach

Besides using the print statements and the debugging tools sometimes (more and more frequently) I’m seeing in the code the logging module. According to the python documentation:

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

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.

https://docs.python.org/3/library/logging.html#module-logging

So you can create a simple logging warning with the following scrip:

import logging
logging.warning("Ey, this is a logging warning")

You’ll see the warning output

WARNING:root:Ey, this is a logging warning

The logging module has different values for the most commons events:

LevelNumeric value
CRITICAL50
ERROR40
WARNING30
INFO20
DEBUG10
NOTSET0
https://docs.python.org/3/library/logging.html#logging-levels

If you are wondering the possibility to add color to your messages you can see this stack overflow question with a lot of comments: How can I color Python logging output?

Sources:

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>