[Python] Have warning, info and debug logs in separate files

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to use dictConfig to configure my logging in Python and let it use 3 different files:

    1. Warning
    2. Info
    3. Debug

    If I want to run DEBUG logging I still want the warning file and info file to be updated. How should I set up my configuration file to manage this? Is it possible?

    I have run some tests with the following config:

    {
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
    "simple": {
    "format": "%(levelname)s: %(asctime)s: %(message)s",
    "datefmt": "%Y-%m-%dT%H:%M:%S%z"
    },
    "detailed": {
    "format": "[%(levelname)s|%(module)s|L%(lineno)d] %(asctime)s: %(message)s",
    "datefmt": "%Y-%m-%dT%H:%M:%S%z"
    }
    },
    "handlers": {
    "stdout": {
    "class": "logging.StreamHandler",
    "level": "WARNING",
    "formatter": "simple",
    "stream": "ext://sys.stdout"
    },

    "debugfile": {
    "class": "logging.handlers.RotatingFileHandler",
    "level": "DEBUG",
    "formatter": "detailed",
    "filename": "debug.log",
    "maxBytes": 10000,
    "backupCount": 3
    },
    "infofile": {
    "class": "logging.handlers.RotatingFileHandler",
    "level": "INFO",
    "formatter": "simple",
    "filename": "logInfo.log",
    "maxBytes": 10000,
    "backupCount": 3
    },
    "warningfile": {
    "class": "logging.handlers.RotatingFileHandler",
    "level": "WARNING",
    "formatter": "simple",
    "filename": "logInfo.log",
    "maxBytes": 10000,
    "backupCount": 3
    }
    },
    "loggers": {
    "root": {
    "level": "DEBUG",
    "handlers": [
    "warningfile"
    ]
    }
    }
    }


    It works but if I choose DEBUG all logging occurs only in the debug.log.

    Continue reading...

Compartilhe esta Página