Python 记录器未选取配置时间格式。

Python logger not picking up the configure time format.

我遇到了一个奇怪的问题,记录器在第一次初始化时没有在日志消息中选择配置的时间戳格式 (ascii)。它默认以 UTC 格式打印日志时间格式,不知道为什么。

下面的片段来自 /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 记录器或默认采用 UTC 时间格式的 uwsgi 是否有任何特定行为?当我重新启动我的 uwsgi 进程时,它会选择在 log_config.yaml

中配置的 correct/expected 时间戳

我假设 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'
)