You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Docker日志格式?仅保留时分秒(无日期与时区)

修改Docker中Gunicorn日志时间格式的方法

你的日志里的时间是由Gunicorn自身输出的,并非Docker日志驱动自动添加,要调整为仅保留时分秒,可通过以下两种方式处理:

方法1:启动命令直接指定自定义日志格式

在启动Gunicorn的命令中,通过参数指定日志格式,将时间部分限制为时分秒:

gunicorn --log-config - <<EOF
[loggers]
keys=root,gunicorn.error,gunicorn.access

[handlers]
keys=console

[formatters]
keys=custom

[logger_root]
level=INFO
handlers=console

[logger_gunicorn.error]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.error

[logger_gunicorn.access]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.access

[handler_console]
class=StreamHandler
formatter=custom
args=(sys.stdout,)

[formatter_custom]
format=[%(asctime)s] [%(process)d] [%(levelname)s] %(message)s
datefmt=%H:%M:%S
EOF
your_app:app

方法2:通过配置文件设置

创建gunicorn.conf.py配置文件,内容如下:

import logging
from logging import Formatter

# 定义仅含时分秒的日志格式
custom_formatter = Formatter(
    '[%(asctime)s] [%(process)d] [%(levelname)s] %(message)s',
    datefmt='%H:%M:%S'
)

# 配置错误日志
errorlog = '-'
for handler in logging.getLogger('gunicorn.error').handlers:
    handler.setFormatter(custom_formatter)

# 配置访问日志
accesslog = '-'
for handler in logging.getLogger('gunicorn.access').handlers:
    handler.setFormatter(custom_formatter)

启动Gunicorn时指定该配置文件:

gunicorn --config gunicorn.conf.py your_app:app

额外说明

如果是Docker日志驱动自动添加的前缀时间(而非应用自身输出),可修改Docker配置文件/etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "timestamp-format": "%H:%M:%S"
  }
}

修改后重启Docker服务:

systemctl restart docker

内容的提问来源于stack exchange,提问作者Harm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:14:56