I will try to resume as much as possible. I have this class I wrote:
Logging class
import logging, logging.handlers.TimedRotatingFileHandler
class Logger(object):
def __init__(self, log_filename):
logging.basicConfig(format='%(asctime)s %(message)s')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
loghandler = TimedRotatingFileHandler(
log_filename, when="midnight", backupCount=50
)
loghandler.setFormatter(formatter)
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
self.logger.addHandler(loghandler)
def getLogger(self):
return self.logger
It works good indeed, now the problem arises when I have a script that uses a Logger instance and within that script I instantiate a class that uses a Logger too, something like this:
Script
import ClassA
A = ClassA()
log = Logger(log_filename='script_logger.log')
logger = log.getLogger()
logger.info('Initiated Script')
while True:
logger.info('Looping')
A.run()
What my class looks like:
ClassA module
class ClassA(object):
def __init__(self):
log = Logger(log_filename='class_logger.log')
self.logger = log.getLogger()
self.logger.info('Started ClassA')
def run(self):
self.logger.info('Into method run')
Now I expect to have 2 separate log files, class_logger.log
and script_logger.log
that works OK, but both files have exactly the same content line by line.
So script_logger.log
and class_logger.log
have the following content:
Started classA
Initiated Script
Looping
Into method run
Looping
Into method run
...
Any clues ?