EDIT: Added Ralf's recommendation to use os.environ
Trying to implement a singleton logger so that everytime a logger instance is generated, it'll only write to a specific filename. Until a new filename is specified.
import logging
def singleton(myClass):
instances = {}
def getInstance(*args,**kwargs):
if myClass not in instances:
instances[myClass] = myClass(*args,**kwargs)
return instances[myClass]
return getInstance
@singleton
class Logger(object):
def __init__(self):
self.filename = os.environ['logger_filename']
self.log = self.init_logger()
def init_logger(self):
fh = logging.FileHandler(self.filename + '.log')
fmt = logging.Formatter("%(asctime)-6s: %(name)s - %(levelname)s - %(message)s)")
fh.setFormatter(fmt)
local_logger = logging.getLogger(self.filename)
local_logger.setLevel(logging.DEBUG)
local_logger.addHandler(fh)
return local_logger
What i have above will always write in "test1.log" file.
os.environ['logger_filename'] = "test1"
a = Logger()
b = Logger()
a.log.debug("Hello A")
b.log.debug("Hello B")
os.environ['logger_filename'] = "test2"
c = Logger()
c.log.debug("Hello C")
Since the singleton will contain the Logger class in the instances, thus it will just return that instance and not run the __init__
If I update the singleton with the following:
def singleton(myClass):
instances = {}
def getInstance(*args,**kwargs):
if myClass not in instances:
instances[myClass] = myClass(*args,**kwargs)
else:
instances[myClass].update_logger()
return instances[myClass]
return getInstance
@singleton
class Logger(object):
def __init__(self):
self.filename = os.environ['logger_filename']
self.log = self.init_logger()
def init_logger(self):
fh = logging.FileHandler(self.filename + '.log')
fmt = logging.Formatter("%(asctime)-6s: %(name)s - %(levelname)s - %(message)s)")
fh.setFormatter(fmt)
local_logger = logging.getLogger(self.filename)
local_logger.setLevel(logging.DEBUG)
local_logger.addHandler(fh)
return local_logger
def update_logger(self):
self.filename = os.environ['logger_filename']
self.log = self.init_logger()
os.environ['logger_filename'] = "test1"
a = Logger()
b = Logger()
a.log.debug("Hello A")
b.log.debug("Hello B")
os.environ['logger_filename'] = "test2"
c = Logger()
c.log.debug("Hello C")
This will now generate 2 log files (test1 and test2). But test1 will contain two "Hello A" and two "Hello B". How do i get to fix that?