Python Logging



One of the lesser known Python Libraries. Teaches you the concept of creating system logs to track your program activities and events that occur during execution. All these are saved to a single file, which we call, a “log” file. This file can be used for troubleshooting or debugging later on. We call this process in python, logging.

Why use the Python Logging Library?

If you’ve begun developing your own scripts by now, there’s a good chance that you’ve been using print statements during your code to see how the variables are changing as the code executes.

Unfortunately, that’s not the right way to be doing it. It works just fine on a basic level, but once things get complicated, this approach will eventually fail. This is where the Python Logging Library comes in, providing a more proper approach to logging system events.

One of the many benefits of the logging system is that you don’t have to go around deleting all the print statements that you wrote. Furthermore, you get a saved copy of the all system events (if implemented properly) that you can view later, unlike the print statement, which only lasts for a single execution. The logging functions can also log more than just text and numbers. You’ll see below.


Level System

Sorted from lowest threat to severest.

  • Debug : These are used to give information regarding the program while it’s running. Useful for debugging in the case of any error, or unexpected outcome.
  • Info : These are used to Confirm that things are working as expected
  • Warning : These are used to indicate that something unexpected has happened, or an error is likely to be produced due to certain events.
  • Error : Used to indicate a problem in the code. Usually thrown when the program has become unable to carry out some commands.
  • Critical : Highest level error. Usually severe enough to prevent the program from continuing any further.

Configuring

To actually begin the logging process, we have to configure the logger by giving it some basic information.

logging.basicConfig(filename="logfile.txt", 
                    format='%(asctime)s %(message)s', 
                    filemode='w',
                    level=logging.DEBUG) 
  1. Filename: The name of the file where the log is to be written. Since no path was specified, it will save itself in the same directory as the python script.
  2. format: Two parameters have been passed, time and message. This will write to the log, the time the event occurred and the contents of the error message. %(levelname)s is another such parameter you may find useful to include. List of all such parameters can be found in the docs.
  3. Filemode: The mode in which the log file is to be written to. You can either pick w for write, or a for append. w will rewrite the file from scratch when running the program again, whereas a will simply add to it.
  4. level: You can see set the threshold level here. If you set the threshold level to Warning, only system events of severity greater or equal to Warning will be written to the log. In other words, Debug and info level events will not be written to log. In example above, we have set it to Debug level, hence it writes everything.

Note: The level is an optional parameter. You can set it later on using the setlevel() Function.

logger.setLevel(logging.DEBUG) 

Using Python Logging

A full example demonstrating the use of Logging in a Python Script.

import logging

logging.basicConfig(filename="logfile.txt", 
                    format='%(asctime)s - %(levelname)s - %(message)s', 
                    filemode='w',
                    level = logging.Debug )

#Creating a logging object
logger = logging.getLogger() 

#Samples
logger.debug("Information that might help in debugging") 
logger.info("For your information") 
logger.warning("Warning, this may cause problems later") 
logger.error("Error, possibly missing a library") 
logger.critical("Missing Syntax, crashing code") 

OUTPUT (In the log file)

2020-03-13 11:45:52,835 - DEBUG - Information that might help in debugging
2020-03-13 11:45:52,844 - INFO - For your information
2020-03-13 11:45:52,845 - WARNING - Warning, this may cause problems later
2020-03-13 11:45:52,845 - ERROR - Error, possibly missing a library
2020-03-13 11:45:52,845 - CRITICAL - Missing Syntax, crashing code
  • logger.debug – Outputs a message of level Debug.
  • logger.info – Outputs a message of warning level Info.
  • logger.debug – Outputs a message of level Warning.
  • logger.debug – Outputs a message of level Error.
  • logger.debug – Outputs a message of level Critical.

Logging Exceptions

You can use the logging module to capture the full stack trace of an exception.

import logging

logging.basicConfig(filename="logfile.txt", 
                    format='%(asctime)s - %(levelname)s - %(message)s', 
                    filemode='w',
                    level = logging.DEBUG)

logger = logging.getLogger()

try:
    result = 5 / 0
except:
    logging.error("Exception thrown", exc_info = True)

OUTPUT (In the log file)

2020-03-13 12:07:11,045 - ERROR - Exception thrown
Traceback (most recent call last):
  File "C:\Users\Shado\Desktop\llala.py", line 15, in <module>
    result = 5 / 0
ZeroDivisionError: division by zero

As you can see, when the zero error was caused and the exception was thrown, the full stack trace was captured and written to the log file. This is particularly helpful when it comes to debugging your program.


Any suggestions or corrections are more than welcome. Any questions can be asked below in the comments. Anything to help improve our site, CodersLegacy is much appreciated.

Leave a Reply

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