Mercurial > genshi > mirror
comparison examples/trac/trac/log.py @ 39:93b4dcbafd7b trunk
Copy Trac to main branch.
author | cmlenz |
---|---|
date | Mon, 03 Jul 2006 18:53:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
38:ee669cb9cccc | 39:93b4dcbafd7b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # | |
3 # Copyright (C) 2003-2006 Edgewall Software | |
4 # Copyright (C) 2003-2005 Daniel Lundin <daniel@edgewall.com> | |
5 # Copyright (C) 2006 Christian Boos <cboos@neuf.fr> | |
6 # All rights reserved. | |
7 # | |
8 # This software is licensed as described in the file COPYING, which | |
9 # you should have received as part of this distribution. The terms | |
10 # are also available at http://trac.edgewall.com/license.html. | |
11 # | |
12 # This software consists of voluntary contributions made by many | |
13 # individuals. For the exact contribution history, see the revision | |
14 # history and logs, available at http://projects.edgewall.com/trac/. | |
15 # | |
16 # Author: Daniel Lundin <daniel@edgewall.com> | |
17 | |
18 import logging | |
19 import logging.handlers | |
20 import sys | |
21 | |
22 def logger_factory(logtype='syslog', logfile=None, level='WARNING', | |
23 logid='Trac'): | |
24 logger = logging.getLogger(logid) | |
25 logtype = logtype.lower() | |
26 if logtype == 'file': | |
27 hdlr = logging.FileHandler(logfile) | |
28 elif logtype in ['winlog', 'eventlog', 'nteventlog']: | |
29 # Requires win32 extensions | |
30 hdlr = logging.handlers.NTEventLogHandler(logid, | |
31 logtype='Application') | |
32 elif logtype in ['syslog', 'unix']: | |
33 hdlr = logging.handlers.SysLogHandler('/dev/log') | |
34 elif logtype in ['stderr']: | |
35 hdlr = logging.StreamHandler(sys.stderr) | |
36 else: | |
37 hdlr = logging.handlers.BufferingHandler(0) | |
38 # Note: this _really_ throws away log events, as a `MemoryHandler` | |
39 # would keep _all_ records in case there's no target handler (a bug?) | |
40 | |
41 format = 'Trac[%(module)s] %(levelname)s: %(message)s' | |
42 if logtype in ['file', 'stderr']: | |
43 format = '%(asctime)s ' + format | |
44 datefmt = '' | |
45 if logtype == 'stderr': | |
46 datefmt = '%X' | |
47 level = level.upper() | |
48 if level in ['DEBUG', 'ALL']: | |
49 logger.setLevel(logging.DEBUG) | |
50 elif level == 'INFO': | |
51 logger.setLevel(logging.INFO) | |
52 elif level == 'ERROR': | |
53 logger.setLevel(logging.ERROR) | |
54 elif level == 'CRITICAL': | |
55 logger.setLevel(logging.CRITICAL) | |
56 else: | |
57 logger.setLevel(logging.WARNING) | |
58 formatter = logging.Formatter(format,datefmt) | |
59 hdlr.setFormatter(formatter) | |
60 logger.addHandler(hdlr) | |
61 | |
62 return logger |