Home | Trees | Indices | Help |
|
---|
|
1 #!/usr/bin/env python 2 """ 3 StarCluster logging module 4 """ 5 import os 6 import types 7 import logging 8 import logging.handlers 9 import textwrap 10 import StringIO 11 12 from starcluster import static 13 14 INFO = logging.INFO 15 DEBUG = logging.DEBUG 16 WARN = logging.WARN 17 ERROR = logging.ERROR 18 CRITICAL = logging.CRITICAL 19 FATAL = logging.FATAL 20 21 RAW_FORMAT = "%(message)s\n" 22 INFO_FORMAT = " ".join(['>>>', "%(message)s\n"]) 23 _DEBUG_FORMAT = "%(filename)s:%(lineno)d - %(levelname)s - %(message)s\n" 24 DEBUG_FORMAT = "%(asctime)s " + _DEBUG_FORMAT 25 DEBUG_FORMAT_PID = ' '.join(["%(asctime)s", "PID: %s" % str(static.PID), 26 _DEBUG_FORMAT]) 27 DEFAULT_CONSOLE_FORMAT = "%(levelname)s - %(message)s\n" 28 ERROR_CONSOLE_FORMAT = " ".join(['!!!', DEFAULT_CONSOLE_FORMAT]) 29 WARN_CONSOLE_FORMAT = " ".join(['***', DEFAULT_CONSOLE_FORMAT]) 30 3133 34 formatters = { 35 INFO: logging.Formatter(INFO_FORMAT), 36 DEBUG: logging.Formatter(DEBUG_FORMAT), 37 WARN: logging.Formatter(WARN_CONSOLE_FORMAT), 38 ERROR: logging.Formatter(ERROR_CONSOLE_FORMAT), 39 CRITICAL: logging.Formatter(ERROR_CONSOLE_FORMAT), 40 FATAL: logging.Formatter(ERROR_CONSOLE_FORMAT), 41 'raw': logging.Formatter(RAW_FORMAT), 42 } 4391 92 96 97 102 103 104 log = get_starcluster_logger() 105 console = ConsoleLogger() 106 session = logging.StreamHandler(StringIO.StringIO()) 107 10845 if hasattr(record, '__raw__'): 46 result = self.formatters['raw'].format(record) 47 else: 48 result = self.formatters[record.levelno].format(record) 49 if hasattr(record, '__nonewline__'): 50 result = result.rstrip() 51 return result5254 msg = textwrap.wrap(msg, width=60, replace_whitespace=False, 55 drop_whitespace=True, break_on_hyphens=False) 56 return msg or ['']5759 lines = [] 60 for line in record.msg.splitlines(): 61 lines.extend(self._wrap(line)) 62 if hasattr(record, '__nosplitlines__'): 63 lines = ['\n'.join(lines)] 64 for line in lines: 65 record.msg = line 66 self._emit(record)6769 msg = self.format(record) 70 fs = "%s" 71 if not hasattr(types, "UnicodeType"): 72 # if no unicode support... 73 self.stream.write(fs % msg) 74 else: 75 try: 76 self.stream.write(fs % msg) 77 except UnicodeError: 78 self.stream.write(fs % msg.encode("UTF-8")) 79 self.flush()8082 try: 83 if hasattr(record, '__textwrap__'): 84 self._emit_textwrap(record) 85 else: 86 self._emit(record) 87 except (KeyboardInterrupt, SystemExit): 88 raise 89 except: 90 self.handleError(record)110 """ 111 Configure logging for StarCluster *application* code 112 113 By default StarCluster's logger has no formatters and a NullHandler so that 114 other developers using StarCluster as a library can configure logging as 115 they see fit. This method is used in StarCluster's application code (ie the 116 'starcluster' command) to toggle StarCluster's application specific 117 formatters/handlers 118 119 use_syslog - enable logging all messages to syslog. currently only works if 120 /dev/log exists on the system (standard for most Linux distros) 121 """ 122 log.setLevel(logging.DEBUG) 123 formatter = logging.Formatter(DEBUG_FORMAT_PID.rstrip()) 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 143145 """ 146 Configure paramiko to log to a file for debug 147 """ 148 l = logging.getLogger("paramiko") 149 l.setLevel(logging.DEBUG) 150 lh = logging.handlers.RotatingFileHandler(static.SSH_DEBUG_FILE, 151 maxBytes=1048576, 152 backupCount=2) 153 lh.setLevel(logging.DEBUG) 154 format = (('PID: %s ' % str(static.PID)) + \ 155 '%(levelname)-.3s [%(asctime)s.%(msecs)03d] ' + \ 156 'thr=%(_threadid)-3d %(name)s: %(message)s') 157 date_format = '%Y%m%d-%H:%M:%S' 158 lh.setFormatter(logging.Formatter(format, date_format)) 159 l.addHandler(lh)160 161163 """ 164 Configure boto to log to a file for debug 165 """ 166 l = logging.getLogger("boto") 167 l.setLevel(logging.DEBUG) 168 lh = logging.handlers.RotatingFileHandler(static.AWS_DEBUG_FILE, 169 maxBytes=1048576, 170 backupCount=2) 171 lh.setLevel(logging.DEBUG) 172 format = (('PID: %s ' % str(static.PID)) + \ 173 '%(levelname)-.3s [%(asctime)s.%(msecs)03d] ' + \ 174 '%(name)s: %(message)s') 175 date_format = '%Y%m%d-%H:%M:%S' 176 lh.setFormatter(logging.Formatter(format, date_format)) 177 l.addHandler(lh)178
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Mon Oct 17 13:43:36 2011 | http://epydoc.sourceforge.net |