📜 ⬆️ ⬇️

Logging - a library for easy logging in Python

In any development, it is necessary to keep logs sooner or later, because you don’t give the customer a program where debug messages are output using print, and later on if something goes wrong with the customer, you can simply ask to show the log and understand what the problem is (most cases), so in python there is a very powerful and convenient library and then I will try to tell about it.

First, let's connect the library and try to display messages of different types on the console:

#log.py #!/usr/bin/python # -*- coding: utf-8 -*- import logging #   logging.debug( u'This is a debug message' ) #   logging.info( u'This is an info message' ) #   logging.warning( u'This is a warning' ) #   logging.error( u'This is an error message' ) #   logging.critical( u'FATAL!!!' ) 


With output, we get:
')
 WARNING:root:This is a warning ERROR:root:This is an error message CRITICAL:root:FATAL!!! 


As we can see, during the output we get everything except debugging and informational messages, so that we can see them, you need to transfer the level of displayed errors to the settings of the logger:

 logging.basicConfig(level = logging.DEBUG) 


I just want to make a reservation that the configuration parameters should be transmitted before the first call of the error output, here is the code:

 #log.py #!/usr/bin/python # -*- coding: utf-8 -*- import logging logging.basicConfig(level = logging.DEBUG) #   logging.debug( u'This is a debug message' ) #   logging.info( u'This is an info message' ) #   logging.warning( u'This is a warning' ) #   logging.error( u'This is an error message' ) #   logging.critical( u'FATAL!!!' ) 


Now after the output we get all the messages:

 DEBUG:root:This is a debug message INFO:root:This is an info message WARNING:root:This is a warning ERROR:root:This is an error message CRITICAL:root:FATAL!!! 


But all the same, the conclusion from it looks scarce looks like a little something you can understand, for this you can also set the format of the displayed message in the logger config, you can see all the attributes ( here ):

 logging.basicConfig(format = u'%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s', level = logging.DEBUG) 


now after the output we get the following messages:
 log.py[LINE:33]# DEBUG [2012-05-25 00:11:58,466] This is a debug message log.py[LINE:34]# INFO [2012-05-25 00:11:58,466] This is an info message log.py[LINE:35]# WARNING [2012-05-25 00:11:58,466] This is a warning log.py[LINE:36]# ERROR [2012-05-25 00:11:58,467] This is an error message log.py[LINE:37]# CRITICAL [2012-05-25 00:11:58,467] FATAL!!! 


Already something more like logs and you can see the name of the file and the line in which the message is called, but outputting them while working is good when development is in progress, but when you submit a project, it’s better to write them to a file, so let's write them there, for that also in the configuration has a parameter to indicate where we will write the log:
 logging.basicConfig(format = u'%(levelname)-8s [%(asctime)s] %(message)s', level = logging.DEBUG, filename = u'mylog.log') 


now logs will be written to the file specified in the filename parameter, in fact, this logger has a lot of opportunities, I showed only those that are needed to start working with the logger. More detailed information can be found on the off. docs.python.org/library/logging.html

Source: https://habr.com/ru/post/144566/


All Articles