Django logging to console

I finally got it. Here’s what was happening.

When you define a logger using getLogger, you give a logger a name, in this case

logger = logging.getLogger(__name__)

and you then have to define how a logger with that name behaves in the LOGGING configuration. In this case, since that file is inside a module, the logger’s name becomes myApp.page_processors, not page_processors, so the logger named ‘page_processors’ in the LOGGING dict is never called. So why was the logging to the file working? Because in the (…) that I show in the code there is another logger named ‘myApp’ that apparently gets called instead, and that one writes to the file.

So the solution to this question is just to properly name the logger:

LOGGING = {
    # (...)
    'loggers': {
        # (...)
        'myApp.page_processors': {
            'handlers': ['console','file'],
            'level': 'DEBUG',
        }
    }
    # (...)
}

Leave a Comment