gpt4 book ai didi

python - 名称错误 : global name 'logger' is not defined

转载 作者:太空狗 更新时间:2023-10-30 00:24:48 30 4
gpt4 key购买 nike

我写了一个 logging.json 文件,从那里设置了日志记录配置,之后我创建了一个类,我的所有方法都将在其中,但是当我在类中使用 logger 时,它会抛出错误 NameError:未定义全局名称“logger”

app.py

#/usr/bin/python

import sys
import logging
import time
import json
import os
import logging.config

def setup_logging(default_path='logging.json',default_level=logging.INFO,env_key='LOG_CFG'):
"""Setup logging configuration"""
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)

class Generic:

def __init__(self , file_path):
self.file_path = file_path

def check_mime(self):
logger.info('Generic on file {} starts at {}'.format(file_path , time.time()))


print 'File path is {}'.format(sys.argv[1])
file_path = sys.argv[1]

def parser():
parser = Generic(file_path)
parser.check_mime()
def main():
print 'This is intended for module purpose only'
setup_logging()
parser()

if __name__ == '__main__':
main()

logging.json

{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
}
},

"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "simple",
"stream": "ext://sys.stdout"
},

"info_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "simple",
"filename": "logs/gp.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
},

"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "simple",
"filename": "logs/errors.log",
"maxBytes": 10485760,
"backupCount": 20,
"encoding": "utf8"
}
},

"loggers": {
"my_module": {
"level": "ERROR",
"handlers": ["console"],
"propagate": "no"
}
},

"root": {
"level": "INFO",
"handlers": ["console", "info_file_handler", "error_file_handler"]
}
}

问题:

当我运行程序时出错

$ python app.py /home/default/domain.txt
File path is /home/default/domain.txt
This is intended for module purpose only
Traceback (most recent call last):
File "app.py", line 44, in <module>
main()
File "app.py", line 41, in main
parser()
File "app.py", line 37, in parser
parser.check_mime()
File "app.py", line 29, in check_mime
logger.info('GenericParser
NameError: global name 'logger' is not defined

我正在使用此链接中的日志记录示例 [ https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/ ]

关于如何解决这个问题的任何建议,因为记录器不是全局性的,有什么办法让它成为全局性的。?

最佳答案

您链接到的示例有:

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) #<<<<<<<<<<<<<<<<<<<<

您错过了 logger 定义。

您可以将 self.logger = logging.getLogger(__name__) 放入您的 Generic.__init__() 函数中,或者定义一个全局 logger 如示例所示,在导入之后。

关于python - 名称错误 : global name 'logger' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058121/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com