Python logging basicConfig














































Python logging basicConfig



Python logging basicConfig


Description: In Computing, a log file is a file that records either events that occur in an operating system, other software runs or message between different users of a communication software. logging is the act of keeping a log.

Many operating systems, software frameworks and programs include a Logging System.

Log Message can contain a variable data which can be date, time or variable's value. logging is an in-built module in Python. It is a flexible framework for emitting log message from python program.


basicConfig: It is a function that takes some keyword arguments. List of parameters are given below. This function does basic configuration by creating a streamhandler(sends logging output to streams such as sys.stdoutsys.stderr or any file-like object) with default formatter and adding it to the root logger.


  1. filename: This kwargs specify that filehandler be created, with the help of this keyword argument after executing basicConfig command log file generate in the same directory.
  2. format: Use the specified format string for the handler.(default value  is '(message)s')
  3. filemodeit can be write, append.default value is 'a'(append)
  4. level: set the root log level to the specified level.(INFO, WARNING, ERROR, DEBUG, CRITICAL,NOTSET)
  5. datefmt: this kwargs will store the date and time in log file as developer wants. %a for day, %d for date, %b for month, %Y for year, %H for hour, %M for minute, %S for second.


Code:
import logging

logging.basicConfig(filename="basic.log",
                    filemode='a',
                    level=logging.INFO,
                    format="%(asctime)s:%(levelname)s:%(message)s",
                    datefmt="%a, %d %b %Y %H:%M:%S "
                    )


After executing this code, basic.log file generated in same directory where python file is saved. 






Comments