Logging multiple formats to the same file
Rob Gaddi
rgaddi at technologyhighland.invalid
Fri Aug 15 17:38:24 EDT 2014
So I've got my program log going to a RotatingFileHandler (actually a
subclass that ensmartens the umask, but I digress). I'd like to be
able to provide information to the logger that is formatted two
different ways, primarily just so that I can provide a Program Started
message into the log.
What I've got going right now works, but boy it feels like I had to
butcher the intent of the logging module to pull it off. Does anyone
have any better ideas than...
class BannerFormatter(logging.Formatter):
"""
Uses the assigned formatting, unless the name of the logger is
'BANNER', in which case we use the special alternate banner
format. """
def __init__(self, *args, **kwargs):
self.normal = logging.Formatter(*args, **kwargs)
self.banner = logging.Formatter(
'\n\n%(asctime)s - ********** %(message)s **********'
)
def format(self, record):
if record.name == 'BANNER':
return self.banner.format(record)
else:
return self.normal.format(record)
...
logging.getLogger('BANNER').critical('Starting program.')
--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.
More information about the Python-list
mailing list