我遇到了一个奇怪的问题,记录器第一次初始化时没有拾取日志消息中配置的时间戳格式(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 记录器或 uwsgi 是否有任何特定行为,默认情况下采用 UTC 时间格式?当我重新启动 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'
)
关于Python 记录器未选择配置时间格式。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053273/