21

I have an app named main in my Django project. This app has a several management commands that I want to log, but nothing is showing up in stdout with the following configuration:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'log_to_stdout': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            },
        },
    'loggers': {
        'main': {
            'handlers': ['log_to_stdout'],
            'level': 'DEBUG',
            'propagate': True,
            }
        }
    }

What am I doing wrong? I've tried using my_project.main as well, but that didn't work either. I'm using 1.3.0 final.

4
  • what command are you using to log output? Commented Mar 9, 2012 at 13:48
  • I've tried logging.info and logging.debug, but neither of them work. After writing this question, I noticed that logging.warn does give me a print to stdout. Commented Mar 9, 2012 at 14:23
  • 1
    Sounds like your log level isn't being set properly. Try something like this reinout.vanrees.org/weblog/2017/03/08/… if you want to tie it to the verbosity flag. Commented Dec 4, 2019 at 16:35
  • For others who may be reading this... The root logger defaults to the "warning" level, so likely the logger being used was propagating to the root logger (or was the root logger) and not the "main" one being declared here. Commented Dec 20, 2021 at 14:37

3 Answers 3

23

you need to namespace your logger. currently you are logging to the root logger, which isn't caught by your handler, which is looking for main

rather than logging.debug("message"), you want

logger = logging.getLogger('main')
logger.debug("message")
Sign up to request clarification or add additional context in comments.

Comments

4

Setting "stream" to sys.stdout is not necessary. However, you should define a formatter:

Example:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'log_to_stdout': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
            },
        },
    'loggers': {
        'main': {
            'handlers': ['log_to_stdout'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
}

1 Comment

I tried by copy-pasting your example code verbatim but still, I get nothing to stdout. However, when I insert logging.warn('foo') as the first statement, I do get a print in stdout. logging.info and logging.debug still don't work.
-5

If you are enrolling these custom commands with cron, you can "force" logging by just redirecting output to a text file.

* 12 * * * python /path/to/my/custom/command.py >> /path/to/my/logfile.txt

This will run a cron job at 12am every morning, and anything directed to stdout (like python print statements) will be dumped into this text file, concatenated on any existing text in the logfile.

If you're not using cron, just create a single shell script that runs all the scripts you need to run and manually direct them to the logfiles you want.

python /path/to/my/custom/command.py >> /path/to/my/logfile.txt

...and so on.

2 Comments

Thanks, but this is the method I used before. I'm trying to do the right thing and use Django's "built-in" logging capabilities if possible.
Fair enough. Hopefully someone will step in and help you with this; I'm primitive and use shell redirection. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.