我已经有了一个自定义的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实现相同的功能?
1条答案
按热度按时间wwtsj6pe1#
logging.ini
文件:现在,只需创建一个
QueueListener
示例并将其添加到日志记录器的处理程序中:main.py
文件: