Package starcluster :: Module logger
[hide private]
[frames] | no frames]

Source Code for Module starcluster.logger

  1  """ 
  2  StarCluster logging module 
  3  """ 
  4  import os 
  5  import types 
  6  import logging 
  7  import logging.handlers 
  8  import textwrap 
  9  import StringIO 
 10   
 11  from starcluster import static 
 12   
 13  INFO = logging.INFO 
 14  DEBUG = logging.DEBUG 
 15  WARN = logging.WARN 
 16  ERROR = logging.ERROR 
 17  CRITICAL = logging.CRITICAL 
 18  FATAL = logging.FATAL 
 19   
 20  RAW_FORMAT = "%(message)s\n" 
 21  INFO_FORMAT = " ".join(['>>>', "%(message)s\n"]) 
 22  _DEBUG_FORMAT = "%(filename)s:%(lineno)d - %(levelname)s - %(message)s\n" 
 23  DEBUG_FORMAT = "%(asctime)s " + _DEBUG_FORMAT 
 24  DEBUG_FORMAT_PID = ' '.join(["%(asctime)s", "PID: %s" % str(static.PID), 
 25                               _DEBUG_FORMAT]) 
 26  DEFAULT_CONSOLE_FORMAT = "%(levelname)s - %(message)s\n" 
 27  ERROR_CONSOLE_FORMAT = " ".join(['!!!', DEFAULT_CONSOLE_FORMAT]) 
 28  WARN_CONSOLE_FORMAT = " ".join(['***', DEFAULT_CONSOLE_FORMAT]) 
 29   
 30   
31 -class ConsoleLogger(logging.StreamHandler):
32 33 formatters = { 34 INFO: logging.Formatter(INFO_FORMAT), 35 DEBUG: logging.Formatter(DEBUG_FORMAT), 36 WARN: logging.Formatter(WARN_CONSOLE_FORMAT), 37 ERROR: logging.Formatter(ERROR_CONSOLE_FORMAT), 38 CRITICAL: logging.Formatter(ERROR_CONSOLE_FORMAT), 39 FATAL: logging.Formatter(ERROR_CONSOLE_FORMAT), 40 'raw': logging.Formatter(RAW_FORMAT), 41 } 42
43 - def format(self, record):
44 if hasattr(record, '__raw__'): 45 result = self.formatters['raw'].format(record) 46 else: 47 result = self.formatters[record.levelno].format(record) 48 if hasattr(record, '__nonewline__'): 49 result = result.rstrip() 50 return result
51
52 - def _wrap(self, msg):
53 msg = textwrap.wrap(msg, width=60, replace_whitespace=False, 54 drop_whitespace=True, break_on_hyphens=False) 55 return msg or ['']
56
57 - def _emit_textwrap(self, record):
58 lines = [] 59 for line in record.msg.splitlines(): 60 lines.extend(self._wrap(line)) 61 if hasattr(record, '__nosplitlines__'): 62 lines = ['\n'.join(lines)] 63 for line in lines: 64 record.msg = line 65 self._emit(record)
66
67 - def _emit(self, record):
68 msg = self.format(record) 69 fs = "%s" 70 if not hasattr(types, "UnicodeType"): 71 # if no unicode support... 72 self.stream.write(fs % msg) 73 else: 74 try: 75 self.stream.write(fs % msg) 76 except UnicodeError: 77 self.stream.write(fs % msg.encode("UTF-8")) 78 self.flush()
79
80 - def emit(self, record):
81 try: 82 if hasattr(record, '__textwrap__'): 83 self._emit_textwrap(record) 84 else: 85 self._emit(record) 86 except (KeyboardInterrupt, SystemExit): 87 raise 88 except: 89 self.handleError(record)
90 91
92 -class NullHandler(logging.Handler):
93 - def emit(self, record):
94 pass
95 96
97 -def get_starcluster_logger():
98 log = logging.getLogger('starcluster') 99 log.addHandler(NullHandler()) 100 return log
101 102 103 log = get_starcluster_logger() 104 console = ConsoleLogger() 105 session = logging.StreamHandler(StringIO.StringIO()) 106 107
108 -def configure_sc_logging(use_syslog=False):
109 """ 110 Configure logging for StarCluster *application* code 111 112 By default StarCluster's logger has no formatters and a NullHandler so that 113 other developers using StarCluster as a library can configure logging as 114 they see fit. This method is used in StarCluster's application code (i.e. 115 the 'starcluster' command) to toggle StarCluster's application specific 116 formatters/handlers 117 118 use_syslog - enable logging all messages to syslog. currently only works if 119 /dev/log exists on the system (standard for most Linux distros) 120 """ 121 log.setLevel(logging.DEBUG) 122 formatter = logging.Formatter(DEBUG_FORMAT_PID.rstrip()) 123 static.create_sc_config_dirs() 124 rfh = logging.handlers.RotatingFileHandler(static.DEBUG_FILE, 125 maxBytes=1048576, 126 backupCount=2) 127 rfh.setLevel(logging.DEBUG) 128 rfh.setFormatter(formatter) 129 log.addHandler(rfh) 130 console.setLevel(logging.INFO) 131 log.addHandler(console) 132 session.setLevel(logging.DEBUG) 133 session.setFormatter(formatter) 134 log.addHandler(session) 135 syslog_device = '/dev/log' 136 if use_syslog and os.path.exists(syslog_device): 137 log.debug("Logging to %s" % syslog_device) 138 syslog_handler = logging.handlers.SysLogHandler(address=syslog_device) 139 syslog_handler.setFormatter(formatter) 140 syslog_handler.setLevel(logging.DEBUG) 141 log.addHandler(syslog_handler)
142 143
144 -def configure_paramiko_logging():
145 """ 146 Configure paramiko to log to a file for debug 147 """ 148 l = logging.getLogger("paramiko") 149 l.setLevel(logging.DEBUG) 150 static.create_sc_config_dirs() 151 lh = logging.handlers.RotatingFileHandler(static.SSH_DEBUG_FILE, 152 maxBytes=1048576, 153 backupCount=2) 154 lh.setLevel(logging.DEBUG) 155 format = (('PID: %s ' % str(static.PID)) + 156 '%(levelname)-.3s [%(asctime)s.%(msecs)03d] ' 157 'thr=%(_threadid)-3d %(name)s: %(message)s') 158 date_format = '%Y%m%d-%H:%M:%S' 159 lh.setFormatter(logging.Formatter(format, date_format)) 160 l.addHandler(lh)
161 162
163 -def configure_boto_logging():
164 """ 165 Configure boto to log to a file for debug 166 """ 167 l = logging.getLogger("boto") 168 l.setLevel(logging.DEBUG) 169 static.create_sc_config_dirs() 170 lh = logging.handlers.RotatingFileHandler(static.AWS_DEBUG_FILE, 171 maxBytes=1048576, 172 backupCount=2) 173 lh.setLevel(logging.DEBUG) 174 format = (('PID: %s ' % str(static.PID)) + 175 '%(levelname)-.3s [%(asctime)s.%(msecs)03d] ' 176 '%(name)s: %(message)s') 177 date_format = '%Y%m%d-%H:%M:%S' 178 lh.setFormatter(logging.Formatter(format, date_format)) 179 l.addHandler(lh)
180