记录:找不到处理程序
Logging: can't find Handlers
我试图在我的脚本末尾关闭所有处理程序,但由于某种原因 logger.handlers 是一个空列表,处理程序应该在其中,因此我无法关闭它们。有人可以解释我如何找到并关闭我的处理程序吗?
import logging
from logging.config import dictConfig
logconfig = {
"version": 1,
"disable_existing_loggers": 0,
"root": {
"level": "DEBUG",
"handlers": [
"console",
"file",
"debugfile"
]
},
"loggers": {
},
"formatters": {
"precise": {
"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"
},
"brief": {
# Don't think we'll use.
"format": "%(levelname)-8s: %(name)-15s: %(message)s"
}
},
"handlers": {
"debugfile": {
"class": "logging.FileHandler",
"formatter": "precise",
"level": "DEBUG",
"mode": "a",
"filename": "debugAndUp.log"
},
"console": {
"class": "logging.StreamHandler",
"formatter": "precise",
"level": "DEBUG",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "precise",
"level": "WARNING",
"backupCount": 3,
"maxBytes": 10240000,
"filename": "warningAndUp.log"
}
}
}
dictConfig(logconfig)
logger = logging.getLogger(__name__)
logger.debug('Test')
logger.handlers >> []
您只想删除 __name__
logging.getLogger(__name__)
,因为它指的是 main 并且此名称中没有处理程序。处理程序在默认 "root" 处理程序下注册。它会给你访问权限。
import logging
from logging.config import dictConfig
logconfig = {
"version": 1,
"disable_existing_loggers": 0,
"root": {
"level": "DEBUG",
"handlers": [
"console",
"file",
"debugfile"
]
},
"loggers": {
},
"formatters": {
"precise": {
"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"
},
"brief": {
# Don't think we'll use.
"format": "%(levelname)-8s: %(name)-15s: %(message)s"
}
},
"handlers": {
"debugfile": {
"class": "logging.FileHandler",
"formatter": "precise",
"level": "DEBUG",
"mode": "a",
"filename": "debugAndUp.log"
},
"console": {
"class": "logging.StreamHandler",
"formatter": "precise",
"level": "DEBUG",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "precise",
"level": "WARNING",
"backupCount": 3,
"maxBytes": 10240000,
"filename": "warningAndUp.log"
}
}
}
logs = dictConfig(logconfig)
logger = logging.getLogger()
logger.debug('Test')
for handles in logger.handlers:
print(handles.name)
handles.close()
输出 -> 处理程序列表:
console
file
debugfile
我试图在我的脚本末尾关闭所有处理程序,但由于某种原因 logger.handlers 是一个空列表,处理程序应该在其中,因此我无法关闭它们。有人可以解释我如何找到并关闭我的处理程序吗?
import logging
from logging.config import dictConfig
logconfig = {
"version": 1,
"disable_existing_loggers": 0,
"root": {
"level": "DEBUG",
"handlers": [
"console",
"file",
"debugfile"
]
},
"loggers": {
},
"formatters": {
"precise": {
"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"
},
"brief": {
# Don't think we'll use.
"format": "%(levelname)-8s: %(name)-15s: %(message)s"
}
},
"handlers": {
"debugfile": {
"class": "logging.FileHandler",
"formatter": "precise",
"level": "DEBUG",
"mode": "a",
"filename": "debugAndUp.log"
},
"console": {
"class": "logging.StreamHandler",
"formatter": "precise",
"level": "DEBUG",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "precise",
"level": "WARNING",
"backupCount": 3,
"maxBytes": 10240000,
"filename": "warningAndUp.log"
}
}
}
dictConfig(logconfig)
logger = logging.getLogger(__name__)
logger.debug('Test')
logger.handlers >> []
您只想删除 __name__
logging.getLogger(__name__)
,因为它指的是 main 并且此名称中没有处理程序。处理程序在默认 "root" 处理程序下注册。它会给你访问权限。
import logging
from logging.config import dictConfig
logconfig = {
"version": 1,
"disable_existing_loggers": 0,
"root": {
"level": "DEBUG",
"handlers": [
"console",
"file",
"debugfile"
]
},
"loggers": {
},
"formatters": {
"precise": {
"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"
},
"brief": {
# Don't think we'll use.
"format": "%(levelname)-8s: %(name)-15s: %(message)s"
}
},
"handlers": {
"debugfile": {
"class": "logging.FileHandler",
"formatter": "precise",
"level": "DEBUG",
"mode": "a",
"filename": "debugAndUp.log"
},
"console": {
"class": "logging.StreamHandler",
"formatter": "precise",
"level": "DEBUG",
"stream": "ext://sys.stdout"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "precise",
"level": "WARNING",
"backupCount": 3,
"maxBytes": 10240000,
"filename": "warningAndUp.log"
}
}
}
logs = dictConfig(logconfig)
logger = logging.getLogger()
logger.debug('Test')
for handles in logger.handlers:
print(handles.name)
handles.close()
输出 -> 处理程序列表:
console
file
debugfile