Logging python to file is a very important and useful tool in a programmer’s toolbox. Logging can help you understand the flow of the program better. And discover new scenarios and methods that you must have not thought about before.

Logging python to file

You can call Logging python to file an extra pair of eyes looking at your program with you. They help check the flow that an application is going through. They also help in storing information like the IP address of the user that accessed the application recently. While running an error occurs, logging can provide a stack trace and tell you the program’s state before it arrives at the code line where the error occurred.

Also, by logging useful data from the right places you can not only debug errors easily. But you can easily use that data to analyze the performance of the application.

You can also find logging as a part of the python library. So, you can easily add logging to your application. Now in this blog, you will learn why using this module is the best way to add logging to your application. Also, how can you get started quickly and you will also get an introduction to some of the advanced features available.

Also Read: Questions on python programming: Top 11

What is the Logging python to file?

Logging python to file

The Logging python to file is a powerful tool that is made to meet the needs of beginners as well as experienced people. Logging is used by most of the third-party Python libraries. This is because you can use it to integrate log messages with the ones from those libraries to produce a homogenous log for your application.

You can easily add logging to your python program. Just type the following program:

import logging

Now after the logging module is imported you can use something called logger. To log messages that you want to see. 5 different standard levels are indicating the severity of events. The different corresponding methods can be used to log different events at the level of severity. Different levels in order of increasing severity are as follows:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

After installing the Logging python to file provides you with a default logger. That helps you to get started without needing to do many configurations. The corresponding methods for each level can be called as shown in the following example:

import logging

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

The output of the above program:

WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message

Now as you can see the output message shows the severity level before each message along with the root. Which is the name of the logging module gives to its default user. As you can see in the format which shows the level, name, and message separated by a colon. The default output can be changed according to your need to include things like linenumber, timestamp, and other details.

Basic configuration of logging:

Also, you can use the basicConfig(**kwargs) method to configure the logging:

Some basic commonly used parameters for basicConfig() are given below:

  • Filename: This parameters is used to specifies the file.
  • Format: Parameter is the format of the log message.
  • Level: The root logger will be set to the specified severity level.
  • Filemode: The file is openend in this mode. This default is a which means append.

By using these logging parameters you can set what level of log messages you want to record. But this part can be easily done by bypassing one of the constants available in the class. You can this by the example given below:

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('This will get logged')
DEBUG:root:This will get logged

Now as you can see in the result all the events at or above the Debug level will now get logged.

Just like this for logging, you can use the file rather than the console, filename, and file mode can be used. And you can also decide the format of the message using the format. We have provided the example given below which will show the usage of all three.

import logging

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logging.warning('This will get logged to a file')
root - ERROR - This will get logged to a file

The following message will look like this but will be written to a file named app.log instead of the console. Then file mode will be named “w” which means the log file is opened in “write mode”. So, each time basicConfig() is called and then each run of the program will rewrite the file. The default configuration for file mode is a which is appended.

You can also customize the root logger even further by easily using more parameters for basicConfig(), which can be found here.

Note: Calling basicConfig() to configure the root logger works that is only if the root logger has not been configured before. So, basically, this function can only be called once.

The basic setting in basicConfig() is to set the logger to write the console in the following format.

ERROR:root:This is an error message

Formatting the Output

There are many variables that you can pass that can be represented as a string from your program as a message to your logs. Also, some basic elements are a part of the LogRecord and can be simply added to the output format. So, if you want to log the process ID along with the level and message, run the command given below.

import logging

logging.basicConfig(format='%(process)d-%(levelname)s-%(message)s')
logging.warning('This is a Warning')
18472-WARNING-This is a Warning

You can use the format can take a string with LogRecord attributes in an arrangement you like. The entire list of available attributes can be found here.

Here’s another example where you can add the date and time info:

import logging

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.info('Admin logged in')
2018-07-11 20:12:06,288 - Admin logged in

%(asctime)s  this function adds the time of the creation of the LogRecord. This format can be changed using the datefmt attribute. This function uses the same formatting language as the formatting functions in the datetime module, that is time.strftime():

import logging

logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
logging.warning('Admin logged out')
12-Jul-18 20:53:19 - Admin logged out

If you want to find the complete guide you can find it here.

Logging python to file Variable Data

People want to include dynamic information in most of the scenarios from your application in the logs. Also, we have seen previously that the logging methods take a string as an argument, and also it might seem natural to format a string with variable data in a separate line and pass it to the log method. But the good part is that it can be done directly by using a format string for the message and appending the variable data as arguments. Let us provide you with an example.

import logging

name = 'John'

logging.error('%s raised an error', name)
ERROR:root:John raised an error

Now the arguments passed to the method would be included as variable data in the message.

Also, while you are using any formatting style, the f-strings introduced in Python 3.6 are an awesome way to format strings as they can help keep the formatting short and easy to read:

import logging

name = 'John'

logging.error(f'{name} raised an error')
ERROR:root:John raised an error

Capturing Stack Traces

The Logging python to file module is considered to be very flexible. The module also allows them to capture the full stack traces in an application. Exception information can be captured if the exc-info parameter is passed as True and then logging functions are called like this:

import logging

a = 5
b = 0

try:
  c = a / b
except Exception as e:
  logging.error("Exception occurred", exc_info=True)
ERROR:root:Exception occurred
Traceback (most recent call last):
  File "exceptions.py", line 6, in <module>
    c = a / b
ZeroDivisionError: division by zero
[Finished in 0.2s]

Conclusion

Now we know that the logging module is considered to be very good and flexible. The design of the logging module and is very practical and will surely fit your use case out of the box. You can add basic logging to a small project or you can also create your own custom log levels, handler classes, and more if you are working on a big project. So, if you haven’t been running logging python to file now is a time to start. Hope you find this information useful. Thank you for the read.

Categorized in:

Tagged in: