Package commons :: Module log
[hide private]
[frames] | no frames]

Source Code for Module commons.log

  1  # -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- 
  2  # vim:ft=python:et:sw=4:ts=4 
  3   
  4  """ 
  5  Convenience functions for the standard L{logging} library. 
  6   
  7  Functions are mostly self-explanatory. 
  8  """ 
  9   
 10  # TODO: consider adding other lazy logging functions that take a lambda. 
 11  # eg: debug(lambda: 'currently at %s items' % n)) 
 12   
 13  from strs import * 
 14  from itertools import chain, imap 
 15  import logging, logging.handlers, sys 
 16  from logging import getLogger 
 17   
 18  __all__ = """ 
 19  fmt 
 20  log 
 21  exception 
 22  critical 
 23  error 
 24  warning 
 25  info 
 26  die 
 27  tailor_logs 
 28  config_logging 
 29  """.split() 
 30   
 31  # TODO cleanup / reorganize this section 
 32   
33 -def fmt( flag, *args ):
34 return ' '.join( chain( [ '%-20s:' % flag ], imap( unicode, args ) ) )
35
36 -def log( level, flag, *args ):
37 getLogger( flag ).log( level, fmt( flag, *args ) )
38
39 -def exception( flag, *args ):
40 getLogger( flag ).exception( fmt( flag, *args ) )
41
42 -def critical( flag, *args ):
43 log( logging.CRITICAL, flag, *args )
44
45 -def error( flag, *args ):
46 log( logging.ERROR, flag, *args )
47
48 -def warning( flag, *args ):
49 log( logging.WARNING, flag, *args )
50
51 -def debug( flag, *args ):
52 log( logging.DEBUG, flag, *args )
53
54 -def info( flag, *args ):
55 log( logging.INFO, flag, *args )
56
57 -def die( flag, *args ):
58 error( flag, *args ) 59 sys.exit( 1 )
60
61 -def tailor_logs( flag, *prefix ):
62 """ 63 Avoid repetitive typing! Usage:: 64 65 debug,info,warning,_,_ = tailor_logs('myflag','some','prefix') 66 """ 67 def new_critical( *args ): critical( flag, * prefix + args ) 68 def new_debug( *args ): debug( flag, * prefix + args ) 69 def new_error( *args ): error( flag, * prefix + args ) 70 def new_info( *args ): info( flag, * prefix + args ) 71 def new_warning( *args ): warning( flag, * prefix + args ) 72 return new_debug, new_info, new_warning, new_error, new_critical
73
74 -def config_logging( level = logging.INFO, 75 do_console = False, 76 do_file = False, 77 do_server = False, 78 flags = [], 79 path = None, 80 log_format = '%(asctime)s %(levelname)-8s %(message)s', 81 log_date_format = None ):
82 """ 83 Configures L{logging}, promoting some common configurations. 84 85 @param level: The default log-level threshold for all 86 loggers. (Note that this is not the level for all handlers.) 87 @type level: int 88 89 @param do_console: Whether to enable the console handler. 90 @type do_console: bool 91 92 @param do_file: Whether to enable the file handler. 93 @type do_file: bool 94 95 @param do_server: Whether to enable a network socket stream 96 handler. 97 @type do_server: bool 98 99 @param flags: The set of flags on which to lower the threshold to 100 C{logging.DEBUG}. 101 @type flags: iterable 102 103 @param path: The path of the log file (only considered if 104 C{do_file} is C{True}. 105 @type path: str 106 107 @param log_format: The log message formatting template. 108 @type log_format: str 109 110 @param log_date_format: The timestamp formatting template. 111 @type log_date_format: str 112 """ 113 formatter = logging.Formatter( log_format, log_date_format ) 114 115 logging.getLogger( '' ).setLevel( level ) 116 logging.raiseExceptions = False 117 118 # log to console 119 if do_console: 120 handler = logging.StreamHandler() 121 handler.setLevel( logging.DEBUG ) 122 handler.setFormatter( formatter ) 123 logging.getLogger( '' ).addHandler( handler ) 124 125 # log to files 126 if do_file and path is not None: 127 handler = logging.FileHandler( path, 'a' ) 128 handler.setFormatter( formatter ) 129 handler.setLevel( logging.DEBUG ) 130 logging.getLogger( '' ).addHandler( handler ) 131 132 # log to the log server 133 if do_server: 134 handler = logging.handlers.SocketHandler( 'localhost', 135 logging.handlers.DEFAULT_TCP_LOGGING_PORT ) 136 handler.setLevel( logging.DEBUG ) 137 logging.getLogger( '' ).addHandler( handler ) 138 139 for flag in flags: 140 logging.getLogger( flag ).setLevel( logging.DEBUG )
141 142 #### logging.raiseExceptions = False 143 #### config_path = get_usr_conf( 'LOGGING_CONFIG_PATH', 'logging.ini' ) 144 #### if config_path is not None: 145 #### logging.config.fileConfig( config_path ) 146