python中的colored logging使用示例
Posted 大海星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中的colored logging使用示例相关的知识,希望对你有一定的参考价值。
# -*- coding:utf-8 -*-
import logging
import sys
from logging.handlers import RotatingFileHandler
import os
class InfoFilter(logging.Filter):
def __init__(self, level):
logging.Filter.__init__(self)
self.level = level
def filter(self, record):
return record.levelno == self.level
class Logger(object):
@staticmethod
def get_logger(name, module_name, save_dir="./log/",
max_bytes=20 * 1024 * 1024,
backup_count=20):
if not os.path.exists(save_dir):
os.system("mkdir -p %s" % save_dir)
logger = logging.getLogger(module_name)
if not logger.handlers:
logger.setLevel(level=logging.DEBUG)
# 处理文件输出,输出所有级别
log_file = "%s/%s.log" % (save_dir, name)
formatter = logging.Formatter(
"[%(asctime)s] [%(levelname)s] [%(filename)s %(funcName)s:%(lineno)s]"
" %(message)s")
file_handler = RotatingFileHandler(
log_file, maxBytes=max_bytes, backupCount=backup_count)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# 标准输出(屏幕)
stdout_formatter = logging.Formatter(
"