File: loggingapi.py

package info (click to toggle)
zopeinterface 3.0.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 700 kB
  • ctags: 1,053
  • sloc: python: 4,958; ansic: 417; makefile: 19
file content (98 lines) | stat: -rw-r--r-- 2,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Compatibility wrapper around the logging package.

If the logging package isn't available (as in Python 2.2), this
provides a limited implementation so the zpkgtools setup.py support
code doesn't fall over.

This isn't sufficient to support building packages with Python 2.2.

$Id: loggingapi.py,v 1.1 2004/06/11 19:24:35 fdrake Exp $
"""

try:
    from logging import getLogger
    from logging import CRITICAL, FATAL, ERROR, WARNING, WARN
    from logging import INFO, DEBUG, NOTSET
    from logging import Handler, StreamHandler

except ImportError:

    CRITICAL = 50
    FATAL = CRITICAL
    ERROR = 40
    WARNING = 30
    WARN = WARNING
    INFO = 20
    DEBUG = 10
    NOTSET = 0


    _loggers = {}

    def getLogger(name=None):
        if "root" not in _loggers:
            _loggers["root"] = Logger("root")
            _loggers["root"].setLevel(WARNING)
        name = name or "root"
        if name not in _loggers:
            _loggers[name] = Logger(name, _loggers["root"])
        return _loggers[name]


    class Logger:

        def __init__(self, name, root=None):
            self.name = name
            self.level = NOTSET
            self._handler = None
            self._root = root

        def setLevel(self, level):
            self.level = level

        def addHandler(self, handler):
            self._handler = handler

        def log(self, level, msg, *args, **kwargs):
            loggers = [self]
            if self._root is not None:
                loggers.append(self._root)

            for logger in loggers:
                effective_level = logger.level
                if effective_level == NOTSET and logger._root is not None:
                    effective_level = self._root.level
                if level >= effective_level:
                    self._log(self.name, level, msg, args, kwargs)

        def _log(self, name, level, msg, args, kwargs):
            if args:
                msg = msg % args
            elif kwargs:
                msg = msg % kwargs
            msg = "%s(%s):%s" % (name, level, msg)
            print >>self._handler, msg


    class Handler:

        def __init__(self):
            pass


    def StreamHandler():
        import sys
        return sys.stderr