18 posts
Python logging is a standard library and can be used in many ways, in this article I am focusing on below things.
While we start implementing our python modules we will use print()
to check its working as expected. In the middle of our scripts also we will add some print statement for debugging and all.
But if we start thinking on a large application point of view with multiple levels of modules its difficult to maintain or understand print statements output. So python provides a standard library called logging
.
Using that logging module we can easily manage all levels of logs for an enterprise/individual applications.
From now on start using logging
modules in place of print
statements to get used to it. Below is code sample is very basic example of logging library usage.
import logging
logging.debug('Hi, This is just a debug msg')
logging.info('I am the info msg')
logging.warning('Something may be wrong')
logging.error('Oops!!! , You got an error')
logging.critical('Your application broken')
If you run the above example you will get the following output
WARNING:root:Something may be wrong
ERROR:root:Oops!!! , You got an error
CRITICAL:root:Your application broken
Python logging library provides 5 default levels of severities DEBUG, INFO, WARNING, ERROR and CRITICAL. Based on your events you can use those functions on logger object.
Notice that first DEBUG and INFO messages are not logged. By default logger severity level set to WARNING, so it will log the messages with minimum severity WARNING.
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('Hi, This is just a debug msg')
logging.info('I am the info msg')
logging.warning('Something may be wrong')
and you will get the following output with DEBUG and INFO messages also.
DEBUG:root:Hi, This is just a debug msg
INFO:root:I am the info msg
WARNING:root:Something may be wrong
We can observer that format of the log message is severity level, name of the logger(root is default) and message. So for any customization like logging format we have to configure ourself.
If we want to write all our logs to one file we can do that like below.
import logging
#configs
logging.basicConfig(level=logging.DEBUG, filename="mylog.log", filemode="w", format='%(levelname)s - %(name)s - %(message)s')
logging.debug('Hi, This is just a debug msg')
logging.info('I am the info msg')
logging.warning('Something may be wrong')
logging.error('Oops!!! , You got an error')
logging.critical('Your application broken')
You can observe that mylog.log
file is created with new format specified with dashes(-
).
Default filemode will be a
(append) , if we don't specify w
it will append the logs to same file.
we can also use variables in log messages by formatting message string, also we can add date time when that event occurred.
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
my_v = "In main logic"
logging.warning('Something may be wrong %s' % my_v)
Output
019-09-16 12:18:22,473 - WARNING - Something may be wrong In main logic
We can also format the datetime string logged by giving datefmt and it will follow datetime strftime
formatting.
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%m-%y %H:%M:%S')
Also if we want to log all trace when some exception occurs we can use logging.exception
function which will add complete traceback also.
You can find the same sample here.
These are all the basic usage of logging library, with this you can also create separate log files for different severity levels based on your application.
You can also write your custom levels.
You can also have multiple logger objects for your application to log individual process logs.
Keep exploring and comment here if you found anything interesting with python logging.
Please log in to leave a comment.
18 posts