0

我有一个循环。对于循环的每一次通过,我都想更改日志记录文件的名称。下面的代码采用分配给日志文件的第一个名称,并且对于循环的每次传递,将每次迭代的所有数据都放入这个文件中:

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()

...我需要修改什么,以便每次循环通过时,正确重新分配日志文件名?

谢谢

4

1 回答 1

0

如果我理解正确,您想在每次循环迭代时创建一个新的日志文件。如果是这种情况,我认为这个问题可能对您有用。

于 2020-05-05T15:32:44.237 回答