我正在尝试为我的模块设置日志记录:
使用了此处托管的完全相同的代码段: https://gist.github.com/kingspp/9451566a5555fb022215ca2b7b802f19
日志.yaml:
info_file_handler:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: standard
filename: /tmp/info.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
设置\u logging.py
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
try:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
我在server.py中启动此操作: moto/服务器.py
setup_logging()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("Logger SETUP successfully")
这会将输出适当地记录到/tmp/info.log中,但是当我在子模块的另一个文件中初始化相同的文件时,我没有看到日志写入该文件:
moto/ec2/型号.py
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
def describe_internet_gateways(
self,
internet_gateway_ids=None,
filters=None):
igws = []
#logger = logging.getLogger(__name__)
#logger.setLevel(logging.DEBUG)
print("\n\n\n\n\n{}\n\n\n\n\n".format(__name__))
logger.info("in describe_internet_gateways")
session = get_db_session()
igws = session.query(InternetGateway_t)
但是,如果我在方法内部初始化记录器(如果我取消对上述方法内部的注释部分的注释),那么日志将适当地写入/tmp/info.log文件中
有人能帮我什么我错过了,如何初始化日志只有一次,并使用它跨模块
阅读并尝试了python文档中的日志模块解释,但我仍然在讨论这个问题
编辑-1: 通过执行以下编辑,通过@olinox14尝试以下解决方案:
日志记录.json
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "standard",
"stream": "ext://sys.stdout"
},
"info_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "standard",
"filename": "/tmp/info.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "error",
"filename": "/tmp/errors.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
"debug_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "standard",
"filename": "/tmp/debug.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
"critical_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "CRITICAL",
"formatter": "standard",
"filename": "/tmp/critical.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},
"warn_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "WARN",
"formatter": "standard",
"filename": "/tmp/warn.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
}
},
"root": {
"level": "NOTSET",
"handlers": [
"console",
],
"propagate": true
},
"loggers": {
"test": {
"level": "DEBUG",
"handlers": [
"console",
"info_file_handler",
"error_file_handler",
"critical_file_handler",
"debug_file_handler",
"warn_file_handler"
],
"propagate": "no"
}
}
}
但现在我看到控制台上的日志,但没有在文件中记录
基本上,命名的记录器是单例的。每次调用新名称时都会设置一个新的记录器,如果调用以前使用的名称,则会检索现有的记录器
这里有一个解决方案:
日志.yaml:
moto/服务器.py
moto/ec2/型号.py
相关问题 更多 >
编程相关推荐