Python日志记录器覆盖文件名

huangapple go评论67阅读模式
英文:

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.pyservice.py 调用 MyLoggerdebug() 方法,它会打印消息,但 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

huangapple
  • 本文由 发表于 2023年7月20日 14:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定