Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76388

Logging multiple formats to the same file

From Rob Gaddi <rgaddi@technologyhighland.invalid>
Newsgroups comp.lang.python
Subject Logging multiple formats to the same file
Date 2014-08-15 14:38 -0700
Organization Highland Technology, Inc.
Message-ID <20140815143824.2ac12d57@rg.highlandtechnology.com> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Logging multiple formats to the same file Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-08-15 14:38 -0700

csiph-web