Python记录器没有提取配置时间格式。

时间:2015-12-02 21:24:02

标签: python logging yaml uwsgi

我面临一个奇怪的问题,即记录器第一次初始化时没有在日志消息中获取配置的时间戳格式(ascii)。它默认以UTC格式打印日志时间格式,不知道原因。

snip来自/proj/req_proc.py python代码,uwsgi启动,初始化记录器。 log_config.yaml包含一个格式化程序定义,用于以ascii格式打印时间戳。

def setup_logging(default_path='=log_config.yaml',
                  default_level=logging.INFO):

    path = default_path
    if os.path.exists(path):
        with open(path, 'rt') as f:
        config = yaml.load(f.read())
        logging.config.dictConfig(config)

以下是我的启动脚本中启动uwsgi进程的剪辑。

uwsgi -M --processes 1 --threads 2 -s /tmp/uwsgi.sock --wsgi-file=/proj/req_proc.py --daemonize /dev/null

对于python logger或uwsgi是否有任何特定行为,默认情况下会选择UTC时间格式?当我重新启动我的uwsgi进程时,它会选择log_config.yaml中配置的正确/预期时间戳

1 个答案:

答案 0 :(得分:2)

我假设uwsgi模块以某种方式劫持了Python的logging模块。设置日志级别,记录器名称和日志记录本身是有效的,但尝试修改格式,即使是基本类似的东西:

logging.basicConfig(level=logging.NOTSET, format='[%(process)-5d:%(threadName)-10s] %(name)-25s: %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)

没有效果。

更新:以下是覆盖uWSGI默认记录器的方法:

# remove uUWSGI's default logging configuration, this can be removed in
# more recent versions of uWSGI
root = logging.getLogger()
map(root.removeHandler, root.handlers[:])
map(root.removeFilter, root.filters[:])

logger = logging.getLogger(__name__)
logging.basicConfig(
    level=logging.INFO,
    format='%(levelname)-8s %(asctime)-15s %(process)4d:%(threadName)-11s %(name)s %(message)s'
)