public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/recipe/
Date: Thu,  5 Jul 2012 16:00:11 +0000 (UTC)	[thread overview]
Message-ID: <1341503558.f4677f4e6be4007c5cfffd8cdd2563f276b24721.dywi@gentoo> (raw)

commit:     f4677f4e6be4007c5cfffd8cdd2563f276b24721
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Thu Jul  5 15:52:38 2012 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Thu Jul  5 15:52:38 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=f4677f4e

recipe/easylogger: configurable logging

* log to disk using a FileHandler/RotatingFileHandler,
  optionally attached to a MemoryHandler
* log to syslog (code is incomplete and disabled)
* log to console (either stderr or stdout)

	modified:   roverlay/recipe/easylogger.py

---
 roverlay/recipe/easylogger.py |  167 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 152 insertions(+), 15 deletions(-)

diff --git a/roverlay/recipe/easylogger.py b/roverlay/recipe/easylogger.py
index 112d07b..411397d 100644
--- a/roverlay/recipe/easylogger.py
+++ b/roverlay/recipe/easylogger.py
@@ -2,30 +2,164 @@
 # Copyright 2006-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
+import sys
 import logging
+import logging.handlers
+import os.path
 
 _STATUS = 0
 
+ROOT_LOGGER = logging.getLogger()
+
+DEFAULT_DATE_FORMAT = '%F %H:%M:%S'
+DEFAULT_STREAM = sys.stdout
+
+def _zap_handlers():
+	for h in ROOT_LOGGER.handlers:
+		ROOT_LOGGER.removeHandler ( h )
+# --- end of _zap_handlers (...) ---
+
+def setup_initial_console():
+	ch = logging.StreamHandler ( stream=DEFAULT_STREAM )
+	ch.setLevel ( logging.WARN )
+
+	ch.setFormatter (
+		logging.Formatter (
+			fmt='%(levelname)-7s [%(name)s] %(message)s'
+		)
+	)
+
+	ROOT_LOGGER.addHandler ( ch )
+	ROOT_LOGGER.setLevel ( logging.WARN )
+# --- end of setup_initial_console (...) ---
+
+def setup_console ( conf ):
+	if not conf.get ( 'LOG.CONSOLE.enabled', False ): return
+	stream = conf.get ( 'LOG.CONSOLE.stream', None )
+
+	if stream is not None:
+		if stream == 'stderr':
+			stream = sys.stderr
+		elif stream == 'stdout':
+			stream = sys.stdout
+		else:
+			stream = None
+
+	ch = logging.StreamHandler (
+		stream=DEFAULT_STREAM if stream is None else stream
+	)
+
+	ch.setLevel (
+		conf.get (
+			'LOG.CONSOLE.level',
+			conf.get ( 'LOG.level', logging.INFO )
+		)
+	)
+
+	ch_fmt = logging.Formatter (
+		fmt=conf.get (
+			'LOG.CONSOLE.format',
+			'%(levelname)-8s %(name)-14s: %(message)s'
+		),
+		datefmt=conf.get ( 'LOG.date_format', DEFAULT_DATE_FORMAT )
+	)
+
+	ch.setFormatter ( ch_fmt )
+
+	ROOT_LOGGER.addHandler ( ch )
+# --- end of setup_console (...) ---
+
+def setup_syslog ( conf ):
+#	if not conf.get ( 'LOG.SYSLOG.enabled', False ): return
+#
+#	lh = logging.handlers.SysLogHandler()
+#
+#	lh.setLevel (
+#		conf.get (
+#			'LOG.SYSLOG.level',
+#			conf.get ( 'LOG.level', logging.CRITICAL )
+#		)
+#	)
+#
+#	lh_fmt = I_DONT_KNOW
+#
+#	lh.setFormatter ( lh_fmt )
+#
+#	ROOT_LOGGER.addHandler ( lh )
+#
+	pass
+# --- end of setup_syslog (...) ---
+
+def setup_file ( conf ):
+	logfile = conf.get ( 'LOG.FILE.file' )
+	if not logfile or not ( 'LOG.FILE.enabled', True ): return
+
+	# FIXME explain this in config (and make it available)
+	rotating = conf.get ( 'LOG.FILE.rotate', False )
+
+	if rotating:
+		# using per-run log files
+
+		# rotate after handler creation if log file already exists
+		rotate_now = os.path.exists ( logfile )
+		fh = logging.handlers.RotatingFileHandler (
+			logfile,
+			backupCount=conf.get ( 'LOG.FILE.rotate_count', 3 )
+		)
+		if rotate_now:
+			fh.doRollover()
+		del rotate_now
+	else:
+		# using a big log file
+		fh = logging.FileHandler ( logfile )
+
+	fh.setLevel (
+		conf.get (
+			'LOG.FILE.level',
+			conf.get ( 'LOG.level', logging.WARN )
+		)
+	)
+
+	fh_fmt = logging.Formatter (
+		fmt=conf.get (
+			'LOG.FILE.format',
+			'%(asctime)s %(levelname)-8s %(name)-10s: %(message)s'
+		),
+		datefmt=conf.get ( 'LOG.date_format', DEFAULT_DATE_FORMAT )
+	)
+
+	fh.setFormatter ( fh_fmt )
+
+	if conf.get ( 'LOG.FILE.buffered', True ):
+		handler = logging.handlers.MemoryHandler (
+			conf.get ( 'LOG.FILE.buffer_capacity', 250 ), # reasonable value?
+			target=fh
+		)
+	else:
+		handler = fh
+
+	ROOT_LOGGER.addHandler ( handler )
+# --- end of setup_file (...) ---
+
+
 def setup ( conf ):
 	global _STATUS
 	if _STATUS > 1:
 		return
 
