PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/circuits/app/log.py

https://bitbucket.org/prologic/circuits/
Python | 91 lines | 70 code | 15 blank | 6 comment | 5 complexity | b1817db96dcd417ed6f74839e5a86d1e MD5 | raw file
 1# Module:   logging
 2# Date:     11th June 2006
 3# Author:   James Mills <prologic@shortcircuit.net.au>
 4
 5"""Logging Components"""
 6
 7
 8import sys
 9import logging
10from logging import DEBUG, INFO, WARNING, WARN, ERROR, CRITICAL
11
12from circuits.core import handler, Event, BaseComponent
13
14
15class Log(Event):
16    """Log Event"""
17
18    channel = "log"
19    target = "logger"
20
21
22class Logger(BaseComponent):
23
24    channel = "logger"
25
26    LEVELS = {"debug": DEBUG, "info": INFO, "warning": WARNING,
27            "warn": WARN, "error": ERROR, "exception": ERROR,
28            "critical": CRITICAL}
29
30    def __init__(self, filename, name, type, level, channel=channel):
31        super(Logger, self).__init__(channel=channel)
32
33        self.logger = logging.getLogger(name)
34
35        type = type.lower()
36        if type == "file":
37            hdlr = logging.FileHandler(filename)
38        elif type in ["winlog", "eventlog", "nteventlog"]:
39            # Requires win32 extensions
40            hdlr = logging.handlers.NTEventLogHandler(name, type="Application")
41        elif type in ["syslog", "unix"]:
42            hdlr = logging.handlers.SysLogHandler("/dev/log")
43        elif type in ["stderr"]:
44            hdlr = logging.StreamHandler(sys.stderr)
45        else:
46            raise ValueError
47
48        format = name + "[%(module)s] %(levelname)s: %(message)s"
49        if type == "file":
50            format = "%(asctime)s " + format
51        dateFormat = ""
52        level = level.upper()
53        if level in ["DEBUG", "ALL"]:
54            self.logger.setLevel(logging.DEBUG)
55            dateFormat = "%X"
56        elif level == "INFO":
57            self.logger.setLevel(logging.INFO)
58        elif level == "ERROR":
59            self.logger.setLevel(logging.ERROR)
60        elif level == "CRITICAL":
61            self.logger.setLevel(logging.CRITICAL)
62        else:
63            self.logger.setLevel(logging.WARNING)
64
65        formatter = logging.Formatter(format,dateFormat)
66        hdlr.setFormatter(formatter)
67        self.logger.addHandler(hdlr)
68
69    @handler("log")
70    def _on_log(self, level, msg, *args, **kwargs):
71        self.logger.log(self.LEVELS[level.lower()], msg, *args, **kwargs)
72
73    def debug(self, msg, *args, **kwargs):
74        self.logger.debug(msg, *args, **kwargs)
75
76    def info(self, msg, *args, **kwargs):
77        self.logger.info(msg, *args, **kwargs)
78
79    def warning(self, msg, *args, **kwargs):
80        self.logger.warning(msg, *args, **kwargs)
81
82    warn = warning
83
84    def error(self, msg, *args, **kwargs):
85        self.logger.error(msg, *args, **kwargs)
86
87    def exception(self, msg, *args, **kwargs):
88        self.logger.exception(msg, *args)
89
90    def critical(self, msg, *args, **kwargs):
91        self.logger.critical(msg, *args, **kwargs)