I am using Python 3, and I am learning how to use logging. I am looking at the code from https://docs.python.org/3/howto/logging-cookbook.html and https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/.
I tried to modify part of the first two code blocks in the first link, the main module and the auxiliary module, to use a JSON file. But when I ran the main file, I get certain log outputs repeated 3 times, but I don't know why, or what to change so that lines aren't repeated but are still outputted to the same .log file.
.log file: .log file output
My JSON file:
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
}
},
"handlers": {
"debug_file_handler": {
"class": "logging.FileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "debug.log",
"encoding": "utf8"
}
},
"loggers": {
"spam_application.auxiliary.Auxiliary": {
"level": "DEBUG",
"handlers": ["debug_file_handler"]
},
"spam_application.auxiliary": {
"level": "DEBUG",
"handlers": ["debug_file_handler"]
}
},
"root": {
"level": "DEBUG",
"handlers": ["debug_file_handler"]
}}
and for the main file:
import auxiliary_module
import os
import json
import logging.config
with open('python_logging_configuration.json', 'r') as logging_configuration_file:
config_dict = json.load(logging_configuration_file)
logging.config.dictConfig(config_dict)
logger = logging.getLogger(__name__)
logger.info('creating an instance of auxiliary_module.Auxiliary')
a = auxiliary_module.Auxiliary()
logger.info('created an instance of auxiliary_module.Auxiliary')
logger.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
logger.info('finished auxiliary_module.Auxiliary.do_something')
logger.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
logger.info('done with auxiliary_module.some_function()')
And for the auxiliary_module file
module_logger = logging.getLogger('spam_application.auxiliary')
class Auxiliary:
def __init__(self):
self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')
self.logger.info('creating an instance of Auxiliary')
self.logger.debug('debug in Auxiliary')
def do_something(self):
self.logger.info('doing something')
a = 1 + 1
self.logger.info('done doing something')
def some_function():
module_logger.info('received a call to "some_function"')
Thanks in advance