0

I have a loop. For each pass of the loop I want to change the name of the logging logfile. The below code takes the first name assigned to the logfile and for each pass of the loop puts all the data for each iteration into this one file:

import logging

global sub_dict

sub_dict = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight'
}


def test_func():

    for key, value in sub_dict.items():

        log_path = ''.join(('C:\\Users\\Oleg Salenko\\AppData\\Local\\WebGrab+Plus\\Addons\\Log Files\\', str(key), '-', str(value), '.log'))

        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :: %(levelname)s :: %(message)s', filename=log_path, filemode='w')
        logging.captureWarnings(True)
        logger = logging.getLogger(__name__)


        try:

            logging.info('{}'.format('This is an example...'))
            logging.info('{} {}'.format(key, value))
            logging.info('{}'.format('Of why concurrent logging into a single file...'))
            logging.info('{}'.format('Is a complete an utter mess...'))


        except Exception as exc:

            print('%r generated an exception: %s' % (new_dict2, exc))


    return key, value


test_func()

...what do I need to amend so that each time the loop passes, the logfile name is reassigned correctly?

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107

1 Answers1

0

If I understood correctly, you want to create a new log file on each loop iteration. If that is the case I think this question may be useful for you.

anbarquer
  • 178
  • 1
  • 2
  • 8
  • hi - after various trial and error, as well as reading of the logging docs, i think i have achieved the desired behaviour in my actual code. as soon as I have verified, I will port to my simple code in the question and provide a tested answer. some of the examples on SO for similar questions look like overkill to me... – gdogg371 May 05 '20 at 16:40