How To Log In Python, A Simple Tutorial

In this tutorial, you will learn how to create a simple python script that makes use of the logging python standard library in order to write log messages to a file. The logging module provides a niceĀ API to the python developer. It can be used to reportĀ errors and status information from applications and libraries.

The code writer can easily log messages with different verbosity levels. It is possible not only to log messages from an application to local files, butĀ HTTP GET/POST locations,Ā Ā generic sockets and also email them via SMTP.

What is logging? In simple words logging means tracking eventsĀ that happen when some software runs. This is very helpful to the software developer because it helps to indicate if specific events occurred. According to the official python documentationĀ Ā logging provides a set of convenience functions for simple usage such as debug(), info(), warning(), error() and critical().

What about the print() function, can it be used for logging? Ā It is the best toolĀ display console output for ordinary usage of a command line script or program.

Now open the python interpreter so we can start practising some logging examples and learn the right tools for performing logging actions.

You know how to import a module in python, right? The import command is used to import the module so we can use it for our tasks. In the following example the logging module is imported.

import logging

Then we make use of the warning() function inside the logging module. logging.warning() is used toĀ report an error regarding a particular runtime event for which the application can do nothing, but it is necessary for the event to be noted.

logging.warning('Be Careful')

The above will printĀ WARNING:root:Hey on your console. Now try logging.info().

logging.info()

What do you see? Nothing? This function is used toĀ confirm that things are working as expected. And if you want to report an error in your application logging.error() is the best tool to use.

logging.error('Error')

After running the above function the console will printĀ ERROR:root:Error.

Now that you have some basic knowledge about logging module and its functions it is time to learn to how to log to a file. Make sure to start a new python interpreter before typing any other command.

1. Import te logging module

import logging

2. Then use the following line to do some basic configurations. It basically sets up a .log file where all the messages will be stored.

logging.basicConfig(filename='unixmen.log', level=logging.DEBUG)

3. Now you can use each of the logging functions (debug(), info(), warning(), error() and critical()) to log to a file.

logging.info('Info')

4. Open the unixmen.log file. What do you see? In your unixmen.log file you should see the following.

INFO:root:Info

Type the logging.info(‘Info’) and run it again. Open unixmen.log file again. Now you should see that the log file is populated with another line of text.

INFO:root:Info
INFO:root:Info

Run the following command in your python interpreter.

logging.debug(‘Using the debug function’)

The unixmen.log file will now contain the following.

INFO:root:Info
INFO:root:Info
DEBUG:root:Using the debug function

Not the name of the function being used for logging at the beginning of every log. And last but not least the level keyword inside basicConfig is being used toĀ set the logging level which acts as the threshold for tracking.