Logging django.request to file instead of console

2024/10/18 12:54:30

I am trying to configure my django settings.py to properly use the python logging facility but I've stumbled upon a rather strange problem:

Even after reading the docs, I simply can't find out how to redirect the console printed debug request lines from Django to a file I've specified; Below is part of my logging 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 keep seeing my console print line of the following format:

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

How may I redirect these to a file using the logging facility?

Thanks in advance

Answer

manage.py runserver is not using logging system for messages like [19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10. Instead of this, runserver uses sys.stderr (and sys.stdout for others messages). If you really need to redirect this to file you can override sys.stderr settings.py. Example - logging sys.stderr to file and console:

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

In write method you can use logging system to handle this by LOGGING settings as well.

Update:

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

https://en.xdnf.cn/q/72623.html

Related Q&A

Coinbase APIerror(id = ) in python

I want to transfer money between my coinbase accounts. Im storing all of my accounts IDs from client.get_accounts()[data][id] and transferring with the code, tx = client.transfer_money(2bbf394c-193b-5b…

cxfreeze missing distutils module inside virtualenv

When running a cxfreeze binary from a python3.2 project I am getting the following runtime error:/project/dist/project/distutils/__init__.py:13: UserWarning: The virtualenv distutils package at %s appe…

Preventing a multiplication expression evaluating in Sympy

I am generating an expression with two fractions, and want to pretty print as a whole expression with LaTeX, to then put on a worksheet.E.g. in the form:(5/7) * (3/4). However, when I do the following:…

Calling PARI/GP from Python

I would like to call PARI/GP from Python only to calculate the function nextprime(n) for different ns that I define. Unfortunately I cant get pari-python to install so I thought I would just call it us…

Can anyone explain why this sorting wont work?

For example if I have a list like this:List1 =[7,6,9] List1 = List1.sort()

Using argparse for mandatory argument without prefix

Im trying to use argparse module within my python application. My application should be run with single mandatory argument without any prefixes. I could not come up with a way to do it.

prevent unexpected stdin reads and lock in subprocess

A simple case Im trying to solve for all situations. I am running a subprocess for performing a certain task, and I dont expect it to ask for stdin, but in rare cases that I might not even expect, it m…

Regex split string by last occurrence of pattern

I am using regex to split a string <book name> by <author name> into book and author names.re.split(r\bby\b, text, 0, re.I)But problem arises when the book name contains the word "by&q…

How to write a unit-test where each test case has different input but does the same?

I need to create a unit-test for some python class. I have a database of inputs and expected results which should be generated by the UUT for those inputs.Here is the pseudo-code of what I want to do:f…

Unable to pass authentication information to NetSuites REST interface using Python

Ive been trying to get a NetSuite Restlet to work with Python 3.3 using urllib, but I cant seem to get the authorization to take and continually return a urllib.error.HTTPError: HTTP Error 401: Authori…