Reputation: 844
I was creating a set of .py files into a 'package' that I wanted all to import a standardized 'logger' that would allow all modules to log to a single file.
class BaseLogger():
def __init__(self, module_name, specific_task_name):
logging.basicConfig(
filename="app." + specific_task_name + "." + module_name + ".log",
format="%(levelname)-10s;%(asctime)s;%(module)s;%(funcName)s;%(message)s;%(thread)s;%(threadName)s",
#level=logging.DEBUG
)
self.baselogger = logging.getLogger("app." + module_name + "." + specific_task_name )
self.baselogger.setLevel('DEBUG')
So the idea is that all my .py files, import BaseLogger and instantiate that object and it'll find a current logger by same name, therefore LOG to one file...
Problem is i'm now trying to create a rotating log structure and just lost... I've added to .basicConfig [failed to iterable], I'm thinking I should do a self.baselogger.addHandler .. but that makes the log file go empty....
How can I create a rotating log file with my above 'classed' implementation... or have I been doing it wrong?...
Thanks
Upvotes: 0
Views: 730
Reputation: 3705
You do not need a class wrapper as logging module is pretty awesome by design. What you need to do is to register/get a logger and configure it with a format and file handler.
Here is an example with a wrapper:
class BaseLogger(object):
def __init__(self, module_name, specific_task_name):
filename = "app." + specific_task_name + "." + module_name + ".log",
format = ("%(levelname)-10s;%(asctime)s;%(module)s;%(funcName)s;"
"%(message)s;%(thread)s;%(threadName)s"),
# you do not have to keep the reference
# it is possible to get logger from anywhere using getLogger
self.logger = logging.getLogger(specific_task_name)
self.logger.setLevel(logging.INFO)
# make sure to add RotatingFileHandler only once
# you might need to update condition if you have more than 1 handler
if not self.logger.handlers:
fh = handlers.RotatingFileHandler(filename,
mode='a',
maxBytes=MAX_LOG_FILE_SIZE,
backupCount=2,
encoding=None,
delay=0)
fh.setLevel(logging.INFO)
formatter = logging.Formatter(format)
fh.setFormatter(formatter)
self.logger.addHandler(fh)
I would drop a class and go with a fabric
function:
def get_logger(name):
# setup new logger here if it does not exist
# return instance if it is there
pass
Upvotes: 2