1 #!/usr/local/python/bin 2 # coding=utf-8 3 4 '''Implements a simple log library. 5 6 This module is a simple encapsulation of logging module to provide a more 7 convenient interface to write log. The log will both print to stdout and 8 write to log file. It provides a more flexible way to set the log actions, 9 and also very simple. See examples showed below: 10 11 Example 1: Use default settings 12 13 import log 14 15 log.debug('hello, world') 16 log.info('hello, world') 17 log.error('hello, world') 18 log.critical('hello, world') 19 20 Result: 21 Print all log messages to file, and only print log whose level is greater 22 than ERROR to stdout. The log file is located in '/tmp/xxx.log' if the module 23 name is xxx.py. The default log file handler is size-rotated, if the log 24 file's size is greater than 20M, then it will be rotated. 25 26 Example 2: Use set_logger to change settings 27 28 # Change limit size in bytes of default rotating action 29 log.set_logger(limit = 10240) # 10M 30 31 # Use time-rotated file handler, each day has a different log file, see 32 # logging.handlers.TimedRotatingFileHandler for more help about 'when' 33 log.set_logger(when = 'D', limit = 1) 34 35 # Use normal file handler (not rotated) 36 log.set_logger(backup_count = 0) 37 38 # File log level set to INFO, and stdout log level set to DEBUG 39 log.set_logger(level = 'DEBUG:INFO') 40 41 # Both log level set to INFO 42 log.set_logger(level = 'INFO') 43 44 # Change default log file name and log mode 45 log.set_logger(filename = 'yyy.log', mode = 'w') 46 47 # Change default log formatter 48 log.set_logger(fmt = '[%(levelname)s] %(message)s' 49 ''' 50 51 __author__ = "tuantuan.lv <dangoakachan@foxmail.com>" 52 __status__ = "Development" 53 54 __all__ = ['set_logger', 'debug', 'info', 'warning', 'error', 55 'critical', 'exception'] 56 57 import os 58 import sys 59 import logging 60 import logging.handlers 61 62 # Color escape string 63 COLOR_RED='