import yaml, os
import logging.config
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""load logging.yaml file or use system environment variable 'LOG_CFG' to 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 = yaml.safe_load(f.read())
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
def logger_decorate(cls: type):
"""logger decorator for each class"""
# setup logging
setup_logging()
aname = '_{}__log'.format(cls.__name__)
setattr(cls, aname, logging.getLogger(cls.__module__ + '.' + cls.__name__))
return cls
@logger_decorate
class Test(object):
def __init__(self, name):
self.name = name
def sayHello(self):
self.__log.info('sayHello called')
print("hello", self.name)
def test(self):
self.__log.info('test called')
getattr(self, 'sayHello')()
test = Test("john")
test.test()
напечатает
2018-09-28 00: 30: 51,881 - main .Test - INFO - тест называется
2018-09-28 00: 30: 51,882 - main .Test - INFO - sayHello позвонил
hello john
, но в vscode будет ошибка компиляции,кто-нибудь знает как это исправить?
![enter image description here](https://i.stack.imgur.com/D21GH.png)