logging configs overwritten by others
this is my module:
# module.py
import logging
logging.basicConfig(filename='app.log')
logger = logging.getLogger(__name__)
def method(x):
logger.info('x is %s', x)
return x + x
and my module is used by a third party script, the problem is that it did
some logging setups which overwrite mine.
# main.py
import logging
from logging.config import dictConfig
from module import method
logger = logging.getLogger(__name__)
def setup_loghandlers():
dictConfig({
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler"
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
}
})
setup_loghandlers()
y = method(20)
logger.info('y is %s', y)
now if I run main.py, I get all the logs in console, while what I actually
need is module.py's logs go to app.log file
PS: I can't modify main.py, I can only modify my module.py
No comments:
Post a Comment