使用queueListeners和queueHandlers沿着文件配置进行Python日志记录

qacovj5a  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(158)

我已经有了一个自定义的Python日志脚本,我在其他模块中导入它来为每个模块创建日志记录器示例。我正在使用.ini配置文件并使用config.fileConfig()加载。现在,为了进行优化,我想使用queueHandlers和queueListeners方法为记录器创建一个非阻塞调用。
我在Python文档中发现了下面的例子,发现它很有用:

que = queue.Queue(-1)  # no limit on size
queue_handler = QueueHandler(que)
handler = logging.StreamHandler()
listener = QueueListener(que, handler)
root = logging.getLogger()
root.addHandler(queue_handler)
formatter = logging.Formatter('%(threadName)s: %(message)s')
handler.setFormatter(formatter)
listener.start()
# The log output will display the thread which generated
# the event (the main thread) rather than the internal
# thread which monitors the internal queue. This is what
# you want to happen.
root.warning('Look out!')
listener.stop()

但是,我不确定如何使用.ini配置文件重新实现上面的代码?有没有人可以指导我这个。
我的配置文件看起来像这样:logging.ini

[loggers]
keys=root,rcs_logger

[handlers]
keys=console,file

[formatters]
keys=file,console

[logger_root]
level=DEBUG
handlers=console

[logger_rcs_logger]
level=DEBUG
handlers=file,console
qualname=rcs_logger
propagate=0

[handler_console]
class=StreamHandler
level=DEBUG
formatter=console
args=(sys.stdout,)

[handler_file]
class=logging.handlers.TimedRotatingFileHandler
level=DEBUG
formatter=file
args=('%(logfilename)s', 's', 5, 0, 'utf8')

[formatter_file]
format={"observed_timestamp": %(observedTime)d, "observed_timestamp_rfc3339": "%(observedTimeRfc3339)s", "intrumentation_scope": "%(filename)s", "hostname": "%(hostname)s", "severity_text": "%(levelname)s","severity_number": %(levelno)s, "event_timestamp": %(eventTime)s, "body": "%(message)s", "resource": "%(node)s",  "span_id": "%(spanId)s", "trace_id": "%(traceId)s"}
datefmt=%s

[formatter_console]
format=[%(levelname)s][%(levelno)s] %(message)s
datefmt=

如何使用queueHandlers和queueListeners实现相同的功能?

wwtsj6pe

wwtsj6pe1#

logging.ini文件:

[loggers]
keys=root,rcs_logger

[handlers]
keys=console,file,queue

[formatters]
keys=file,console

[logger_root]
level=DEBUG
handlers=console,queue

[logger_rcs_logger]
level=DEBUG
handlers=file,console,queue
qualname=rcs_logger
propagate=0

[handler_console]
class=StreamHandler
level=DEBUG
formatter=console
args=(sys.stdout,)

[handler_file]
class=logging.handlers.TimedRotatingFileHandler
level=DEBUG
formatter=file
args=('%(logfilename)s', 's', 5, 0, 'utf8')

[handler_queue]
class=queue.QueueHandler
level=DEBUG
formatter=file
args=(queue.Queue(-1),)

[formatter_file]
format={"observed_timestamp": %(observedTime)d, "observed_timestamp_rfc3339": "%(observedTimeRfc3339)s", "intrumentation_scope": "%(filename)s", "hostname": "%(hostname)s", "severity_text": "%(levelname)s","severity_number": %(levelno)s, "event_timestamp": %(eventTime)s, "body": "%(message)s", "resource": "%(node)s",  "span_id": "%(spanId)s", "trace_id": "%(traceId)s"}
datefmt=%s

[formatter_console]
format=[%(levelname)s][%(levelno)s] %(message)s
datefmt=

现在,只需创建一个QueueListener示例并将其添加到日志记录器的处理程序中:

main.py文件:

import logging
import queue

que = queue.Queue(-1)
queue_handler = logging.handlers.QueueHandler(que)

listener = logging.handlers.QueueListener(que, *logging.getLogger().handlers)
listener.start()

# Now you can log messages using the logger as usual
logger = logging.getLogger(__name__)
logger.info("This is a tayst message")

listener.stop()

相关问题