sazr
sazr

Reputation: 25938

Configure Logger to create new file each time script is run

Is there a way to make Python logging create a new log file each time the script/application runs? I'm just trying to create logs whose name is something unique like a timestamp.

Any idea how to configure to logger to do this?

    logging.config.dictConfig({
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            "file": {
                "level": "DEBUG",
                "filename": "test.log",
            },
        },
        'loggers': {
            '': {
                'handlers': ['file'],
                'level': 'DEBUG',
                'propagate': True
            }
        }
    })

Upvotes: 5

Views: 4152

Answers (1)

SpaceMan
SpaceMan

Reputation: 126

I'm not using dictConfig but I do find success using this setup.

    logdatetime = time.strftime("%m%d")
    logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
                        filename='../chown_parser_' + logdatetime + '.log',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        level=logging.INFO)

Upvotes: 6

Related Questions