Adnan Alam
Adnan Alam

Reputation: 63

Django application not logging request info in production log file when deployed with gunicorn and Nginx

This is the log settings of my Django application.

LOG_DIR = "logs"
LOG_DIR_PATH = os.path.join(BASE_DIR, LOG_DIR)
if not os.path.exists(LOG_DIR_PATH):
    os.mkdir(LOG_DIR_PATH)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "{asctime} {levelname} : {filename} line - {lineno:d} : {message}",
            "style": "{",
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    },
    "filters": {
        "require_debug_false": {
            "()": "django.utils.log.RequireDebugFalse",
        },
        "require_debug_true": {
            "()": "django.utils.log.RequireDebugTrue",
        },
    },
    "handlers": {
        "console": {
            "level": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "production": {
            "level": "INFO",
            "filters": ["require_debug_false"],
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "production.log"),
            "maxBytes": 1024 * 1024 * 5,  # 5 MB
            "backupCount": 5,
            "formatter": "verbose",
        },
        "development": {
            "level": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "development.log"),
            "maxBytes": 1024 * 1024 * 5,  # 5 MB
            "backupCount": 5,
            "formatter": "verbose",
        },
        "task_handler": {
            "level": "INFO",
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "tasks.log"),
            "maxBytes": 1024 * 1024 * 10,  # 10 MB
            "backupCount": 10,
            "formatter": "verbose",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
        },
        "py.warnings": {
            "handlers": ["console"],
        },
        "django.request": {
            "handlers": ["console"],
            "level": "ERROR",
        },
        "task_logger": {
            "handlers": ["task_handler"],
            "level": "INFO",
            "propagate": False,
        },
    },
    "root": {
        "handlers": ["production", "development"],
        "level": "INFO",
        "propagate": False,
    },
}

So, I'm expecting it to log all the requests info in development.log and production.log. It's working fine in development mode and also works with DEBUG=False with the python manage.py runserver command.

But when I deployed it with gunicorn and Nginx no requests logs appear in production.log. Other log messages appear in production.log except the requests log.

Log messages I'm expecting in production.log

2021-08-05 11:48:37 INFO : basehttp.py line - 157 : "GET /api/v1/settings/vm/cloud-status/ HTTP/1.1" 200 46
2021-08-05 11:48:38 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:40 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:41 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:42 INFO : basehttp.py line - 157 : "GET /api/v1/settings/vm/cloud-status/ HTTP/1.1" 200 46

Any help will be appreciated. Thanks in advance.

Upvotes: 4

Views: 2019

Answers (2)

Gagan
Gagan

Reputation: 1

'loggers': {
        ...
        ...
        'django.request': {
            'handlers':['console', 'reqlog'],
            'level':'ERROR'
        }
        ...
        ...
}

here reqlog is handler, which in your case should be production handler name [production] after adding reqlog to django request handlers, it works for me.

Upvotes: 0

Mohammad Fayaj
Mohammad Fayaj

Reputation: 9

import logging.config


logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            'format': '%(name)-12s %(levelname)-8s %(message)s'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console'
        },

    },
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['console']
        }
    }
})

#Try This

Upvotes: -1

Related Questions