Home > Enterprise >  Why is my error not being written to my logfile?
Why is my error not being written to my logfile?

Time:09-16

I have an existing program (a python executable) but when I run it, it sometimes stops for no reason. So to try and see where the problem arises I aimed to add the logging concept to the python application so I could exactly see what and when a problem arises.

Below is my code, the file log.txt is being created which is good. Now for testing I created an error on purpose by changing the password of the database. Now from experience I know that it should give me an mysql.connector.errors error, which it does in my terminal. But when I open the logfile.txt I get an empty file...

My aim is to catch all different types of errors thats why I use the Exception method.

What I expected to see in log.txt was for example:

2021-09-16 09:04:29,827 ERROR __main__ 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

This is my code:

logging.basicConfig(filename='log.text', level=logging.DEBUG, 
                    format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)


def main():
    mp_list = get_mp_list() #<-- Get the list created 
    pool = Pool(min(cpu_count(), len(mp_list))) #<-- create a pool
    results = pool.imap_unordered(process_mp, mp_list) #<-- Create the pool.imap object
    while True:
        try:
            result = next(results) #<-- run the program
        except StopIteration:
            break
        except Exception as err: #<-- write to the logfile in  case of error
            logger.error(err)
            raise


if __name__ == '__main__':
    main()

CodePudding user response:

You seem to be using multiprocessing, and if multiple processes log to the same file, it might not work - for the reasons described in the logging documentation. Basically, you need to use a different strategy to ensure that only one process writes to a particular file. It's too long to reproduce the details here - refer to the linked info for details.

CodePudding user response:

Add filemode it should work then

logging.basicConfig(filename='log.text', level=logging.DEBUG, filemode="w",
                    format='%(asctime)s %(levelname)s %(name)s %(message)s')
  • Related