Django shows the requests logs in command line, I want to save that logs in a file. I found a solution here but it saves the logs only that we print manually. Can we save all of the request and response logs in a separate file?
To log the Django request debug events, check out my answer here about using the fail-nicely-django config. You will also get 4xx and 5xx request events logged from Django's default loggers.
Note, though, that 2xx requests generally don't get logged by Django, if that's what you are after (only runserver shows them in the console). There is a workaround mentioned here, but you should generally use your server's access logs for these in production (e.g. access.log in nginx/Apache).
This should work on both linux and windows I hope:
python manage.py runserver > logs.txt
Related
Our Django App is failing media upload. This has been an off-and-on issue for us for a while. however, for about a week now, it's been consistently failing to upload media. Our media files are stored on S3.
On inspection, the uploaded files were found in the S3 buckets... However, the logs display the message below while the app throws an Application error...
Found this answer on GitHub (https://github.com/benoitc/gunicorn/issues/840)
Hi, we hit this issue in production using Flask + Gunicorn + Heroku and couldn't find a cause or a workaround.
For one particular POST request with POST parameters, the request would fail with an H18 error (sock=backend) in Heroku's router indicating that the server closed the socket when it shouldn't have.
We started decreasing the response size of that failing endpoint until we narrowed it down to around the 13k mark. If we sent less than 13k, the response would always work. If we sent more than 13k, the response would almost always not work.
Code to reproduce this is available at https://github.com/erjiang/gunicorn-issue - just deploy the repo to Heroku as is and follow the instructions in the README.
I have a Django app running on a gunicorn server with an
nginx up front.
I need to diagnose a production failure with an HTTP 500 outcome,
but the error log files do not contain the information I would expect.
Thusly:
gunicorn has setting errorlog = "/somepath/gunicorn-errors.log"
nginx has setting error_log /somepath/nginx-errors.log;
My app has an InternalErrorView the dispatch of which does an
unconditional raise Exception("Just for testing.")
That view is mapped to URL /fail_now
I have not modified handler500
When I run my app with DEBUG=True and have my browser request
/fail_now, I see the usual Django error screen alright, including
the "Just for testing." message. Fine.
When I run my app with DEBUG=False, I get a response that consists
merely of <h1>Server Error (500)</h1>, as expected. Fine.
However, when I look into gunicorn-errors.log, there is no entry
for this HTTP 500 event at all. Why? How can I get it?
I would like to get a traceback.
Likewise in nginx-errors.log: No trace of a 500 or the /fail_now URL.
Why?
Bonus question:
When I compare this to my original production problem, I am getting
a different response there: a 9-line document with
<h1><p>Internal Server Error</p></h1> as the central message.
Why?
Bonus question 2:
When I copy my database contents to my staging server (which is identical
in configuration to the production server) and set
DEBUG=True in Django there, /fail_now works as expected, but my original
problem still shows up as <h1><p>Internal Server Error</p></h1>.
WTF?
OK, it took long, but I found it all out:
The <h1>Server Error (500)</h1> response comes from Django's
django.views.defaults.server_error (if no 500.html template exists).
The <h1><p>Internal Server Error</p></h1> from the bonus question
comes from gunicorn's gunicorn.workers.base.handle_error.
nginx logs the 500 error in the access log file, not the error log file;
presumably because it was not nginx itself that failed.
For /fail_now, gunicorn will also log the problem in the access log,
not the error log; again presumably because gunicorn as such has
not failed, only the application has.
My original problem did actually appear in the gunicorn error log,
but I had never searched for it there, because I had
introduced the log file only freshly (I had relied on Docker logs
output before, which is pretty confusing) and assumed it would be
better to use the very explicit InternalErrorView for initial
debugging. (This was an idea that was wrong in an interesting way.)
However, my actual programming error involved sending a response
with a Content-Disposition header (generated in Django code) like this:
attachment; filename="dag-wönnegården.pdf".
The special characters are apparently capable of making
gunicorn stumble when it processes this response.
Writing the question helped me considerably with diagnosing this situation.
Now if this response helps somebody else,
the StackOverflow magic has worked once again.
may be server response 500 is logged in access_log not in errorlog
in nginx default file
access_log /var/log/nginx/example.log;
i think <h1><p>Internal Server Error</p></h1> is generated by nginx in production `
in debug=False
raise exception is treated as error or http500,so unless you changed the view for handler500,default 500 error page will be displayed
debug =true
raise exception is displayed in fancy djnago's debug page
I have a django app on Webfaction, and it seems that logs it is sending do not get written in the log file.
I have the following code:
log.debug('batch, guid: %s' % request.POST['guid'])
events = request.POST.getlist('logbatch[]')
for event in events:
a = event.split(';')
userlog = UserLog(csq_id=CsqId.objects.get(guid=request.POST['guid']), elementId=a[1], event=a[0], counter=int(a[2]), clientTime=js_time_to_python(int(a[3])))
userlog.save()
In general, logging works and is configured correctly. However, when I look in the database, I can see UserLog objects that are not logged in. As the DB insertion happens after the logging, I am certain that django did log the code. So why is it not in the log file?
Read the Django documentation on logging. You need to configure logging in your settings.py first, before you start using it. There is also an example configuration provided in the documentation.
I am using HTSQL with Django. I use HTSQL shell to check/generate my queries and then use them for rendering data in json and raw formats.
so like, my HTSQL shell url is:
http://127.0.0.1:8000/htsql
so when I want to access data from a table in the HTSQL shell environment, I do,
http://127.0.0.1:8000/htsql/table_name
and to get JSON data,
http://127.0.0.1:8000/htsql/table_name/:json
In background, HTSQL shell fetches this data by using a GET request. So from my client-side Javascipt/jQuery, I initiate a GET request with its URL in above format and get my desired JSON data directly.
Everything was fine when I was using local Django server, but when I deployed my project using Gunicorn and Nginx, it naturally started to block some of my long(actually, pretty long) queries in the GET requests. I searched this problem and found out that Gunicorn allows GET request values ranging from 0 to 8190 characters. So I tweaked my Gunicorn settings for the maximum limit but still the same problem. This was because my queries, when used with several filter values, are exceeding 8190 limit.
So I thought to use POST request as its normally preferred for secure and long requests. So I changed my GET request to POST request and pointed it to the same URL as mentioned above and tried it on my local Django Server(i.e without Gunicorn and Nginx). But now I get "400 BAD REQUEST". With firebug, I checked that the response was "POST requests are not permitted."
I also noticed that the HTSQL_Django Module routes all the request to htsql_django.views.gateway. I had a look to this gateway function in the views.py of htsql_django module but couldn't find any clue.
Is it so, that the HTSQL doesn't accepts POST requests?? How can I fetch/access JSON data from HTSQL using POST request?
We're using django 1.2.7 and the hoptoad logging middleware (hoptoad.middleware.HoptoadNotifierMiddleware).
When an error occurs, the error message gets sent to airbrake, but it does not seem to get logged to the django log file. It seems like it's intercepted by hoptoad exclusively.
Does anyone know how to also get the errors logged to the local django log file on disk? I couldn't find any such option in the hoptoad documentation.
Is there a custom modification needed to the hoptoad middleware class?