Writing django.request to a file instead of a console

I am trying to configure django settings.py to use the python settings.py correctly, but I came across a rather strange problem:

Even after reading the docs, I just can’t find out how to redirect deleted console debug request lines from Django to the file I specified; Below is my registration configuration.

 LOGGING = { 'version': 1, 'formatters': { 'simple': { 'format': '%(levelname)s %(message)s' }, } 'handlers': { 'file_http': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': r'C:\mysystem-http.log', 'formatter': 'verbose' } }, 'loggers': { 'django.request': { 'handlers': ['file_http'], 'level': 'DEBUG', 'propagate': False } } } 

I continue to see my console print line in the following format:

[19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10

How can I redirect them to a file using the logger?

Thank you in advance

+6
source share
3 answers

manage.py runserver does not use a logging system for messages like [19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10 . Instead, servererver uses sys.stderr (and sys.stdout for other messages). If you really need to redirect this to a file, you can override sys.stderr settings.py. An example is writing sys.stderr to a file and console:

 import sys class Logger(object): def __init__(self): self.console = sys.stderr self.file = open("runserver.log", "a", 0) def write(self, msg): self.console.write(msg) self.file.write(msg) sys.stderr = Logger() 

In the recording method, you can use the logging system to deal with this using the LOGGING settings.

Update:

In Django 1.10, the runerver output goes through the log: https://docs.djangoproject.com/en/dev/releases/1.10/#runserver-output-goes-through-logging

+6
source

These outputs are processed by your HTTP server (WSGIServer from the standard library, if it works in dev mode).

The configuration of your settings.py has nothing to do with this.

+3
source

@dikamilo the answer is correct.

Prior to Django 1.10, manage.py runserver ran Django's own WSGIServer implementation, which went into sys.stderr.

Adding the following code to settings.py monkey will fix the WSGIRequestHandler used by runserver so that all requests are logged through a logger named django.server to simulate the behavior of Django 1. 10+.

 ############################################################################### # SUPER-JANK PATCH FOR DJANGO RUNSERVER LOGGING PRIOR TO VERSION 1.10 ############################################################################### # # Add this to settings.py # import logging from django.core.servers.basehttp import WSGIRequestHandler logger = logging.getLogger('django.server') def log_message(self, format, *args): # Don't bother logging requests for admin images or the favicon. if self.path.startswith(self.admin_static_prefix) or self.path == '/favicon.ico': return msg = format % args if args[1][0] in ['1', '4', '5']: logger.warn(msg) else: logger.info(msg) WSGIRequestHandler.log_message = log_message 

It replaces the log_message method, which is responsible for actually writing to stderr .

After adding this patch, you can configure the registration, as usual, through LOGGING in settings.py .

0
source

Source: https://habr.com/ru/post/979905/


All Articles