英文:
Python logger override filename
问题
我正在遵循面向对象的设计来创建 Python 包,并且以下是我如何创建日志记录器并在类中使用的方式 -
logger.py
from logging import getLogger, config
class MyLogger():
def __init__(self, config_path):
config.fileConfig(config_path)
self.logger = getLogger()
def debug(self, message):
self.logger.debug(message)
client.py
from logger import MyLogger
class Client():
def __init__(self, logger: MyLogger):
self.logger = logger
service.py
from logger import MyLogger
class Service():
def __init__(self, logger: MyLogger):
self.logger = logger
log_config.conf
[loggers]
keys=root
[handlers]
keys=console
[formatters]
keys=std_out
[logger_root]
handlers=console
level=DEBUG
[handler_console]
class=logging.StreamHandler
level=DEBUG
formatter=std_out
[formatter_std_out]
format=%(asctime)s: %(levelname)s: %(filename)s: %(message)s
问题在于,如果我从 client.py
或 service.py
调用 MyLogger
的 debug()
方法,它会打印消息,但 filename
总是显示为 logger.py
。是否有办法覆盖 filename
内容?还是需要改变设计?
(Note: I made a minor correction in the import statement in client.py
and service.py
by changing Logger
to MyLogger
. This should match the class name in your logger.py
file.)
英文:
I am following object oriented design for the python package and here is how I have created logger and used in classes -
logger.py
from logging import getLogger, config
class MyLogger():
def __init__(self, config_path):
config.fileConfig(config_path)
self.logger = getLogger()
def debug(self, message):
self.logger.debug(message)
client.py
from logger import Logger
class Client():
def __init__(self, logger: Logger):
self.logger = logger
service.py
from logger import Logger
class Service():
def __init__(self, logger: Logger):
self.logger = logger
log_config.conf
[loggers]
keys=root
[handlers]
keys=console
[formatters]
keys=std_out
[logger_root]
handlers=console
level=DEBUG
[handler_console]
class=logging.StreamHandler
level=DEBUG
formatter=std_out
[formatter_std_out]
format=%(asctime)s: %(levelname)s: %(filename)s: %(message)s
Issue is if I call the debug()
method of MyLogger
from client.py
or service.py
it prints the message but the filename
it always shows as logger.py
. Is there a way to override the filename
content? Or does the design needs to be changed?
答案1
得分: 0
**logger.py**
from logging import getLogger, config, LoggerAdapter
class MyLogger():
def __init__(self, config_path):
config.fileConfig(config_path)
self.logger = getLogger()
def debug(self, message):
self.logger.debug(message)
def get_logger_adapter(self):
return LoggerAdapter(self.logger, {})
**client.py**
from logger import MyLogger
class Client():
def __init__(self, logger: MyLogger):
self.logger = logger.get_logger_adapter()
**service.py**
from logger import MyLogger
class Service():
def __init__(self, logger: MyLogger):
self.logger = logger.get_logger_adapter()
当你在Client或Service类中使用logger时,文件名和行号将反映调用debug()方法的正确位置
英文:
logger.py
from logging import getLogger, config, LoggerAdapter
class MyLogger():
def __init__(self, config_path):
config.fileConfig(config_path)
self.logger = getLogger()
def debug(self, message):
self.logger.debug(message)
def get_logger_adapter(self):
return LoggerAdapter(self.logger, {})
client.py
from logger import MyLogger
class Client():
def __init__(self, logger: MyLogger):
self.logger = logger.get_logger_adapter()
service.py
from logger import MyLogger
class Service():
def __init__(self, logger: MyLogger):
self.logger = logger.get_logger_adapter()
when you use logger in Client or Service class filename and linenumber will reflect the correct location from where debug() method is called
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论