0

I working on python code to add contextual information to log dynamically based on subsequent method. Below is the code

import logging

class AppFilter(logging.Filter):

    def __init__(self,app_name):
        self.app_name=app_name

    def filter(self, record):
        record.app_name = self.app_name
        return True


def custom_log(app):

    logger = logging.getLogger(__name__)

    logger.addFilter(AppFilter(app))

    syslog = logging.StreamHandler()

    formatter = logging.Formatter('%(asctime)s %(app_name)s : %(message)s')

    syslog.setFormatter(formatter)

    logger.setLevel(logging.INFO)

    logger.addHandler(syslog)

    return logger

def stuckTran(app):

    logger=custom_log(app)

    logger.info('The sky is so blue')

    logger.info('hi')

list1=sys.argv[1:]

for a in list1:

    stuckTran(a)

Below is the output:

2019-06-07 08:28:30,761 test1 : The sky is so blue

2019-06-07 08:28:30,761 test1 : hi

2019-06-07 08:28:30,761 test2 : The sky is so blue

2019-06-07 08:28:30,761 test2 : The sky is so blue

2019-06-07 08:28:30,762 test2 : hi

2019-06-07 08:28:30,762 test2 : hi

2019-06-07 08:28:30,762 test3 : The sky is so blue

2019-06-07 08:28:30,762 test3 : The sky is so blue

2019-06-07 08:28:30,762 test3 : The sky is so blue

2019-06-07 08:28:30,762 test3 : hi

2019-06-07 08:28:30,762 test3 : hi

2019-06-07 08:28:30,762 test3 : hi

Issue here is log info is getting printed multiple times. I think issue is with the way I am calling custom_log method.

I have been through https://docs.python.org/2/howto/logging-cookbook.html#using-filters-to-impart-contextual-information. Still I got stuck

I am expecting output like below python logtest.py test1 test2 test2

2019-06-07 08:28:30,761 test1 : The sky is so blue

2019-06-07 08:28:30,761 test1 : hi

2019-06-07 08:28:30,761 test2 : The sky is so blue

2019-06-07 08:28:30,762 test2 : hi

2019-06-07 08:28:30,762 test3 : The sky is so blue

2019-06-07 08:28:30,762 test3 : hi

I want to add different context to logs based on functions parameter. Please help.

sudhir
  • 219
  • 5
  • 17

1 Answers1

1

logging is a singleton class i.e you can have only one instance at a time. So every time custom_log() is called, you are not creating a new instance rather you are adding handlers to the logging object. To fix this, you can clear existing handlers. The modified custom_log function would be:

def custom_log(app):

    logger = logging.getLogger(__name__)

    logger.handlers.clear()

    logger.addFilter(AppFilter(app))

    syslog = logging.StreamHandler()

    formatter = logging.Formatter('%(asctime)s %(app_name)s : %(message)s')

    syslog.setFormatter(formatter)

    logger.setLevel(logging.INFO)

    logger.addHandler(syslog)

    return logger
Vishnu Dasu
  • 533
  • 5
  • 18