Featured Post

Keyword to create root window in python

What is Logging in python

 (10)LOGGING:



Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don't have any logging record and your program crashes, there are very few chances that you detect the cause of the problem. The logging module in Python is a powerful built-in module so you can quickly add logging to your application.

for example:  

import logging

logging.debug('This is a debug message')

logging.info('This is an info message')

logging. warning('This is a warning message')

logging.error('This is a error message')

logging.critical('This is a critical message')

output = WARNING:root: This is a warning message

  ERROR:root: This is an error message

CRITICAL:root: This is a critical message


As we can see only warning, error, and critical are printed because these are defaults so if you want to change this it can be done by setting the basic configuration. To set the basic configuration import logging the type 'logging.basicConfig'then set the level format and default. 

To see more information go to-  https://docs.python.org/3/library/logging.html


1.Logging basic usage:

You can customize your root logger with the 'basicConfig'. The most common parameters are level, format, and filename.

for example:

import logging

logging.basicConfig(level=logging.DEBUGformat=
'%(asctime)s  %(name)s  %(levelname)s  %(message)s'datefmt='%m/%d/%Y %H:%M:%S')

logging.debug('This is a debug message')

logging.info('This is an info message')

logging.warning('This is a warning message')

logging.error('This is a error message')

logging.critical('This is a critical message')

output = 

02/22/2021 08:38:14  root  DEBUG  This is a debug message

02/22/2021 08:38:14  root  INFO  This is an info message

02/22/2021 08:38:14  root  WARNING  This is a warning message

02/22/2021 08:38:14  root  ERROR  This is an error message

02/22/2021 08:38:14  root  CRITICAL  This is a critical message


2.Making your own logger module:

First, make a .py file named helper then import logging module then make a variable named logger then use logging, and then using logging .getLogging(__name__) to name the module as the name of the py file. nd then setting the info given by the module 'logger.info('hello form helper')'


for example:

# helper.py
# -------------------------------------
import logging
logger = logging.getLogger(__name__)
logger.info('HELLO')

# main.py
# -------------------------------------
import logging
logging.basicConfig(level=logging.INFOformat='%(name)s - %(levelname)s - %(message)s')
import helper

# --> Output when running main.py
# helper - INFO - HELLO

output = 02/23/2021 08:04:12 - helper - INFO - hello form helper

helper.py:- 


main.py:- 




3.Propagate(spread and promote):

By default, all created loggers will pass the log events to the handlers of higher loggers, in addition to any handlers attached to the created logger. You can deactivate this by setting propagate = False.
Sometimes if you don't see a log message from another log this may be the reason for it.

for example:


main.py file:

import logging

logging.basicConfig(level=logging.DEBUGformat='%(asctime)s  %(name)s  %(levelname)s  %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')

helper.py:

import logging

logger = logging.getLogger(__name__)

logger.propagate = False

logger.info('hello from helper')

output =


4.Log handler:

Log handlers are responsible for dispatching the appropriate log message to the handler's specific destination. You can use different log handlers to send the message to a standard output stream to files via HTTP or via email. To do this first we create a handler that logs to the stream and a file handler the file handler equals 'logging.FileHandler(file.log')' the 'file.log' is the name of the file, then we set the level and the format. We set the stream to only log messages of warning level and file to error level. With file_h = logging.FileHandler('file.log') you can create a log file in this file you will see the message __main__ - ERROR - this is an error

If you don't know about handlers check out this website:- https://docs.python.org/3/library/logging.handlers.html

for example:

import logging

logger = logging.getLogger(__name__)


stream_h = logging.StreamHandler()
file_h = logging.FileHandler('file.log')

stream_h.setLevel(logging.WARNING)
file_h.setLevel(logging.ERROR)

formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')

stream_h.setFormatter(formatter)
file_h.setFormatter(formatter)

logger.addHandler(stream_h)
logger.addHandler(file_h)

logger.warning('this is a warning')
logger.error('this is an error')

output = __main__ - WARNING - this is a warning

__main__ - ERROR - this is an error


5.File config method:

First, you need to create a '.conf' file specify and it according to your configuration. Define the loggers, handlers, and formatters and provide the names as keys, after their names are defined, they are configured by adding the words logger, handler, and formatter before their names are defines.


.conf file:-


[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s


To use this config file we need to import it, and type 'logger.config.fileConfig('logging.conf')' and to get the logger use 'logging.getLogger('simpleExample').

for example:

import logging 
import logging.config

logging.config.fileConfig('logging.conf')

logger = logging.getLogger('simpleExample')

logger.debug('this is a debug message')

output = 2021-03-1 8:41:30,666 - simpleExample - DEBUG - this is a debug message


6.Capturing traceback message with logger:

To capture the traceback message 'use exc_info'.

for example:

import logging 

try:

    a = [1,2,3]

    val = a[4]

except IndexError as e:

    logging.error(e, exc_info=True)

output = 

ERROR:root: list index out of range

Traceback (most recent call last):

  File "d:/PycharmProjects/learn python by projects/venv/dictation.py", line 40, in <module>

    val = a[4]

IndexError: list index out of rangeT


If you don't know what error will be raised using the traceback module.


for example:

import logging 
import traceback

try:

    a = [1,2,3]

    val = a[4]

except IndexError as e:

    logging.error("The error is %s", traceback.format_exc())

output = 

ERROR:root:The error is Traceback (most recent call last):

  File "d:/PycharmProjects/learn python by projects/venv/dictation.py", line 41, in <module>

    val = a[4]

IndexError: list index out of range


7.Rotating file handler:

To keep track of the most recent log messages of an application.


for example:

import time
import logging
from logging.handlers import TimedRotatingFileHandler
 
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# This will create a new log file every minute, and 5 backup files with a timestamp before overwriting old logs.
handler = TimedRotatingFileHandler('timed_test.log'when='m'interval=1backupCount=5)
logger.addHandler(handler)
 
for i in range(6):
    logger.info('Hello, world!')
    time.sleep(50)


If your application is running for a long time you can use the 'TimedRotatingFileHandler'.this will create a rotating log base,

on how much time has passed. it can be set with the letters 's,m,h,d, midnight,w0,w1...' where s is seconds m is minutes 'h' is hours

d is days and w0 is Monday and you can set the other days of the week with the w and writing the number of the day as a number in front of 

the w.for example: w1 = Tuesday

the following code will create 5 log files having the text Hello, world!


for example:

import logging
import time
from logging.handlers import TimedRotatingFileHandler
 
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# This will create a new log file every minute, and 5 backup files with a timestamp 
before overwriting old logs.
handler = TimedRotatingFileHandler('timed_test.log'when='s'interval=1
backupCount=5)
logger.addHandler(handler)
 
for i in range(6):
    logger.info('Hello, world!')
    time.sleep(1)


Comments