It is sometimes tempting to log less in order to let's say improve performance or save disk space, and blah blah blah!
Loggers in python are the last thing to think about when you think about performance. You can place log ratote to use less disk space. From my own experience log as much as you can. Log as every step necessary to make log level verbose. At last in case you think you application is very stable you change log level to info, and all debug logs are ignored.
It took sometimes hours to debug a few modules (in microservice structure) to find the culprit, something that could have been solved by putting more logs.
Log, log and log more...
#python #log #logger #performance
Loggers in python are the last thing to think about when you think about performance. You can place log ratote to use less disk space. From my own experience log as much as you can. Log as every step necessary to make log level verbose. At last in case you think you application is very stable you change log level to info, and all debug logs are ignored.
It took sometimes hours to debug a few modules (in microservice structure) to find the culprit, something that could have been solved by putting more logs.
Log, log and log more...
#python #log #logger #performance
How to add color to your logs in
It's easy as pie, just install
By default the
If you don't want to see log messages from libraries, you can pass a specific logger object to the
#log #logger #coloredlogs #logging #color
python
?It's easy as pie, just install
coloredlogs
with pip and then:import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG')
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")
By default the
install()
function installs a handler on the root logger, this means that log messages from your code and log messages from the libraries that you use will all show up on the terminal.If you don't want to see log messages from libraries, you can pass a specific logger object to the
install()
function. In this case only log messages originating from that logger will show up on the terminal:coloredlogs.install(level='DEBUG', logger=logger)
#log #logger #coloredlogs #logging #color
Do you log a lot like me in your
You can see that both have the same log content but it's hard to follow
The formatter is as below:
Now the output will something like below:
You can see that log content is so much easier to follow by using space padding. It may not be obvious on telegram with small devices. So try it your self :)))
#python #logging #log #logger #formatter #log_formatter #space_padding #padding
Python
modules? If so, you had the same problem to always find the first occurence of a log after time, filename, etc. Let's clarify this with a sample log:[2012-10-02 application.py:1 _get()] DEBUG: this is a log content
[2012-10-02 db.py:1005 _fetch_all_info()] INFO: this is a log content
You can see that both have the same log content but it's hard to follow
cause of length of file name, line number and function name. To format this better we can have space padding in formatters. spaces are identified by `s
. Now lets see the same log, but this time with space padding.The formatter is as below:
[%(asctime)s %(filename)15s:%(lineno)4s %(funcName)20s()] %(levelname)s %(message)s
NOTE: this is not the exact formatter for the above log, it is for demonstration!
Now the output will something like below:
[2012-10-02 application.py: 1 _get()] DEBUG: this is a log content
[2012-10-02 db.py: 1005 _fetch_all_info()] DEBUG: this is a log content
You can see that log content is so much easier to follow by using space padding. It may not be obvious on telegram with small devices. So try it your self :)))
#python #logging #log #logger #formatter #log_formatter #space_padding #padding