Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django loggers - Correct output to stdout and stderr

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!

like image 248
alonisser Avatar asked Jun 09 '14 09:06

alonisser


People also ask

Should logs go to stdout or stderr?

As mentioned in #6099: all log messages should go to stderr instead of stdout. command API related output should use a dedicated interface that outputs to stdout.

Does logger info go to stdout?

Logger defaults to printing to stderr instead of stdout.

Where does Python logger output?

Python Logging Best Practices If you want to catch error messages from libraries you use, make sure to configure the root logger to write to a file, for example, to make the debugging easier. By default, the root logger only outputs to stderr , so the log can get lost easily.

How do you write log to stdout?

There is a parameter for you CustomLog sets log filename and format. If you configure /proc/self/fd/1 as the log filename, Apache2 process will now write to its own stdout.


1 Answers

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.

like image 91
mkriheli Avatar answered Sep 29 '22 00:09

mkriheli