Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django LOGGING not printing to console and file

I have encountered a strange behavior of Django Loggers.

I am developing a front end application using Django. During the login service, I make some requests to certain components and use log.warning() calls to see the flow of the requests.

The logs worked perfectly, until I decided to add a LOGGING configuration to print the output of the logs in a file, as I want to deploy the application via Docker and I want to periodically check the log files.

When I added the following Django configuration concerning logging:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'detailed': {
            'class': 'logging.Formatter',
            'format': "[%(asctime)s] - [%(name)s:%(lineno)s] - [%(levelname)s] %(message)s",
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'INFO',
            'formatter': 'detailed',
        },
        'file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': "{}/am.log".format(BASE_DIR),
            'mode': 'w',
            'formatter': 'detailed',
            'level': 'INFO',
            'maxBytes': 2024 * 2024,
            'backupCount': 5,
        },
    },
    'loggers': {
        'am': {
            'level': 'INFO',
            'handlers': ['console', 'file']
        },
    }
}

The logging stops working. The file specified in the logging configuration, am.log, is indeed created but nothing gets printed to this file. Even the console logging does not take place.

I have taken this logging configuration from one of my Django projects for the backend of this application, and there it works perfectly. I really don't understand what I am doing wrong. Could you please help me or guide me in the right direction. I would be very grateful.

I wish you all a good day!

like image 400
Lazar Vlad Avatar asked Sep 17 '25 06:09

Lazar Vlad


1 Answers

By using the key "am" in your 'loggers' configuration, you're defining one logger with name "am":

'loggers': {
        'am': {   #  <-- name of the logger
            'level': 'INFO',
            'handlers': ['console', 'file']
        },
    }

So to use that logger, you have to get it by that name:

logger = logging.getLogger("am")
logger.warning("This is a warning")

If you name your loggers by the name of the module in which you're running, which is recommended practice, then you need to define each module logger:

logger = logging.getLogger(__name__)  # <-- this logger will be named after the module, e.g. your app name.

Then in your logging configuration you can specify logging behavior per module (per app):

'loggers': {
        'my_app': {  # <-- logging for my app
            'level': 'INFO',
            'handlers': ['console', 'file']
        },
        'django': {   # <-- logging for Django module
            'level': 'WARNING',
            'handlers': ['console', 'file']
        },
    }

Or if you just want to log everything the same, use the root ('') logger, which doesn't have a name, just empty string:

'loggers': {
        '': {   # <-- root logger
            'level': 'INFO',
            'handlers': ['console', 'file']
        },
    }
like image 121
dirkgroten Avatar answered Sep 19 '25 01:09

dirkgroten