0

I'm trying to create a python logger using the logging module. How can I create a multiple loggers to multiple files that the first character will be a record number? using formatter I didn't find how to do that

formatter=logging.Formatter('%(recored_number)s %(asctime)s %(message)s %(name)s')

This is how I wanted the files to be:

First log file:

#1 2019-09-05 08:55:10,000 first logger line for log 1 INFO logger
#2 2019-09-05 08:55:20,000 second logger line for log 1 INFO logger

Second log file:

#1 2019-09-05 08:58:10,000 first logger line for log 2 INFO logger
#2 2019-09-05 08:58:20,000 second logger line for log 2 INFO logger

2 Answers 2

1

Similar behaviour is achieved in this question

The only difference, is that You have to increment the counter upon processing each log record.

You can add a singleton class RecordCounter for this purpose.

import logging

class RecordCounter:
    _instance = None
    _count = 0

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def count(self):
        self._count += 1
        return self._count

class ContextFilter(logging.Filter):
    def filter(self, record):
        record.record_number = RecordCounter().count()
        return True

logging.basicConfig(
    level=logging.DEBUG,
    format='%(record_number)s %(asctime)s %(message)s %(name)s')
logger = logging.getLogger(__name__)
logger.addFilter(ContextFilter())


Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! this is just what i was looking for
Thanks for the previous answer, i would like to create multiple log files so that the counter is per log file . how can i count the log records per log file?
Usually, different logging outputs are handled by logging.Handler instances. You can subclass logging.Handler to make Your own handler, which will add record_number to each record. The singleton is not usable here, then, as You would count the records per instance of handler. Also, the Handler has filter() method, just like the logging.Filter - so You can easily override it to add record_number inside the handler, without the need of using logging.Filter at all.
0

I just found an answer using extra to every log record:

formatter=logging.Formatter('%(recored_number)s %(asctime)s %(message)s %(name)s')

log_1_counter=1
log_2_counter=1
log1.info('first logger line for log 1',extra=
{'record_number':str(log_1_counter)})
log_1_counter+=1

log_1_counter=1
log_2_counter=1
log1.info('first logger line for log 1',extra= 
{'record_number':str(log_1_counter)})
log_1_counter+=1
log1.info('second logger line for log 1',extra= 
{'record_number':str(log_1_counter)})
log2.info('first logger line for log 2',extra= 
{'record_number':str(log_2_counter)})
log_2_counter+=2
log2.info('second logger line for log 2',extra= 
{'record_number':str(log_2_counter)})

This is provably not the best solution but it works for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.