1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import logging, logging.handlers import config import inspect
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' LOG_LEVEL = logging.DEBUG logger = {} handler = logging.handlers.RotatingFileHandler( config.PATH_LOG_FILE, maxBytes = 1048576 * 2, backupCount = 5, ) handler.setFormatter(logging.Formatter(FORMAT))
__all__ = ['Log', 'LogI', 'LogE', 'LogD', 'LogC', 'LogW'] def Log(name): if inspect.isclass(type(name)): name = name.__class__.__name__ elif inspect.isclass(type(name)): name = name.__name__ else: name = name or "no_name" if name in logger: return logger[name] else: l = logging.getLogger(name) l.setLevel(LOG_LEVEL) l.addHandler(handler) logger[name] = l return l
def LogI(name, log): return Log(name).info(log)
def LogE(name, log): return Log(name).error(log)
def LogD(name, log): return Log(name).debug(log)
def LogC(name, log): return Log(name).critical(log)
def LogW(name, log): return Log(name).warning(log)
|