diff options
author | Kali Kaneko <kali@futeisha.org> | 2014-07-02 11:38:16 -0500 |
---|---|---|
committer | Kali Kaneko <kali@futeisha.org> | 2014-07-02 11:38:16 -0500 |
commit | 073dc931282a1389dcddb713f133d8504310ae9f (patch) | |
tree | 241d30509e30704431ddd378883e477eaa225a07 /gnupg/_logger.py | |
parent | bc02d9b78a274f3d2a73c63473bc6b28cfcc5f8d (diff) | |
parent | d1955bd267a132c24d9e64dde7a1cdb8bd9fe9c5 (diff) |
Merge tag 'upstream/1.2.6'
Upstream version 1.2.6
Diffstat (limited to 'gnupg/_logger.py')
-rw-r--r-- | gnupg/_logger.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/gnupg/_logger.py b/gnupg/_logger.py new file mode 100644 index 0000000..870617e --- /dev/null +++ b/gnupg/_logger.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +# +# This file is part of python-gnupg, a Python interface to GnuPG. +# Copyright © 2013 Isis Lovecruft, <isis@leap.se> 0xA3ADB67A2CDB8B35 +# © 2013 Andrej B. +# © 2013 LEAP Encryption Access Project +# © 2008-2012 Vinay Sajip +# © 2005 Steve Traugott +# © 2004 A.M. Kuchling +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the included LICENSE file for details. + +'''Logging module for python-gnupg.''' + +from __future__ import absolute_import +from __future__ import print_function +from datetime import datetime +from functools import wraps + +import logging +import sys +import os + +try: + from logging import NullHandler +except: + class NullHandler(logging.Handler): + def handle(self, record): + pass + +from . import _ansistrm + +GNUPG_STATUS_LEVEL = 9 + +def status(self, message, *args, **kwargs): + """LogRecord for GnuPG internal status messages.""" + if self.isEnabledFor(GNUPG_STATUS_LEVEL): + self._log(GNUPG_STATUS_LEVEL, message, args, **kwargs) + +@wraps(logging.Logger) +def create_logger(level=logging.NOTSET): + """Create a logger for python-gnupg at a specific message level. + + :type level: :obj:`int` or :obj:`str` + :param level: A string or an integer for the lowest level to include in + logs. + + **Available levels:** + + ==== ======== ======================================== + int str description + ==== ======== ======================================== + 0 NOTSET Disable all logging. + 9 GNUPG Log GnuPG's internal status messages. + 10 DEBUG Log module level debuging messages. + 20 INFO Normal user-level messages. + 30 WARN Warning messages. + 40 ERROR Error messages and tracebacks. + 50 CRITICAL Unhandled exceptions and tracebacks. + ==== ======== ======================================== + """ + _test = os.path.join(os.path.join(os.getcwd(), 'gnupg'), 'test') + _now = datetime.now().strftime("%Y-%m-%d_%H%M%S") + _fn = os.path.join(_test, "%s_test_gnupg.log" % _now) + _fmt = "%(relativeCreated)-4d L%(lineno)-4d:%(funcName)-18.18s %(levelname)-7.7s %(message)s" + + ## Add the GNUPG_STATUS_LEVEL LogRecord to all Loggers in the module: + logging.addLevelName(GNUPG_STATUS_LEVEL, "GNUPG") + logging.Logger.status = status + + if level > logging.NOTSET: + logging.basicConfig(level=level, filename=_fn, + filemode="a", format=_fmt) + logging.logThreads = True + if hasattr(logging,'captureWarnings'): + logging.captureWarnings(True) + colouriser = _ansistrm.ColorizingStreamHandler + colouriser.level_map[9] = (None, 'blue', False) + colouriser.level_map[10] = (None, 'cyan', False) + handler = colouriser(sys.stderr) + handler.setLevel(level) + + formatr = logging.Formatter(_fmt) + handler.setFormatter(formatr) + else: + handler = NullHandler() + + log = logging.getLogger('gnupg') + log.addHandler(handler) + log.setLevel(level) + log.info("Log opened: %s UTC" % datetime.ctime(datetime.utcnow())) + return log |