Issues with django file logger

55 views Asked by At

I have a created a django logger settings. It is intended that console django.db.backends should not be applied to console. But it is printing in console as well.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s "
            "%(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "file": {
            "level": "DEBUG",
            "class": "logging.handlers.TimedRotatingFileHandler",
            "filename": "debug/debug.log",
            'when': 'W0', # Weekly log staring from Monday
            'interval': 1,
            'backupCount': 10,
        },
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
    },
    "root": {"level": "INFO", "handlers": ["console"]},
    "loggers": {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['file'],
        }
    },
}

This is my logger configuration. With this it is logging sql commands in file as well as console. I want this to be logged only for file. Please help if you have encountered this issue before.

1

There are 1 answers

0
Vishnu T On

Issue fixed when I added propagate=False to django.db.backends

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s "
            "%(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "file": {
            "level": "DEBUG",
            "class": "logging.handlers.TimedRotatingFileHandler",
            "filename": "debug/debug.log",
            'when': 'W0', # Weekly log staring from Monday
            'interval': 1,
            'backupCount': 10,
        },
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
    },
    "root": {"level": "INFO", "handlers": ["console"]},
    "loggers": {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['file'],
            "propagate": False,
        }
    },
}