Welcome to a tutorial on Logging Basic configurations in Python. The basic config(**kwargs) method where **kwargs in the function definition means that the function takes variable length arguments, that are passed in the key-value form.
In addition, in the basicConfig() function, the level parameter can either be provided with an appropriate value, filename, and filecode for printing logs in the file and the format parameter to specify the logs, or also, all the parameters can be specified. This is the uniqueness of the **kwargs, which just means the number of arguments that can be supplied to a function is not fixed.
import logging
logging.basicConfig(level=logging.INFO)
logging.info('This is an info message.This will get logged.')
Output:
INFO:root: This is an info message. This will get logged.
From above, every event at or above INFO level will be logged.
Also, the basicConfig() method can be used to store logs in a file. You will learn how to do this in the next tutorial.
The logging module can be configured to print logs in any format. In python, there are standard formats that are used universally.
In addition, we have some basic components of logs that are already a part of the LogRecord and we can add them or remove them easily from the output format if necessary.
For this, if we want to include the process ID for the running Python script along with the log level and message, the code snippet is given below.
import logging
logging.basicConfig(format='%(process)d-%(levelname)s-%(message)s')
logging.warning('This message is a warning')
Output:
1396-WARNING-This message is a warning
From the above example, in the method basicConfig(), we set the format parameter with the format string as a value in which we have specified, and the components we want in our log, along with specifying its datatype, such as d with process to print the integer process id, and then s for loglevel which happens to be a string value and same for the message.
In addition, we can as well set the format using the LogRecord attributes set in any order in our example above.
The date and time info (timestamp) can be added to your logs along with the log message. Check the example below:
import logging
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.info('This message is to indicate that Admin has just logged in')
Output:
2020-06-19 11:43:59,887 - This message is to indicate that Admin has just logged in
From the example above, the code %(asctime)s typically adds the time of the creation of the LogRecord. the code level=logging.INFO was used to configure the log level.
Interestinly, when using the datefmt attribute, we can change the format, which uses the same formatting language as the formatting functions in the datetime module, like time.strftime(). Check out the example below.
import logging
logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
logging.warning('The Admin just logged out')
Output:
19-Jun-20 11:50:28 - The Admin just logged out
From the example above, we saw that the log printed as the output of the above code has a date format DD-MM-YY. Alongside with the time.
Finally on this, by making use of the basicConfig(**kwargs) method, we can configure the logs format as we require.