source: trunk/packages/xen-3.1/xen-3.1/tools/python/logging/logging-0.4.9.2/test/log_test1.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

  • Property svn:executable set to *
  • Property svn:mime-type set to text/script
File size: 3.0 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
4#
5# Permission to use, copy, modify, and distribute this software and its
6# documentation for any purpose and without fee is hereby granted,
7# provided that the above copyright notice appear in all copies and that
8# both that copyright notice and this permission notice appear in
9# supporting documentation, and that the name of Vinay Sajip
10# not be used in advertising or publicity pertaining to distribution
11# of the software without specific, written prior permission.
12# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
15# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
16# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19# This file is part of the Python logging distribution. See
20# http://www.red-dove.com/python_logging.html
21#
22"""Test harness for the logging module. A test showing exception handling and use of SysLogHandler.
23
24Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
25"""
26import sys, logging
27import syslog
28
29class SLHandler(logging.Handler):
30    def __init__(self, ident, logopt=0, facility=syslog.LOG_USER):
31        logging.Handler.__init__(self)
32        self.ident = ident
33        self.logopt = logopt
34        self.facility = facility
35        self.mappings = {
36            logging.DEBUG: syslog.LOG_DEBUG,
37            logging.INFO: syslog.LOG_INFO,
38            logging.WARNING: syslog.LOG_WARNING,
39            logging.ERROR: syslog.LOG_ERR,
40            logging.CRITICAL: syslog.LOG_CRIT,
41            }
42
43    def encodeLevel(self, level):
44        return self.mappings.get(level, syslog.LOG_INFO)
45
46    def emit(self, record):
47        syslog.openlog(self.ident, self.logopt, self.facility)
48        msg = self.format(record)
49        prio = self.encodeLevel(record.levelno)
50        syslog.syslog(prio, msg)
51        syslog.closelog()
52
53def config():
54    logging.basicConfig()
55    logging.getLogger("").setLevel(logging.DEBUG)
56    if __name__ == "__main__":
57        fmt = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d %(levelname)-5s - %(message)s")
58        hdlr = logging.FileHandler("tmp.tmp")
59        hdlr.setFormatter(fmt)
60        logging.getLogger("").addHandler(hdlr)
61    else:
62        fmt = None
63    hdlr = SLHandler("log_test1")
64    if fmt:
65        hdlr.setFormatter(fmt)
66    logging.getLogger("").addHandler(hdlr)
67    return hdlr
68
69def run():
70    logging.info("Starting...")
71    try:
72        print "7" + 4
73    except Exception, e:
74        logging.error("Problem %s (%d)", "ERROR", logging.ERROR, exc_info=1)
75        logging.debug("Problem %s (%d)", "DEBUG", logging.DEBUG, exc_info=1)
76    logging.info("Done.")
77
78
79def main():
80    hdlr = config()
81    run()
82    logging.getLogger("").removeHandler(hdlr)
83
84if __name__ == "__main__":
85    main()
Note: See TracBrowser for help on using the repository browser.