简体   繁体   English

uvicorn 抑制 gelf 驱动程序的 python 系统日志

[英]uvicorn suppresses python's syslog for gelf driver

I have a docker container running logging with gelf to a logging instance via udp -- all fine!我有一个docker 容器,通过 udp 使用gelf运行日志记录到日志记录实例——一切都很好!

The container is based on Ubuntu 18 where rsyslog is running as a service, which works well.该容器基于Ubuntu 18 ,其中rsyslog作为服务运行,运行良好。

Inside the container is a FastAPI application running with uvicorn webserver.容器内部是一个与uvicorn网络服务器一起运行的FastAPI 应用程序 It also works perfectly and uvicorn is perfectly logging to the logging instance.它也可以完美地工作并且 uvicorn 可以完美地记录到日志记录实例。

Here comes what is not working, but usually works with non-FastAPI python projects.这是不起作用的,但通常适用于非 FastAPI python 项目。 I use python's syslog to log more stuff.我使用 python 的系统日志来记录更多的东西。

The app with syslog looks like this (I created an easy example to debug for myself):带有系统日志的应用程序如下所示(我创建了一个简单的示例来为自己调试):

from fastapi import FastAPI
import syslog

syslog.openlog(facility=syslog.LOG_LOCAL0)

app = FastAPI()

syslog.syslog(syslog.LOG_INFO, 'startup done')

@app.get("/")
async def root():
    syslog.syslog(syslog.LOG_INFO, 'get hello')
    return {"message": "Hello World"}

The logs at the logging instance don't show the syslog messages.日志记录实例中的日志不显示系统日志消息。 Only uvicorn's messages:只有 uvicorn 的消息:

INFO:     Started server process [21]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
INFO:     172.17.0.1:35346 - "GET / HTTP/1.1" 200 OK

For further debugging I checked rsyslog's log file, and it contains the syslog messages:为了进一步调试,我检查了 rsyslog 的日志文件,它包含系统日志消息:

Dec 23 17:21:39 /uvicorn: startup done
Dec 23 17:21:50 /uvicorn: get hello

and here is the rsyslog configuration at /etc/rsyslog.d这是 /etc/rsyslog.d 中的 rsyslog 配置

local0.* {
   action(type="omfile" file="/var/log/test.log" fileOwner="syslog" fileGroup="syslog" fileCreateMode="0640")
   stop
}

What am I missing here?我在这里错过了什么? Why is gelf ignoring rsyslog?为什么 gelf 忽略 rsyslog? What do I need to understand about uvicorn concerning syslog?关于系统日志,我需要了解有关 uvicorn 的哪些内容? or what can I do?或者我能做什么?

Thanks谢谢

The problem results from gelf ignoring syslog.问题是由 gelf 忽略 syslog 引起的。 A simple print in python will be recognized though.不过,可以识别 python 中的简单打印。

Solution: I built myself a log function in python which will log to syslog and print out whatever I want to log.解决方案:我在 python 中为自己建立了一个日志 function,它将记录到系统日志并打印出我想要记录的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM