Categories: python

Python Logging in a file

In Python, you can use the built-in logging module to print logging in a file. Here are the basic steps to do this:

  1. Import the logging module:
import logging
  1. Create a logging.FileHandler object, and specify the file name and mode in which the file should be opened:
file_handler = logging.FileHandler('example.log', mode='w')
  1. Set the logging level and format for the file handler:
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
  1. Add the file handler to the root logger:
logging.getLogger().addHandler(file_handler)
  1. Use the logging methods to print the logs in the file:
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

This code will create a file named example.log in the current working directory and write the logs in it. Each log message will include the date and time, the logger name, the level of the message, and the message itself.

It’s important to note that when you are done with the logging, you should close the file handler by calling file_handler.close()

You can also use other options to customize the logging, like rotating the file when it reaches a certain size or creating a new file when the log reaches a certain date.

Example:
#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="File1.log", 
     format='%(asctime)s %(message)s', 
     filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("debug message") 
logger.info("information") 
logger.warning("Warning") 
logger.error("error") 
logger.critical("critical") 

The above code will write some messages to a file named File1.log.

Note: also read about Logging basicConfig()function

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago