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


Groups > comp.lang.python > #76388

Logging multiple formats to the same file

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Rob Gaddi <rgaddi@technologyhighland.invalid>
Newsgroups comp.lang.python
Subject Logging multiple formats to the same file
Date Fri, 15 Aug 2014 14:38:24 -0700
Organization Highland Technology, Inc.
Lines 34
Message-ID <20140815143824.2ac12d57@rg.highlandtechnology.com> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
Injection-Info mx05.eternal-september.org; posting-host="903ac420d4384e8fcf51b0ca3b6abd1b"; logging-data="29605"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/eW/p5aV4lSErrMVEXbXmm"
X-Newsreader Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu)
Cancel-Lock sha1:9OgyO6lQ25EDFcH7TvnPxBwWvzs=
Xref csiph.com comp.lang.python:76388

Show key headers only | 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