Django loggers - Correct output to stdout and stderr

666 views
Skip to first unread message

alonn

unread,
Jun 9, 2014, 5:40:43 AM6/9/14
to pywe...@googlegroups.com

Seems that Django loggers default to use stderr for **all** logging levels.

for example when logging setup is:

    'version': 1,
            'disable_existing_loggers': True,
            'formatters': {
                'default': {
                    'format': "%(asctime)s:%(name)s:%(levelname)s:%(message)s"
                },
            },
            'handlers': {
                'console': {
                    'level': 'DEBUG',
                    'class': 'logging.StreamHandler',
                    'formatter': 'default',

                }
            },
            'loggers': {
                '': {
                    'handlers': ['console'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
                'django': {
                    'handlers': ['console'],
                    'level': 'WARNING',
                    'propagate': False,
                },
                'appname': {
                    'handlers': ['console'],
                    'level': 'WARNING',
                    'propagate': False,
                },
When I change handlers to:

     'handlers': {
                'console': {
                    'level': 'DEBUG',
                    'class': 'logging.StreamHandler',
                    'formatter': 'default',
                    'stream': sys.stdout  #Notice the change
                }
            },

Then all output is in stdout. 

But I need the **correct** behavior: logging info,debug, warning to ```stdout``` (warning can go either way, don't really care) and exception, error, critical to ```stderr```

What am I missing here? (some obvious setting? everybody knows about?) Thanks for the help!

(Cross posted from SO get the points!)

Shai Berger

unread,
Jun 9, 2014, 5:50:25 AM6/9/14
to pywe...@googlegroups.com
On Monday 09 June 2014 12:40:43 alonn wrote:
> Seems that Django loggers default to use stderr for **all** logging levels.
>

I think this is a Python stdlib issue; I don't think Django is doing anything
special here.

> for example when logging setup is:
>
> 'handlers': {
> 'console': {
> 'level': 'DEBUG',
> 'class': 'logging.StreamHandler',
> 'formatter': 'default',
>
> }
> },
> When I change handlers to:
>
> 'handlers': {
> 'console': {
> 'level': 'DEBUG',
> 'class': 'logging.StreamHandler',
> 'formatter': 'default',
> 'stream': sys.stdout #Notice the change
> }
> },
>
> Then all output is in stdout.
>

What you see here is documented behavior:
https://docs.python.org/2/library/logging.handlers.html#streamhandler


> But I need the **correct** behavior: logging info,debug, warning to
> ```stdout``` (warning can go either way, don't really care) and exception,
> error, critical to ```stderr```
>

I don't think I'd call that **correct**.

> What am I missing here? (some obvious setting? everybody knows about?)

You are, essentially, asking for a handler that only emits messages *under* a
given severity level. I don't think the Python logging framework supports
that.

If that is really what you want, subclassing logging.StreamHandler should not
be too hard; however, I doubt your spec.

Shai.

Meir Kriheli

unread,
Jun 9, 2014, 12:00:47 PM6/9/14
to pyweb-il
Quoting:

Try defining 2 handlers, e.g consoleout and consoleerr each using the correct stream.

Use the correct handler for each entry in the loggers section.

Cheers



--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyweb-il+u...@googlegroups.com.
To post to this group, send email to pywe...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyweb-il.
For more options, visit https://groups.google.com/d/optout.



--

alonn

unread,
Jun 11, 2014, 9:03:47 AM6/11/14
to pywe...@googlegroups.com
Thanks! work perfectly
Reply all
Reply to author
Forward
0 new messages