Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django logging to console

People also ask

How do I use Django console?

LOGGING = { 'handlers' = { 'file': { 'level': 'DEBUG', 'class': 'logging. FileHandler', 'filename': 'mysite. log', 'formatter': 'verbose' }, 'console':{ 'level': 'DEBUG', 'class': 'logging. StreamHandler', }, }, (...) 'loggers'={ (...) 'page_processors': { 'handlers': ['console','file'], 'level': 'DEBUG', } } (...) }

How do I enable logging in Django?

By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.

How do I log into the console in Python?

Use the logging Module to Print the Log Message to Console in Python. To use logging and set up the basic configuration, we use logging. basicConfig() . Then instead of print() , we call logging.

Where are Django logs stored?

The Django One-Click application employs Gunicorn and Upstart. Application level logging can be found in /var/log/upstart/gunicorn. log By default, Gunicorn logs to stderr and Upstart will collect output to stderr/stdout in /var/log/upstart/$JOB_NAME.


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',
        }
    }
    # (...)
}

The following script:

import logging, logging.config
import sys

LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
        }
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO'
    }
}

logging.config.dictConfig(LOGGING)
logging.info('Hello')

writes Hello to sys.stdout, as can be verified by piping its output to a file. So your problem is likely to be somewhere else (or possibly that sys.stdout isn't what you expect). You could try with sys.__stdout__ to see if that makes a difference.


I am writing this for easy understanding dummies like me.

In settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'app_api': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    },
    }

Somewhere in your application views

import logging
logger = logging.getLogger('app_api') #from LOGGING.loggers in settings.py

try:
    one = 1/0
except Exception as e:
    logger.error(e)