Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueuedLogHandler中针对flush的重写不应该杀掉worker线程 #203

Open
anyongjin opened this issue Nov 13, 2021 · 1 comment
Open

Comments

@anyongjin
Copy link

flush函数在python logging模块中是用于将缓存的日志立即发送到目标端的。如官方文档
我在log_handler.py文件中看到,QueuedLogHandler重写了flush,调用stop杀死了推送线程:

def flush(self):
    self.stop()

这个导致的一个问题是,如果先实例化了sls的logger,其他地方如果再次调用logging.config.***Config的话,会导致sls的logger无法发送日志:

# 配置
logging.config.fileConfig('logging.conf')
logger = logging.getLogger('sls')
# 这条日志可以记录
logger.info(f"test1 in sls: {time.time()}")

logging.config.dictConfig({'version':1})
# 这条日志不会被记录
logger.info(f"test2 in sls: {time.time()}")

我遇到的场景是:我使用fastapi+uvicorn提供http服务,启动后sls日志无法记录。
分析后发现uvicorn在初始化时内部有调用logging.config.dictConfig,里面会调用_clearExistingHandlers方法,此方法内部调用了handler的flush,sls的flush是调用stop,导致worker死掉了。

解决方法也很简单,删掉flush的重写部分就可以。

@jackgreen12345
Copy link

我们也遇到了您说的这个问题,测试环境试了一下,日志能上报进去。请教一下,您删掉flush(self)方法后,是否还遇到了其他的问题?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants