From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.77) (envelope-from ) id 1SmoTy-0007dT-2I for garchives@archives.gentoo.org; Thu, 05 Jul 2012 16:00:42 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2A53CE0667; Thu, 5 Jul 2012 16:00:15 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id DD322E0667 for ; Thu, 5 Jul 2012 16:00:14 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 1D2C41B40DD for ; Thu, 5 Jul 2012 16:00:14 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id BFC85E5440 for ; Thu, 5 Jul 2012 16:00:11 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1341503558.f4677f4e6be4007c5cfffd8cdd2563f276b24721.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/recipe/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/recipe/easylogger.py X-VCS-Directories: roverlay/recipe/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: f4677f4e6be4007c5cfffd8cdd2563f276b24721 X-VCS-Branch: master Date: Thu, 5 Jul 2012 16:00:11 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: 58abb050-b104-4693-83b1-e09472e5b45d X-Archives-Hash: 66932c8504940a5bb41ace39c50db1ea commit: f4677f4e6be4007c5cfffd8cdd2563f276b24721 Author: Andr=C3=A9 Erdmann mailerd de> AuthorDate: Thu Jul 5 15:52:38 2012 +0000 Commit: Andr=C3=A9 Erdmann mailerd de> CommitDate: Thu Jul 5 15:52:38 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/R_overlay.git= ;a=3Dcommit;h=3Df4677f4e 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.p= y 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 =20 +import sys import logging +import logging.handlers +import os.path =20 _STATUS =3D 0 =20 +ROOT_LOGGER =3D logging.getLogger() + +DEFAULT_DATE_FORMAT =3D '%F %H:%M:%S' +DEFAULT_STREAM =3D sys.stdout + +def _zap_handlers(): + for h in ROOT_LOGGER.handlers: + ROOT_LOGGER.removeHandler ( h ) +# --- end of _zap_handlers (...) --- + +def setup_initial_console(): + ch =3D logging.StreamHandler ( stream=3DDEFAULT_STREAM ) + ch.setLevel ( logging.WARN ) + + ch.setFormatter ( + logging.Formatter ( + fmt=3D'%(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 =3D conf.get ( 'LOG.CONSOLE.stream', None ) + + if stream is not None: + if stream =3D=3D 'stderr': + stream =3D sys.stderr + elif stream =3D=3D 'stdout': + stream =3D sys.stdout + else: + stream =3D None + + ch =3D logging.StreamHandler ( + stream=3DDEFAULT_STREAM if stream is None else stream + ) + + ch.setLevel ( + conf.get ( + 'LOG.CONSOLE.level', + conf.get ( 'LOG.level', logging.INFO ) + ) + ) + + ch_fmt =3D logging.Formatter ( + fmt=3Dconf.get ( + 'LOG.CONSOLE.format', + '%(levelname)-8s %(name)-14s: %(message)s' + ), + datefmt=3Dconf.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 =3D logging.handlers.SysLogHandler() +# +# lh.setLevel ( +# conf.get ( +# 'LOG.SYSLOG.level', +# conf.get ( 'LOG.level', logging.CRITICAL ) +# ) +# ) +# +# lh_fmt =3D I_DONT_KNOW +# +# lh.setFormatter ( lh_fmt ) +# +# ROOT_LOGGER.addHandler ( lh ) +# + pass +# --- end of setup_syslog (...) --- + +def setup_file ( conf ): + logfile =3D 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 =3D conf.get ( 'LOG.FILE.rotate', False ) + + if rotating: + # using per-run log files + + # rotate after handler creation if log file already exists + rotate_now =3D os.path.exists ( logfile ) + fh =3D logging.handlers.RotatingFileHandler ( + logfile, + backupCount=3Dconf.get ( 'LOG.FILE.rotate_count', 3 ) + ) + if rotate_now: + fh.doRollover() + del rotate_now + else: + # using a big log file + fh =3D logging.FileHandler ( logfile ) + + fh.setLevel ( + conf.get ( + 'LOG.FILE.level', + conf.get ( 'LOG.level', logging.WARN ) + ) + ) + + fh_fmt =3D logging.Formatter ( + fmt=3Dconf.get ( + 'LOG.FILE.format', + '%(asctime)s %(levelname)-8s %(name)-10s: %(message)s' + ), + datefmt=3Dconf.get ( 'LOG.date_format', DEFAULT_DATE_FORMAT ) + ) + + fh.setFormatter ( fh_fmt ) + + if conf.get ( 'LOG.FILE.buffered', True ): + handler =3D logging.handlers.MemoryHandler ( + conf.get ( 'LOG.FILE.buffer_capacity', 250 ), # reasonable value? + target=3Dfh + ) + else: + handler =3D fh + + ROOT_LOGGER.addHandler ( handler ) +# --- end of setup_file (...) --- + + def setup ( conf ): global _STATUS if _STATUS > 1: return =20 - logging.basicConfig ( - level=3Dlogging.DEBUG, - filename=3Dconf.get ( [ 'LOG', 'FILE', 'main' ], 'roverlay.log' ), - filemode=3D'a', - format=3D'%(asctime)s %(levelname)-8s %(name)-14s -- %(message)s', - datefmt=3D'%F %H:%M:%S' - ) + _zap_handlers() =20 - # add console output to the logger - ch =3D 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 ) ) =20 _STATUS =3D 2 =20 @@ -34,6 +168,9 @@ def setup_initial(): if _STATUS > 0: return =20 - pass + _zap_handlers() + logging.lastResort =3D None + logging.raiseExceptions =3D True + setup_initial_console() =20 _STATUS =3D 1