Package jazzparser :: Package utils :: Module loggers
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.loggers

  1  """Initialization of the logging system. 
  2   
  3  An application that calls methods that use logging should first call  
  4  init_logging, or else the logging messages will all get lost. 
  5   
  6  """ 
  7  """ 
  8  ============================== License ======================================== 
  9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 10    
 11   This file is part of The Jazz Parser. 
 12    
 13   The Jazz Parser is free software: you can redistribute it and/or modify 
 14   it under the terms of the GNU General Public License as published by 
 15   the Free Software Foundation, either version 3 of the License, or 
 16   (at your option) any later version. 
 17    
 18   The Jazz Parser is distributed in the hope that it will be useful, 
 19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21   GNU General Public License for more details. 
 22    
 23   You should have received a copy of the GNU General Public License 
 24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 25   
 26  ============================ End license ====================================== 
 27   
 28  """ 
 29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 30   
 31  import logging 
 32   
33 -def init_logging(log_level=None):
34 """ 35 Set up a logger to output test/error messages 36 """ 37 # Get a logger to configure from the logging system: 38 # will get the same one when logging later. 39 logger = logging.getLogger("main_logger") 40 if log_level is not None: 41 logger.setLevel(log_level) 42 43 # Create a console handler to output message to the console 44 chandler = logging.StreamHandler() 45 chandler.setLevel(logging.DEBUG) 46 # Format the logging messages thusly 47 format = "%(levelname)s: %(message)s" 48 formatter = logging.Formatter(format) 49 chandler.setFormatter(formatter) 50 51 # Add the console handler to the logger 52 logger.addHandler(chandler)
53 # Maybe add a file handler to the logger 54
55 -def create_logger(log_level=None, name=None, stdout=False, filename=None, stderr=False):
56 """ 57 Set up a logger to log to a file. 58 59 @type filename: string 60 @param filename: the file to write the logs to. If None, doesn't add a 61 handler for file output. 62 @param log_level: one of the level constants in L{logging}; the log level 63 to give the logger. Defaults to B{INFO}. 64 @type name: string 65 @param name: name to give to the logger. By default, uses the filename. If 66 neither is given, raises an exception. 67 @type stdout: bool 68 @param stdout: if True, also adds a handler to output to stdout 69 70 @return: the Logger instance created 71 72 """ 73 import sys 74 from jazzparser.utils.base import check_directory 75 76 if name is None: 77 if filename is None: 78 raise ValueError, "neither a name nor a filename was given for the "\ 79 "logger" 80 name = filename 81 82 # Create the logger 83 logger = logging.getLogger(name) 84 85 # Set the log level, or leave it as the default 86 if log_level is None: 87 logger.setLevel(logging.INFO) 88 else: 89 logger.setLevel(log_level) 90 91 formatter = logging.Formatter("%(levelname)s %(asctime)s: %(message)s", 92 "%Y-%m-%d %H:%M:%S") 93 94 if filename is not None: 95 # Make sure the containing directory exists 96 check_directory(filename) 97 # Create a file handler 98 fhandler = logging.FileHandler(filename, 'a') 99 fhandler.setLevel(logging.DEBUG) 100 fhandler.setFormatter(formatter) 101 # Use this handler 102 logger.addHandler(fhandler) 103 104 if stderr: 105 shandler = logging.StreamHandler() 106 shandler.setLevel(logging.DEBUG) 107 shandler.setFormatter(formatter) 108 logger.addHandler(shandler) 109 110 if stdout: 111 sohandler = logging.StreamHandler(sys.stdout) 112 sohandler.setLevel(logging.DEBUG) 113 sohandler.setFormatter(formatter) 114 logger.addHandler(sohandler) 115 116 return logger
117
118 -def create_dummy_logger():
119 """ 120 Creates a new logger that won't ever output anything. 121 122 """ 123 logger = logging.getLogger("dummy") 124 if len(logger.handlers) == 0: 125 logger.addHandler(NullHandler()) 126 return logger
127 128 global _logger_id 129 _logger_id = 0 130
131 -def create_plain_stderr_logger(log_level=None, stdout=False):
132 """ 133 Creates a new logging that just outputs the messages to stderr, with no 134 extra logging information. The log level will be set to debug by default, 135 so logging anything to this logger is just the same as writing to stderr. 136 137 If you call this more than once, a different logger will be returned. 138 139 @type stdout: bool 140 @param stdout: if True, uses stdout instead of stderr 141 142 """ 143 global _logger_id 144 if stdout: 145 logger = logging.getLogger("_stdout_logger_%d" % _logger_id) 146 else: 147 logger = logging.getLogger("_stderr_logger_%d" % _logger_id) 148 _logger_id += 1 149 150 if log_level is None: 151 logger.setLevel(logging.DEBUG) 152 else: 153 logger.setLevel(log_level) 154 155 # If this has been initialized before, remove all handlers and create a new one 156 if len(logger.handlers) > 0: 157 for h in logger.handlers: 158 logger.removeHandler(h) 159 160 if stdout: 161 import sys 162 handler = logging.StreamHandler(sys.stdout) 163 else: 164 handler = logging.StreamHandler() 165 fmt = logging.Formatter("%(message)s") 166 handler.setFormatter(fmt) 167 168 logger.addHandler(handler) 169 return logger
170 171
172 -class NullHandler(logging.Handler):
173 """ 174 A handler that drops all the logs. 175 176 This in the library for Python 3.1, but needs to be manually defined in 177 earlier versions. 178 179 """
180 - def emit(self, record):
181 pass
182