0

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!

4
  • Your question is incoherent. What is dictConfig? It seems like you're referring to logging.config.dictConfig() but that is not at all obvious from our question. Also, you call dictConfig(d) outside the block that defines d. Commented Sep 13, 2022 at 3:23
  • @KurtisRader, thanks for the feedback. Have just tried specifying where the dictConfig comes from. What edit do you suggest in the code please? Commented Sep 13, 2022 at 3:27
  • You don't seem to understand variable scoping. As I mentioned in my previous comment you define the variable d in a scope not accessible to where you use it in your call to dictConfig(). That should be a compilation error so it appears your example code is not representative of your actual code. Commented Sep 14, 2022 at 4:13
  • @KurtisRader, okay, let me try that and see - but I didn't see any compilation error on this one. I am really new to this - so quite unsure about this piece of code. Thanks for your help! Commented Sep 14, 2022 at 10:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.