logging_config.py
Go to the documentation of this file.
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         self.progress = None
00027 
00028     def write(self, str):
00029         if self.progress and str.partition('\n')[0].endswith(self.progress):
00030             return
00031         with self.lock:
00032             self.buffer += str
00033             while True:
00034                 pos = self.buffer.find('\n')
00035                 if pos == -1:
00036                     break
00037                 self.progress = self.buffer[0:pos]
00038                 self.func(self.progress)
00039                 self.progress = None
00040                 self.buffer = self.buffer[pos+1:]
00041 
00042     def flush(self):
00043         if self.buffer:
00044             with self.lock:
00045                 self.func(self.buffer)
00046 
00047 all_logger = logging.getLogger('')
00048 all_logger_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir,'all.log'), when='midnight', backupCount=logfilecount)
00049 all_logger_formatter = MyFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
00050 all_logger_handler.setFormatter(all_logger_formatter)
00051 all_logger_handler.setLevel(logging.DEBUG)
00052 all_logger.addHandler(all_logger_handler)
00053 
00054 console_logger = logging.getLogger('console')
00055 console_logger.setLevel(logging.DEBUG)
00056 
00057 console_handler = logging.StreamHandler(sys.stdout)
00058 console_handler.setLevel(logging.DEBUG)
00059 console_formatter = MyFormatter('%(message)s')
00060 console_handler.setFormatter(console_formatter)
00061 console_logger.addHandler(console_handler)
00062 
00063 console_file_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir,'console-output.log'), when='midnight', backupCount=logfilecount)
00064 console_file_handler.setFormatter(file_formatter)
00065 console_file_handler.setLevel(logging.DEBUG)
00066 console_logger.addHandler(console_file_handler)
00067 
00068 sys.stdout = LoggerStream(console_logger.info)
00069 sys.stderr = LoggerStream(console_logger.error)
00070 
00071 def get_logger_stream(name, level = logging.DEBUG):
00072     logger = logging.getLogger(name)
00073     logger.setLevel(logging.DEBUG)
00074     return LoggerStream(logger.info)
00075 
00076 def make_logger_file(name, level = logging.DEBUG):
00077     file_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(logdir, name), when='midnight', backupCount=logfilecount)
00078     file_handler.setFormatter(file_formatter)
00079     file_handler.setLevel(level)
00080     logger = logging.getLogger(name)
00081     logger.addHandler(file_handler)
00082 
00083 def get_logger_stream_for_file(name, level = logging.DEBUG):
00084     make_logger_file(name, level)
00085     return get_logger_stream(name, level)


multi_interface_roam
Author(s): Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:18