00001 import logging 00002 import os 00003 import threading 00004 import sys 00005 import time 00006 00007 logdir = '/var/log/roam' 00008 logfilecount = 10 00009 datefmt="%c %s" 00010 00011 class MyFormatter(logging.Formatter): 00012 def formatTime(self, record, datefmt=None): 00013 ct = self.converter(record.created) 00014 t = time.strftime("%Y-%m-%d %H:%M:%S / %s", ct) 00015 s = "%s.%03d" % (t, record.msecs) 00016 return s 00017 00018 00019 file_formatter = MyFormatter('%(asctime)s - %(levelname)s - %(message)s', datefmt=datefmt) 00020 00021 class LoggerStream: 00022 def __init__(self, func): 00023 self.lock = threading.Lock() 00024 self.buffer = "" 00025 self.func = func 00026 00027 def write(self, str): 00028 with self.lock: 00029 self.buffer += str 00030 while True: 00031 pos = self.buffer.find('\n') 00032 if pos == -1: 00033 break 00034 self.func(self.buffer[0:pos]) 00035 self.buffer = self.buffer[pos+1:] 00036 00037 def flush(self): 00038 if self.buffer: 00039 with self.lock: 00040 self.func(self.buffer) 00041 00042 all_logger = logging.getLogger('') 00043 all_logger_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir,'all.log'), when='midnight', backupCount=logfilecount) 00044 all_logger_formatter = MyFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 00045 all_logger_handler.setFormatter(all_logger_formatter) 00046 all_logger_handler.setLevel(logging.DEBUG) 00047 all_logger.addHandler(all_logger_handler) 00048 00049 console_logger = logging.getLogger('console') 00050 console_logger.setLevel(logging.DEBUG) 00051 00052 console_handler = logging.StreamHandler(sys.stdout) 00053 console_handler.setLevel(logging.DEBUG) 00054 console_formatter = MyFormatter('%(message)s') 00055 console_handler.setFormatter(console_formatter) 00056 console_logger.addHandler(console_handler) 00057 00058 console_file_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir,'console-output.log'), when='midnight', backupCount=logfilecount) 00059 console_file_handler.setFormatter(file_formatter) 00060 console_file_handler.setLevel(logging.DEBUG) 00061 console_logger.addHandler(console_file_handler) 00062 00063 sys.stdout = LoggerStream(console_logger.info) 00064 sys.stderr = LoggerStream(console_logger.error) 00065 00066 def get_logger_stream(name, level = logging.DEBUG): 00067 logger = logging.getLogger(name) 00068 logger.setLevel(logging.DEBUG) 00069 return LoggerStream(logger.info) 00070 00071 def make_logger_file(name, level = logging.DEBUG): 00072 file_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir, name), when='midnight', backupCount=logfilecount) 00073 file_handler.setFormatter(file_formatter) 00074 file_handler.setLevel(level) 00075 logger = logging.getLogger(name) 00076 logger.addHandler(file_handler) 00077 00078 def get_logger_stream_for_file(name, level = logging.DEBUG): 00079 make_logger_file(name, level) 00080 return get_logger_stream(name, level)