def inject_main_logfile(): # the main log file. log every kind of message, format properly, # rotate the log. someday - SocketHandler mainlog = logging.handlers.RotatingFileHandler(filename=logpath, mode='a', maxBytes=2**20, backupCount=1) mainlog.setFormatter(bt_log_fmt) mainlog.setLevel(logging.DEBUG) logger = logging.getLogger('') logging.getLogger('').addHandler(mainlog) logging.getLogger('').removeHandler(console) atexit_threads.register(lambda : logging.getLogger('').removeHandler(mainlog)) global stderr_console if not is_frozen_exe: # write all stderr messages to stderr (unformatted) # as well as the main log (formatted) stderr_console = logging.StreamHandler(old_stderr) stderr_console.setLevel(STDERR) stderr_console.setFormatter(logging.Formatter(u'%(message)s')) logging.getLogger('').addHandler(stderr_console)
def inject_main_logfile(): # the main log file. log every kind of message, format properly, # rotate the log. someday - SocketHandler mainlog = logging.handlers.RotatingFileHandler(filename=logpath, mode='a', maxBytes=2**20, backupCount=1) mainlog.setFormatter(bt_log_fmt) mainlog.setLevel(logging.DEBUG) logger = logging.getLogger('') logging.getLogger('').addHandler(mainlog) logging.getLogger('').removeHandler(console) atexit_threads.register( lambda: logging.getLogger('').removeHandler(mainlog)) global stderr_console if not is_frozen_exe: # write all stderr messages to stderr (unformatted) # as well as the main log (formatted) stderr_console = logging.StreamHandler(old_stderr) stderr_console.setLevel(STDERR) stderr_console.setFormatter(logging.Formatter(u'%(message)s')) logging.getLogger('').addHandler(stderr_console)
root_logger = logging.getLogger('') class StderrProxy(StringIO): # whew. ugly. is there a simpler way to write this? # the goal is to stop every '\n' and flush to the log # otherwise keep buffering. def write(self, text, *args): lines = text.split('\n') for t in lines[:-1]: if len(t) > 0: StringIO.write(self, t) try: # the docs don't say it, but logging.log is new in 2.4 #logging.log(STDERR, self.getvalue()) root_logger.log(STDERR, self.getvalue()) except: # logging failed. throwing a traceback would recurse pass self.truncate(0) if len(lines[-1]) > 0: StringIO.write(self, lines[-1]) sys.stderr = StderrProxy() def reset_stderr(): sys.stderr = old_stderr atexit_threads.register(reset_stderr)
class StderrProxy(StringIO): # whew. ugly. is there a simpler way to write this? # the goal is to stop every '\n' and flush to the log # otherwise keep buffering. def write(self, text, *args): lines = text.split('\n') for t in lines[:-1]: if len(t) > 0: StringIO.write(self, t) try: # the docs don't say it, but logging.log is new in 2.4 #logging.log(STDERR, self.getvalue()) root_logger.log(STDERR, self.getvalue()) except: # logging failed. throwing a traceback would recurse pass self.truncate(0) if len(lines[-1]) > 0: StringIO.write(self, lines[-1]) sys.stderr = StderrProxy() def reset_stderr(): sys.stderr = old_stderr atexit_threads.register(reset_stderr)