1

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...

jonalv
  • 5,706
  • 9
  • 45
  • 64

3 Answers3

1

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.

RompePC
  • 815
  • 1
  • 8
  • 17
  • So what I see on my console is not really logging but something else? How do I get that stuff into my file then? – jonalv Jun 28 '17 at 08:58
  • Sorry for the latency, I've been busy with work (and yet). You could try two things: add/modify code for `runserver` command to log initial info of the development server, and set a [`request_started`](https://docs.djangoproject.com/en/1.11/ref/signals/#request-started) signal for autologging then. – RompePC Jun 28 '17 at 10:41
  • But doesn't Django do any logging? Like say that it has started and & or say when something goes wrong. Right now my server isn't working and I realise I don't have any logging at all to figure out why not... :/ – jonalv Jun 28 '17 at 11:21
  • 1
    Of course not, the one who logs is the web server (who captures exceptions and other stuff that your projects throw). Django only loggins if you tell it to do. And you'll need to tell to do it if you wanna get logs in production servers (own experience). – RompePC Jun 28 '17 at 11:50
1

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.

hxysayhi
  • 1,888
  • 18
  • 25
0

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'] },

scott huang
  • 2,478
  • 4
  • 21
  • 36
  • I was just hoping to catch the Django logging. I want to see what is going on and figure out why my server is not working... – jonalv Jun 28 '17 at 11:19
  • 1
    I think you are talking about log from Django built-in modules. This modules will logging to the pre-defined loggers. so you need to set 'disable_existing_loggers' to True, and define your own logger with same name and handler of your interest. Here is a good reference: https://www.webforefront.com/django/setupdjangologging.html. – scott huang Jun 29 '17 at 14:59