Folks,
I want to just have a runlog of whether everything has run as per expectation in the Module that's been written and I made use of logging (and dictConfig) for that purpose. I can see the files being generated but they're empty!
It's important to note that this is a big project so am having to log every module.
The code in the main module is:
from logging.config import dictConfig
if config['system']['log_ind'] == 1:
d = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'detailed': {
'format': '%(asctime)s-%(levelname)s-%(name)s-%(process)d::%(module)s|%(lineno)s:: %(message)s'
}
},
'handlers': {
'info_file': {
'class': 'logging.FileHandler',
'level':'INFO',
'formatter': 'detailed',
'filename': 'test.log',
'mode': 'w',
},
'errors_file': {
'class': 'logging.FileHandler',
'filename': 'errors.log',
'mode': 'w',
'level': 'ERROR',
'formatter': 'detailed',
},
},
'loggers': {
' ': {
'handlers': ['info_file','errors_file'],
'level':'DEBUG',
'propagate':False
}
},
'mypackage': {
'level': 'DEBUG',
'handlers': ['debug_console']
},
}
dictConfig(d)
logger=logging.getLogger(__name__)
def starting_main_mod(thisfile):
logger.debug("starting the main module")
try:
logger.debug("main pid:", os.getpid())
logger.debug("start time: ",time.strftime("%H:%M:%S",time.localtime()))
logger.debug("available worker: ", worker_count)
logger.debug("end time: ",time.strftime("%H:%M:%S",time.localtime()), time.time()-start_time)
except:
logger.debug("could not write file %s to destination", thisfile)
finally:
logger.debug("the function is done for the file %s", thisfile)
Essentially I want all the lines with the time and available workers information to be printed out in the log file. Below is the code in one of the modules that this main module calls:
logger = logging.getLogger(__name__)
def result_log(thisfile):
try:
path=Path("C:/Users/result.db")
if path.is_file():
logger.debug("the result was sucessfully saved as result.db")
except OSError as e:
logger.error(e)
logger.error(e, exc_info = True)
I am using Python 3.9.7. Don't see any error messages, just that the logs are empty!
I am new to Python so wanted to understand what I could be doing wrong here. Many thanks in advance for your help!
dictConfig? It seems like you're referring tologging.config.dictConfig()but that is not at all obvious from our question. Also, you calldictConfig(d)outside the block that definesd.din a scope not accessible to where you use it in your call todictConfig(). That should be a compilation error so it appears your example code is not representative of your actual code.