Python日志查询

Python日志查询,python,python-3.x,logging,Python,Python 3.x,Logging,我在Python3.7.x中遇到了一个日志问题 #logging_config_yaml_file version: 1 formatters: simple: format: "%(asctime)s || %(name)s || %(levelname)s || %(message)s" handlers: console: # configuration for handler with id 'console' goes here class

我在Python3.7.x中遇到了一个日志问题

#logging_config_yaml_file

version: 1
formatters:
  simple:
    format: "%(asctime)s || %(name)s || %(levelname)s || %(message)s"
handlers:
  console: # configuration for handler with id 'console' goes here
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
  info_file_handler: # configuration for handler with id 'info_file_handler' goes here
    class: logging.handlers.RotatingFileHandler
    level: DEBUG
    formatter: simple
    filename: ./logs/db_update_info.log
    maxBytes: 10485760 # 10MB
    backupCount: 20
    encoding: utf8
  rotate_file_handler2: # configuration for handler with id 'rotate_file_handler2' goes here
     class: logging.handlers.RotatingFileHandler
     level: DEBUG
     formatter: simple
     filename: ./logs/app_search_info.log
     maxBytes: 10485760 # 10MB
     backupCount: 20
     encoding: utf8
  rotate_file_handler3: # configuration for handler with id 'rotate_file_handler3' goes here
     class: logging.handlers.RotatingFileHandler
     level: DEBUG
     formatter: simple
     filename: ./logs/query_search_info.log
     maxBytes: 10485760 # 10MB
     backupCount: 20
     encoding: utf8
loggers:
  Trie_db:
    level: DEBUG
    handlers: [info_file_handler]
    propagate: False
  Trie_db.insert:
    level: DEBUG
    handlers: [rotate_file_handler2]
    propagate: False
  Trie_db.search:
    level: DEBUG
    handlers: [rotate_file_handler3]
    propagate: False
root:
  level: DEBUG
  handlers: [console]
这是我的
日志yaml config
文件,我使用它们如下:

self.logger = logging.getLogger('Trie_db')
self.insertLogger = logging.getLogger('Trie_db.insert')
#sample line used to log is : t.insertLogger.debug(f'Insert used...blah blah')
self.searchLogger = logging.getLogger('Trie_db.search')
#sample line used to log is : t.searchLogger.info(f'Searched for...blah blah')
到目前为止一切都很好。问题来了:它只创建了两个日志文件,名为:

  • 创建此db\u update\u info.log(信息文件处理程序中的一个)并
  • 创建此应用程序搜索信息.log(一个在rotate\u文件\u handler2中) 及
  • 甚至不创建第三个文件(query\u search\u info.log)(一个在rotate\u文件\u handler3中)
相反,当使用第三个(
.searchLogger
)时,它将日志消息写入
db\u update\u info.log
中的文件,而不是我在
rotating\u file\u handler3
中指定的文件,即
query\u search\u info.log


为什么会发生这种情况?

很难在注释中发布代码,所以我将在这里发布我的示例,也许您可以发现代码的不同之处。我将日志配置放入名为
logconfig.yml
的文件中,然后运行以下代码:

import logging
import logging.config
import yaml

logger = logging.getLogger('Trie_db')
insertLogger = logging.getLogger('Trie_db.insert')
searchLogger = logging.getLogger('Trie_db.search')

with open('logconfig.yml', 'r') as f:
    log_cfg = yaml.safe_load(f.read())

logging.config.dictConfig(log_cfg)

# I log to each logger at three different priorities just to make sure
# nothing is getting filtered.
for l in [logger, insertLogger, searchLogger]:
    l.debug('This is a debug message')
    l.info('This is an info message')
    l.warning('This is a warning message')
这将在我的
日志
目录中生成三个文件:

$ ls logs
app_search_info.log  db_update_info.log  query_search_info.log
其中包含预期信息:

$ cat logs/app_search_info.log
2019-11-23 23:12:02,273 || Trie_db.insert || DEBUG || This is a debug message
2019-11-23 23:12:02,273 || Trie_db.insert || INFO || This is an info message
2019-11-23 23:12:02,273 || Trie_db.insert || WARNING || This is a warning message
$ cat logs/db_update_info.log
2019-11-23 23:12:02,273 || Trie_db || DEBUG || This is a debug message
2019-11-23 23:12:02,273 || Trie_db || INFO || This is an info message
2019-11-23 23:12:02,273 || Trie_db || WARNING || This is a warning message
$ cat logs/query_search_info.log
2019-11-23 23:12:02,273 || Trie_db.search || DEBUG || This is a debug message
2019-11-23 23:12:02,273 || Trie_db.search || INFO || This is an info message
2019-11-23 23:12:02,273 || Trie_db.search || WARNING || This is a warning message

当我尝试它时,它似乎起作用了。你能发布一个可运行的例子来说明这个问题吗?我必须编一个例子,因为我的工作是使用相同的配置,我想知道代码中是否有我们看不到的东西…谢谢,我会尝试做同样的事情,因为版权问题不确定。您不需要共享您的实际代码。只是在运行时重现问题的示例代码。好的,我将尝试创建它并重新更新!谢谢。哦,糟了!我怎么会这么傻:(它在工作,只是我把相同的文件名弄乱了,编辑了不同的yml,使用了不同的yml!谢谢你的帮助:)