How to set up logging on Django + Heroku? - django

I know heroku has its own built-in logging system but before heroku, my django already set up a logging system where I saved logs into a different file. And everytime I used the logging module it wouold be saved into that new file.
How do I set this up for heroku? All the "logging.debug(message)" and other logging messages have disappeared. They don't show up on heroku.
EDIT: so far, it looks like I have to stream that logging output to the console for stdout stderr... but it looks like I have to replace logging.debug(message) with ' print "message"'? or there should be another way to output that stream

Sorry for the stupid question due to my lack of understanding of Django logging in the first place.
I just need to redirect my logging "logger" to point its handler to the console rather than the logfile handler that I created.
It was simply a mistake of me rushing through the documentation and not viewing that all outputs were logged.

Related

How do I get a Django Server Error 500 report

I recently ran my tests on django for my project, one of them turned out as a server error 500. Thi confused me as I thought everything was passing. I currently have DEBUG=True. When I checked the documentation it said to set debug to true and add some admins to email for the full output. Is there an easier way to get the output or should I work on setting that up.
For more info my project is still being developed. I dont really want to post my test code as I really need more debugging experience but if you people ask I will!
Thanks for any help!
Errors are mailed throught Django logging framework. You must configure logging apropiatly to use other Handler than Email (email is by default for errors on django). Change it to Console or File Handler instead....
Here is the source doc:
https://docs.djangoproject.com/en/3.0/topics/logging/

How to add logs in django

I am new to django I want to print log information in my console how to do that. and also what is the use of that things in django, why we need that one.
If you want to log stuff to your console, javascript's console.log() would be the way to do that. With Django, print statements will show up in your terminal window where the server is running.

Django Error Reporting Simple Syntax Errors

I'm brand new to Django and am working through the tutorial. I fat-fingered a bit of code which would normally raise a syntax error or name error if I were running the file like a standard script. However, when I try to browse to the view in my web-browser using manage.py runserver I just get a blank page with no relevant error/traceback. The same is true of the console from which I ran manage.py runserver -- it just shows the GET request. Also, when starting the server, the console displayed "0 errors found" which was clearly false.
My question is how does Django report errors and tracebacks? Do I need to enable anything? (I have DEBUG = True in my settings.py file) It's kind of un-nerving to me that I could make a little typo and all of a sudden the entire website stops working and I'm not given a report of where the problem is.
Regarding running the development server, it'll say that there are 0 errors found in getting the server started. This typically refers to configuration and syntax in your settings.py file and models. It will only report errors that come up through the startup code path, which typically it doesn't go through your view handlers.
As you modify files, the development server is smart enough to reload itself. Syntax errors will crash the development server.
If you are seeing a blank page and the GET request returning successfully, then your code is working fine from a Django perspective. It's handling the request and returning a response, though you know the response is incorrect. Typically, syntax errors here will cause the view to return a 500, where you'll see debug information returned in the view (if DEBUG = True) and you'll see it in the console as well. It would seem that your typo was valid python as it went through your view handler successfully to return a blank page. There really isn't anything you can do here other than unit tests as to Django, a blank page could be a valid response.
Now as you move from development to production, you'll likely want to know when a page runs into an error. Django builds off Python's logging framework to handle error and other messages. The defaults in your settings.py file, set up a mail_admins handler that handlers error on django.request. If you set up ADMINS and EMAIL_BACKEND in your settings file, you'll get e-mailed the stack trace for any 500 error that happens on your server. As you get more sophistication, you can create your own logging by sending messages to Python's logging framework and custom handlers.
import logging
logger = logging.GetLogger(__name__) #using the module name so in your settings.py you can configure different settings per module
logger.info('msg')
logger.error('msg')
I'd recommend looking into Sentry as a handler for your error messages, that does hit counting and stores your error logs.

django social auth error after completing pipeline

I am using django social auth to power facebook connect in my app (admittedly with a little complicated user model and legacy database).
I was redirected to error page and seem to be running into an error AFTER completion of pipeline( redirect as last step of pipeline still redirects me).
Can someone tell me where to look to debug this?
Thanks,
A good place to start would be to look in social-auth's views.py, at the few places where the redirect to LOGIN_ERROR_URL happens (the variable url is set to LOGIN_ERROR_URL and then HttpResponseRedirect(url) is called). Add some print statements, or better, set breakpoints using the python debugger. If you run your app in the Django development server, the print statements will show up in the terminal in which you ran the server. Otherwise, they may show up in your server logs, depending on your configuration. You may also find django-debug-toolbar helpful.
Using print statements or the debugger, my workflow would be:
Figure out what line in views.py the redirect is triggered from
Figure out what condition causes that line to be reached
Inspect the variables leading to that condition
Sorry this is so general. Happy to help more if you can provide some more specific information.
Aaron

Logging Django Errors instead of returning them to inspect API callback

I'm working on a project that gets callbacks from some other sites' API. I expect my code to have a few errors, because I'm new to Python & Django. My site in development is using mod_wsgi and Apache with Debug = True ;)
The API I get the calls from adds a parameter to the querystring to my callback that is built using their own private key. So I have no way of accurately simulating that. If my code fails once I activate my probably faulty key validation code, I have no way of knowing except for apache server logs (which don't show the actual stacktrace or anything).
How can I log a more detailed python exception, like the one I usually see in my browser every 2 minutes ;) to a file? Especially the local variables around the faulty line are interesting obviously :)
Thanks! :)
If you set ADMINS in your settings.py (and set DEBUG=False) you will be emailed all 500 server errors (just like the DEBUG error page).
However, if you want your app to continue without responding with a 500, you can import logging and write your own debug log. There's a good tutorial here:
http://simonwillison.net/2008/May/22/debugging/