jitanghu
11/16/2017 - 4:24 AM

python日志设置

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M:%S')
def setup_logger():
    """
    设置Logger,默认输出到console和wisteria-v3-upgrade.log
    """
    root_logger = logging.getLogger()
    stream_handler = logging.StreamHandler()
    timestamp = datetime.now().strftime('%Y%m%d%H%M%s')
    file_handler = logging.FileHandler('wisteria-v3-upgrade-{}.log'.format(timestamp))
    formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    stream_handler.setFormatter(formatter)
    file_handler.setFormatter(formatter)

    root_logger.addHandler(stream_handler)
    root_logger.addHandler(file_handler)
    root_logger.setLevel(logging.DEBUG)