Logging in a File

Logging to a File

As we saw in the previous blog, Python allows us to store our log onto a file. Python offers two ways in which we can do this:

  • Using the ‘filename’ parameter from the basicConfig() function.
  • Using the FileHandler() function.

 

Using basicConfig()

As seen in the previous blog, the ‘filename’ parameter is what we need to use to create log files. Like any File Handling operation, Python checks if the file exists, and if it does not the file will be created in the same directory where the Python script is unless we explicitly specify that the file that is should be created in such and such folder.

import logging

# Creating a specific log format
custom_log_format = "%(levelname)s : %(asctime)s => %(message)s"

logging.basicConfig(
    level=logging.DEBUG,  # show logs from debug level onwards
    filename="logFile.log",  # Creating a .log file
    filemode="w",  # write mode
    format=custom_log_format
)

logging.debug("Log from DEBUG")
logging.info("Log from INFO")
logging.warning("Log from WARNING")
logging.error("Log from ERROR")
logging.critical("Log from CRITICAL")

This would give us:

Here we are saving the program to a .log file, which is a file that is set to be produced automatically and contains a record of events that occur from software to operating systems.

We set the ‘filemode’ as ‘w’ which would write over the existing log records every time the program ran. However, if we want to save the log records from all the different times the program ran, we can use ‘a’ which keeps appending all of the logs to our file.

%(asctime)s returns the time when the log was created in a format that is readable by humans. We can use this to note down exactly when the log was created and we can use this information to better understand our program.

In the example shown below, we ran the program twice to see the 2 different logs.

import logging

# Creating a specific log format
custom_log_format = "Log created on: %(asctime)s\n%(levelname)s: => %(message)s"

logging.basicConfig(
    level=logging.DEBUG,  # show logs from debug level onwards
    filename="logFile.log",  # Creating a .log file
    filemode="a",  # append mode
    format=custom_log_format
)

# Ran this first, log 1 produced
# logging.critical("Critical log message")

# Ran this second, log 2 produced
logging.error("Error occurred here")

The output would be:

The custom log format used here first displays the time when the log was created and then in the next line (as we use \n) it displays the severity level and then the message. Since we opened the file in the append mode, it keeps adding the different logs one by one.

 

Using FileHandler()

The FileHandler() function takes the path of the file as a parameter.

import logging

# Logger Object
logger = logging.getLogger()

# Created a file handler
handler = logging.FileHandler('log-file.log')

# Adding handler to our object
logger.addHandler(handler)

logger.error("Error occurred here, log saved using FileHandler()")

The output would be:

 

We first had to create a logger object; we will explore these classes and functions in detail in the next blog but for now, just remember that we will use it to write logs. Then we created a file handler, which takes the parameter as the name of the log file, and then we added this to our logger object using the addHandler() function.

 

What have we learned?

  • What are two functions we can use to create a log file?
  • What is a .log file?
  • How can we keep adding log records to a log file?
  • What is the significance of using %(asctime)s when formatting our log output?
  • What parameter does the FileHandler() function take?
Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
trackback

canadian pharmacies shipping to usa https://canadianpharmaceuticalsonline.home.blog/

Appreciate it! Lots of forum posts!