debuggers.hg

view tools/python/logging/logging-0.4.9.2/test/log_test10.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children dae98734f12e
line source
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. Shows use of a user-defined Logger subclass.
24 Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
25 """
26 import sys
27 import locale
29 locale.setlocale(locale.LC_ALL, '')
31 from logging import *
33 LOG_FORMAT = "%(asctime)s %(levelname)-5s %(message)s"
34 DATE_FORMAT = "%x %X"
36 class MyLogger(Logger):
37 """
38 A simple example of a logger extension.
39 """
40 def debug(self, msg, *args, **kwargs):
41 """
42 This overridden method passes exception information for DEBUG level calls
43 """
44 if self.manager.disable >= DEBUG:
45 return
46 if DEBUG >= self.getEffectiveLevel():
47 exc_info = kwargs.get("exc_info", 0)
48 ei = None
49 if exc_info:
50 ei = sys.exc_info()
51 if not ei[1]:
52 ei = None
53 self._log(DEBUG, msg, args, ei)
54 del ei
56 class NotALogger:
57 pass
59 def config():
60 try:
61 setLoggerClass(NotALogger)
62 except Exception, e:
63 sys.stderr.write("%s\n" % e)
64 setLoggerClass(MyLogger)
65 if __name__ == "__main__":
66 basicConfig()
67 if __name__ == "__main__":
68 getLogger("").handlers[0].setFormatter(Formatter(LOG_FORMAT, DATE_FORMAT))
70 def run():
71 getLogger("").setLevel(DEBUG)
72 logger = getLogger("mylogger")
73 logger.info("Starting...")
74 logger.debug("Debug message not in exception handler (no traceback)")
75 logger.info("About to throw exception...")
76 try:
77 print "7" + 4
78 except Exception, e:
79 logger.debug("Debug message inside exception handler (traceback)",exc_info=1)
80 logger.info("Done.")
82 def main():
83 config()
84 run()
86 if __name__ == "__main__":
87 main()