Package rrlog :: Module logging23
[hide private]
[frames] | no frames]

Source Code for Module rrlog.logging23

 1  #Copyright (c) 2007 
 2  #        Ruben Reifenberg, Germany, 07381. 
 3  #    All rights reserved. 
 4  # 
 5  #Redistribution and use in source and binary forms, with or without 
 6  #modification, are permitted provided that the following conditions 
 7  #are met: 
 8  #1. Redistributions of source code must retain the above copyright 
 9  #   notice, this list of conditions and the following disclaimer as 
10  #   the first lines of this file unmodified. 
11  #2. Redistributions in binary form must reproduce the above copyright 
12  #   notice, this list of conditions and the following disclaimer in the 
13  #   documentation and/or other materials provided with the distribution. 
14  # 
15  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
16  # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
17  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
18  # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
19  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
20  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
21  # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
22  # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
23  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
24  # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
25  # SUCH DAMAGE. 
26   
27  """ 
28  @summary: Integration into Python 2.3 logging as a Handler 
29  @author: Ruben Reifenberg 
30  @var LEVELMAP: 1:1 mapping of single characters (Like "E") to the level constants of the standard logging module; 
31  {logging-level: Category-Character}. You may modify this. 
32  """ 
33   
34   
35   
36  from logging import Handler,CRITICAL,ERROR,WARNING,INFO,DEBUG,NOTSET 
37   
38  LEVELMAP = { 
39          CRITICAL: "C", # == FATAL 
40          ERROR: "E", 
41          WARNING: "W", 
42          INFO: "I", 
43          DEBUG: "D", 
44          NOTSET: "", 
45          } 
46   
47 -def seFilesExclude(fname):
48 """ 49 callable that exludes logging.__init__ from stack extraction. 50 @return: True if fname is "logging/__init__.py" 51 """ 52 return fname.endswith("logging/__init__.py")
53
54 -class _Handler(Handler):
55 - def __init__(self, log, level=NOTSET):
56 """ 57 @param log: Your ready-to-use log object 58 @param level: One of the ordered standard logging constants 59 """ 60 Handler.__init__(self, level) 61 self._log = log
62
63 - def emit(self, record):
64 self._log(record.msg, traceDepth=2)
65
66 -def handler(*args, **kwargs):
67 return _Handler(*args, **kwargs)
68 69
70 -def useALogger():
71 logger = logging.getLogger("gimmeALogger") 72 for i in range(0,7): 73 logger.info("info%d?"%(i)) 74 logger.warn("warning%d?"%(i)) 75 logger.error("error%d?"%(i))
76 77 if __name__ == "__main__": 78 from rrlog.server.filewriter import createLocalLog # local == all in same process, no separate server 79 log = createLocalLog( 80 filePathPattern="./integrated-log-%s.txt", # "pattern" because %s (or %d) is required for the rotate-number 81 rotateCount=3, # rotate over 3 files 82 rotateLineMin=10, #at least 10 lines in each file before rotating 83 #seFilesExclude=seFilesExclude, 84 ) 85 import logging 86 logger = logging.getLogger("gimmeALogger") 87 logger.setLevel(WARNING) 88 logger.addHandler(handler(log)) 89 useALogger() 90