-	logging.basicConfig (
-		level=logging.DEBUG,
-		filename=conf.get ( [ 'LOG', 'FILE', 'main' ], 'roverlay.log' ),
-		filemode='a',
-		format='%(asctime)s %(levelname)-8s %(name)-14s -- %(message)s',
-		datefmt='%F %H:%M:%S'
-	)
+	_zap_handlers()
 
-	# add console output to the logger
-	ch = logging.StreamHandler()
-	ch.setLevel ( logging.DEBUG )
-	ch.setFormatter (
-		logging.Formatter  ( '%(levelname)-8s %(name)-14s -- %(message)s' )
-	)
-	logging.getLogger().addHandler ( ch )
+	if conf.get ( 'LOG.enabled', True ):
+		setup_console ( conf )
+		setup_file ( conf )
+		#setup_syslog ( conf )
+
+
+	if not ROOT_LOGGER.handlers:
+		# logging is disabled, add a nop handler
+		ROOT_LOGGER.addHandler ( logging.NullHandler() )
+
+	ROOT_LOGGER.setLevel ( min ( h.level for h in ROOT_LOGGER.handlers ) )
 
 	_STATUS = 2
 
@@ -34,6 +168,9 @@ def setup_initial():
 	if _STATUS > 0:
 		return
 
-	pass
+	_zap_handlers()
+	logging.lastResort      = None
+	logging.raiseExceptions = True
+	setup_initial_console()
 
 	_STATUS = 1



             reply	other threads:[~2012-07-05 16:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-05 16:00 André Erdmann [this message]
  -- strict thread matches above, loose matches on Subject: below --
2014-02-22 14:56 [gentoo-commits] proj/R_overlay:master commit in: roverlay/recipe/ André Erdmann
2014-02-15 20:11 André Erdmann
2013-11-14 18:24 André Erdmann
2013-09-06 17:27 André Erdmann
2013-07-24  9:54 André Erdmann
2013-07-11 16:29 André Erdmann
2012-07-06 22:19 André Erdmann
2012-06-27 14:46 André Erdmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1341503558.f4677f4e6be4007c5cfffd8cdd2563f276b24721.dywi@gentoo \
    --to=dywi@mailerd.de \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox