我想从子类调用父类的方法,但是当我运行以下命令时,我没有在日志文件中看到日志记录条目:
python.exe B.py
如果我printA()
在代码中调用该方法,A.py
那么我会看到日志记录条目。
以下 Python 代码是A.py
文件:
#!/usr/bin/env python
import logging
import logging.config
import yaml
with open('logging.yaml', 'r') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
class A:
def __init__(self, name, value):
self.name = name
self.value = value
def printA(self):
logger.info('Name: {}'.format(self.name))
logger.info('Value: {}'.format(self.value))
B.py
文件:
#!/usr/bin/env python
from A import *
import logging
import logging.config
import yaml
with open('logging.yaml', 'r') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
class B(A):
def __init__(self, name, value):
super(B, self).__init__(name, value + 1)
b = B('Name', 1)
b.printA()
logging.yaml
文件:
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
debug_file_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: simple
filename: debug.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
root:
level: DEBUG
handlers: [console, debug_file_handler]
实际结果是一个空的日志文件。问题是我应该对源代码进行哪些更改以使日志记录功能完整?将不胜感激任何帮助。