有没有办法将日志作为参数传递?

huangapple go评论76阅读模式
英文:

python logging - there is any way to pass the log as an argument?

问题

在我的项目中,我正在使用日志模块将日志写入本地的 .log 文件,此外,我想将相同的日志传递给另一个函数,使用循环队列算法在本地记录日志。是否可以配置日志记录器来实现这个目标?谢谢。

当前的日志配置如下:

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("<SOME FORMAT>")
file_handler = logging.FileHandler('logfile.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

!! 更新:问题已解决 - @imriqwe 在这里的回答 https://stackoverflow.com/a/36408692 帮助我找到了解决方法。

英文:

in my project i am using the logging module and write the logs into a local .log file, in addition i want to pass the same log to another function to document locally the logs with circular queue algorithm.
it is possible to configure the logger to do it?
thanks .

the currnet logging config

logger=logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter=logging.Formatter(&quot;&lt;SOME FORMAT&gt;&quot;)
file_handler=logging.FileHandler(&#39;logfile.log&#39;)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

!! UPDATE : SOLVED - @imriqwe answer here https://stackoverflow.com/a/36408692 helped me to figure it.

答案1

得分: 0

这个线程 https://stackoverflow.com/questions/25187083/python-logging-to-multiple-handlers-at-different-log-levels 回答了你的问题,它展示了如何添加多个处理程序,一个文件处理程序和一个流处理程序。

英文:

I think this thread https://stackoverflow.com/questions/25187083/python-logging-to-multiple-handlers-at-different-log-levels answers your question, it shows how to add multiple handlers, one file handler and one stream handler.

huangapple
  • 本文由 发表于 2023年1月9日 18:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定