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



我面临一个小问题,记录器第一次初始化时没有在日志消息中获取配置的时间戳格式(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记录器或默认采用UTC时间格式的uwsgi,是否有任何特定的行为?当我重新启动uwsgi进程时,它会选择log_config.yaml 中配置的正确/预期的时间戳

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

最新更新