简体   繁体   中英

Why is my Python Django logging file empty when my console log is not?

I am trying to set up a Django log to file. Based on https://stackoverflow.com/a/19257221/214742 I came up with the following configuration:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
        'applogfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(PROJECT_ROOT, 'MODELINGWEB.log'),
            'maxBytes': 1024*1024*15, # 15MB
            'backupCount': 10,
            'formatter': 'simple'
        },
    },
    'loggers': {
        '': {
          'level': 'DEBUG',
          'handlers': ['console','applogfile']
        },
    },
}

Now when I try to run and load a page my console log looks like:

Performing system checks...

System check identified no issues (0 silenced).
June 28, 2017 - 10:18:22
Django version 1.11, using settings 'Modeling.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[28/Jun/2017 10:18:27] "GET / HTTP/1.1" 200 12564

I also get a file MODELINGWEB.log but it is empty. Why is that? I was expecting it to contain the same things...

Although you are setting the configuration for a logger , you aren't logging the info into the file:

import logging

logger = logging.getLogger(__name__)
logger.info("blah blah blah")  # Or loggger.error(..), or whatever

I would recommend getting a closer look to the docs (the link goes to the specific part of using this logger object).

What I truly unknown is how to add a signal when running the development server to trigger the piece of code above.

As you can see in https://docs.djangoproject.com/en/dev/topics/logging/#using-logging Once you have configured your loggers, handlers, filters and formatters, you need to place logging calls into your code. Using the logging framework is very simple.

So what you see in your console is not really logging but something else

If you want to get the stuff in your console into my file you can use pipe and tee :

[the command you want to run] | tee you_log_file.log

For example:

ifconfig | tee ifconfig.log

you will get the output in your console and the file ifconfig.log.

What's your code for logging logs? You need to obtain logger for logging into specified logger. I notice that you don't even give a name to your logger, so I suppose you just use "logging.error, logging.debug, etc"

'loggers': { '': { 'level': 'DEBUG', 'handlers': ['console','applogfile'] },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM