我有一个在gunicorn的帮助下运行的快速API脚本,我的访问和错误文件正在创建,但是,我想要JSON格式的日志。
我尝试以"{'remote_ip':'%(h)s','request_id':'%({X-Request-Id}i)s','response_code':'%(s)s','request_method':'%(m)s','request_path':'%(U)s','request_querystring':'%(q)s','request_timetaken':'%(D)s','response_length':'%(B)s'}"
的形式传递访问日志格式,但是日志格式在文件中没有改变。
我的gunicorn.conf.py文件
bind = '0.0.0.0:5000'
worker_class = 'uvicorn.workers.UvicornH11Worker'
loglevel = 'debug'
accesslog = '/var/log/gunicorn/access.log'
acceslogformat ="{'remote_ip':'%(h)s','request_id':'%({X-Request-Id}i)s','response_code':'%(s)s','request_method':'%(m)s','request_path':'%(U)s','request_querystring':'%(q)s','request_timetaken':'%(D)s','response_length':'%(B)s'}"
errorlog = '/var/log/gunicorn/error.log'
和我正在像 sudo gunicorn -w 5 --threads 8 -c gunicorn.conf.py main:app
一样运行我的应用程序。
--这是我的访问日志文件在运行命令之后的样子
<IP>:50506 - "GET /docs HTTP/1.1" 200
<IP>:50506 - "GET /openapi.json HTTP/1.1" 200
这是我的错误日志文件看起来像
[2022-02-16 18:28:17 +0530] [681146] [DEBUG] Current configuration:
config: gunicorn_htmltopdf.conf.py
bind: ['0.0.0.0:5000']
backlog: 2048
workers: 5
worker_class: uvicorn.workers.UvicornH11Worker
threads: 8
worker_connections: 1000
max_requests: 0
max_requests_jitter: 0
timeout: 30
graceful_timeout: 30
keepalive: 2
limit_request_line: 4094
limit_request_fields: 100
limit_request_field_size: 8190
reload: False
reload_engine: auto
reload_extra_files: []
spew: False
check_config: False
preload_app: False
sendfile: None
reuse_port: False
chdir: /home/ubuntu/API
daemon: True
raw_env: []
pidfile: None
worker_tmp_dir: None
user: 0
group: 0
umask: 0
initgroups: False
tmp_upload_dir: None
secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'}
forwarded_allow_ips: ['127.0.0.1']
accesslog: /var/log/gunicorn/access.log
disable_redirect_access_to_syslog: False
access_log_format: {'remote_ip':'%(h)s','request_id':'%({X-Request-Id}i)s','response_code':'%(s)s','request_method':'%(m)s','request_path':'%(U)s','request_querystring':'%(q)s','request_timetaken':'%(D)s','response_length':'%(B)s'}
errorlog: /var/log/gunicorn/error.log
loglevel: debug
capture_output: False
logger_class: gunicorn.glogging.Logger
logconfig: None
logconfig_dict: {}
syslog_addr: udp://localhost:514
syslog: False
syslog_prefix: None
syslog_facility: user
enable_stdio_inheritance: False
statsd_host: None
dogstatsd_tags:
statsd_prefix:
proc_name: None
default_proc_name: main:app
pythonpath: None
paste: None
on_starting: <function OnStarting.on_starting at 0x7fdc4b28b670>
on_reload: <function OnReload.on_reload at 0x7fdc4b28b790>
when_ready: <function WhenReady.when_ready at 0x7fdc4b28b8b0>
pre_fork: <function Prefork.pre_fork at 0x7fdc4b28b9d0>
post_fork: <function Postfork.post_fork at 0x7fdc4b28baf0>
post_worker_init: <function PostWorkerInit.post_worker_init at 0x7fdc4b28bc10>
worker_int: <function WorkerInt.worker_int at 0x7fdc4b28bd30>
worker_abort: <function WorkerAbort.worker_abort at 0x7fdc4b28be50>
pre_exec: <function PreExec.pre_exec at 0x7fdc4b28bf70>
pre_request: <function PreRequest.pre_request at 0x7fdc4b21e0d0>
post_request: <function PostRequest.post_request at 0x7fdc4b21e160>
child_exit: <function ChildExit.child_exit at 0x7fdc4b21e280>
worker_exit: <function WorkerExit.worker_exit at 0x7fdc4b21e3a0>
nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7fdc4b21e4c0>
on_exit: <function OnExit.on_exit at 0x7fdc4b21e5e0>
proxy_protocol: False
proxy_allow_ips: ['127.0.0.1']
keyfile: None
certfile: None
ssl_version: 2
cert_reqs: 0
ca_certs: None
suppress_ragged_eofs: True
do_handshake_on_connect: False
ciphers: None
raw_paste_global_conf: []
strip_header_spaces: False
[2022-02-16 18:28:17 +0530] [681146] [INFO] Starting gunicorn 20.0.4
[2022-02-16 18:28:17 +0530] [681146] [DEBUG] Arbiter booted
[2022-02-16 18:28:17 +0530] [681146] [INFO] Listening at: http://0.0.0.0:901 (681146)
[2022-02-16 18:28:17 +0530] [681146] [INFO] Using worker: uvicorn.workers.UvicornH11Worker
[2022-02-16 18:28:17 +0530] [681148] [INFO] Booting worker with pid: 681148
[2022-02-16 18:28:17 +0530] [681149] [INFO] Booting worker with pid: 681149
[2022-02-16 18:28:17 +0530] [681150] [INFO] Booting worker with pid: 681150
[2022-02-16 18:28:17 +0530] [681151] [INFO] Booting worker with pid: 681151
[2022-02-16 18:28:17 +0530] [681152] [INFO] Booting worker with pid: 681152
[2022-02-16 18:28:17 +0530] [681146] [DEBUG] 5 workers
[2022-02-16 18:28:18 +0530] [681148] [INFO] Started server process [681148]
[2022-02-16 18:28:18 +0530] [681148] [INFO] Waiting for application startup.
[2022-02-16 18:28:18 +0530] [681148] [INFO] Application startup complete.
[2022-02-16 18:28:19 +0530] [681149] [INFO] Started server process [681149]
[2022-02-16 18:28:19 +0530] [681149] [INFO] Waiting for application startup.
[2022-02-16 18:28:19 +0530] [681149] [INFO] Application startup complete.
[2022-02-16 18:28:19 +0530] [681150] [INFO] Started server process [681150]
[2022-02-16 18:28:19 +0530] [681150] [INFO] Waiting for application startup.
[2022-02-16 18:28:19 +0530] [681150] [INFO] Application startup complete.
[2022-02-16 18:28:19 +0530] [681151] [INFO] Started server process [681151]
[2022-02-16 18:28:19 +0530] [681151] [INFO] Waiting for application startup.
[2022-02-16 18:28:19 +0530] [681151] [INFO] Application startup complete.
[2022-02-16 18:28:19 +0530] [681152] [INFO] Started server process [681152]
[2022-02-16 18:28:19 +0530] [681152] [INFO] Waiting for application startup.
[2022-02-16 18:28:19 +0530] [681152] [INFO] Application startup complete.
如何让Gunicorn将日志作为JSON输入到文件中?
发布于 2022-02-17 07:02:36
好的,我需要从[医]古尼科恩氏集线器的这个链接中添加[医]古尼科恩氏集线器文件。并运行以下命令sudo gunicorn -w 5 --threads 8 -c gunicorn.conf.py --log-config gunicorn.conf main:app
我的gunicorn.conf.py文件看起来如下:
bind = '0.0.0.0:901'
worker_class = 'uvicorn.workers.UvicornH11Worker'
我将gunicorn.conf文件中的日志格式编辑为:
{Time:[%(asctime)s], Process:[%(process)d], LogLevel:[%(levelname)s], Module:[%(module)s], Message:[%(message)s], PathName:[%(filename)s], ThreadId:[%(thread)d], ThreadName:[%(threadName)s]}
https://stackoverflow.com/questions/71143412
复制相似问题