When a program runs, It undertakes various tasks and events to produce the required outcome. Messages, data, error codes, or a log can be used to document the steps and events it goes through to achieve a result. This is where Python logging comes into play.
Therefore, logging may be defined as the systematic recording of a program’s activity.
Why do we need logging?
There are many different reasons for developers to maintain logs of their program. For instance, they could be monitoring user traffic, or at what point user traffic is high on their application, any communication between servers, or they could be logging all the instances where their application faced an error which a developer could later refer to while debugging.
How is logging implemented in Python?
As of the latest Python 3.10, it comes with a built-in module called logging. It’s part of the Python Standard Module under the Generic Operating System Services category.
This module, which the name itself describes its purpose, comes with various attributes for creating logging systems. You can try the following commands right here on your browser here.
import logging
Exploring basiconfig()
One of the essential methods in this module is basicConfig(), which allows us to implement a configuration according to our needs. These are the basic parameters that we will need to configure for logging basicconfig:
- The level of logging which we require, out of the 5. They include:
- Debug(10) – Used for determining the issue in the code.
- Info(20) – Provides a message into the log of any sort, a use-case scenario would be to indicate the successful completion of a task or event.
- Warning(30) – This can indicate any abnormal functioning of a program which may cause more complications down the road if not taken care of.
- Error(40) – Indicates a bug in the code, examples include memory loss, syntax errors, and other common exceptions.
- Critical(50) – This can cause the entire program to cease its functions and exit unexpectedly.
- The file, that will contain the logs.
- To allow the storing of new logs by overwriting the older logs.
- File format of stored logs.
Writing Logging Levels in Python
The following code represents the various logging levels in Python basicconfig()
import logging
logging.debug(“Severity Level 10 message”)
logging.info(“Severity Level 20 message”)
logging.warning(“Severity Level 30 message”)
logging.error(“Severity Level 40 message”)
logging.critical(“Severity Level 50 message”)
Output
DEBUG:root:Severity Level 10 message INFO:root:Severity Level 20 message WARNING:root:Severity Level 30 message ERROR:root:Severity Level 40 message CRITICAL:root:Severity Level 50 message
Setting Severity Levels
By default, Python only logs severity levels above 20.
import logging
logging.basicConfig(level=logging.INFO)
Severity levels over ten are logged.
Saving Generated Logs in Log Files
import logging
logging.basicConfig(level=logging.INFO, filename=‘sample.log’)
We’ve ensured that generated logs will be stored in “sample.log”. After saving, you’ll find it in the directory where it was saved.
Log File Access
The module sets the log file in “append” mode by default. This will result in multiple copies of the same log. Setting the file in write mode can solve this problem.
import logging
logging.basicConfig(level=logging.INFO, filename=‘sample.log’, filemode = ‘w’)
In case of repeated logs, the program will just overwrite.
Adding Timestamps to your Logs
Another powerful tool in logging is the ability to add time-stamps. You can do this by making use of the asctime
attribute. This mentions the date and time of log generation.
Since we are using various attributes, we are using the format
and message
methods to avoid writing clunky code.
import logging
logging.basicConfig(level=logging.INFO, format=’date+time:%(asctime)s | %(message)s’)
Sample Output
date+time: 2022-03-14 06:49:08,234 | Severity Level 20 message
Other Attributes of basicconfig()
The above examples did not include these parameters. Try on your own and implement these into the above lines right here in your browser using our Online Python interpreter.
- style – If the
format
attribute is specified, we can usestyle
to format string types.logging.basicConfig(level=logging.INFO,style = '{}', format=’date+time:%(asctime)s | %(message)s’)
- stream – Initializes an object of class
logging.StreamHandler
. This instance logs output. However, this cannot be used withfilename
as it will result inValueError
exception.logging.basicConfig(stream, level=logging.INFO,style = '{}', format=’date+time:%(asctime)s | %(message)s’, )
- handlers – Used as an iterable if you’ve created handlers. Similar to stream, it cannot be used with filename due to raising of
ValueError
exception.logging.basicConfig(stream, level=logging.INFO,style = '{}', format=’date+time:%(asctime)s | %(message)s’,handler = sampleHandler )
- force – Setting this to
True
will result in all the handlers for the system to be closed and removed. before satisfying other parameters.logging.basicConfig(stream, level=logging.INFO,style = '{}', format=’date+time:%(asctime)s | %(message)s’,force = 'True' )
- encoding – Allows encoding for the log-file. Used with
filename
.logging.basicConfig(level=logging.INFO, filename=‘sample.log’, filemode = ‘w’, encoding = 'utf-8')
- errors – Used with filename to handle errors while opening log files. Class
FileHandler
uses its value. If there’s an issue with the log file it will call open() in order to open it.logging.basicConfig(level=logging.INFO, filename=‘sample.log’, filemode = ‘w’, encoding = 'utf-8', errors = errorHandler)
Using Python logging basicconfig method to write logs to Console and Create a file.
The following code will demonstrate how to display the logs on a console and save it in a log file using logging basicconfig.
import logging
# Setting up basicconfig attributes file name, information format, and severity
logging.basicConfig(
filename='log_file_name.log',
level=logging.INFO,
format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
datefmt='%H:%M:%S'
)
# Setting up the function to display on console
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# Formatting the output information for better readability
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
How we add handlers for basicconfig()?
As we saw, basicconfig has attributes to support handlers. Refer to the below demonstration:
import logging
#creating an instance of stream handler
sampleHandler = logging.StreamHandler()
#setting severity level
sampleHandler.setLevel(logging.ERROR)
# passing sampleHandler object into handler attribute
logging.basicConfig(stream, level=logging.INFO,style = '{}', format=’date+time:%(asctime)s | %(message)s’,handler = sampleHandler )
Common Errors from basicconfig()
logging.basicconfig no such file or directory
Not mentioning the working directory explicitly may cause this issue.
logging.basicConfig(level=logging.DEBUG, filename="example2.log", format='%(asctime)s %(message)s', handlers=[logging.StreamHandler()])
You might want to provide a complete system address to the filename parameter.
logging.basicConfig(level=logging.DEBUG, filename="C:\Users\Admin\Desktop\example2.log", format='%(asctime)s %(message)s', handlers=[logging.StreamHandler()])
Python IDEs may create working directories outside your project folder. This can get hectic if you are managing multiple log files. Thus, setting the working directory to your project file is advisable.
FAQs on Logging basicconfig
Even though logging is similar to print and file writing functions, it includes lots of additional information, as we discussed, in its log files. Logging has powerful capabilities, for example, pinpointing the exact line of code which failed to execute. This saves enormous amounts of time for developers.
The log files generated by this function appear in the Python working directory on your system. To find the current working directory in Python, use os.getcwd()
. To change the current working directory, use os. chdir(yourpathname)
.
Conclusion
Using logging basicconfig, you can experiment with other attributes to see how they affect your logs. Following these few demonstrations, you can create your versatile logging systems.