Missing logs in django - django

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.

Related

How to insert Django data on Nginx logs?

Im thinking on how to retrieve Django user data on the user authetication class and pass it to Nginx session variables, then on the nginx logging settings use that data to create a Nginx access log entry that contains the Django user that create such a request.
I have found these ideas:
Get current request by Django's or Python threading
https://gist.github.com/vparitskiy/71bb97b4fd2c3fbd6d6db81546622346
https://nedbatchelder.com/blog/201008/global_django_requests.html
Set a session variable:
How can I set and get session variable in django?
And then log the cookie variable via a Nginx configuration like:
https://serverfault.com/questions/223584/how-to-add-recently-set-cookies-to-nginxs-access-log
https://serverfault.com/questions/872375/what-is-the-difference-between-http-cookie-and-cookie-name-in-nginx
Any better idea?. I'm reinventing the wheel?
Finally I have done this. Place a middleware en Django that insert in the cookies the logging data that I want nginx to log.
Then I used the $upstream_cookies_NAME to rescue the COOKIES['NAME'] if any.
have you read django's documentation on logging?
I haven't worked with nginx, yet, but with apache djangos default logger also outputs to the apache log, meaning that you can do this:
from logging import getLogger
logger = getLogger('django')
def my_view(request):
logger.info(f'my view: {request.user}')
which will output the user to the server log.

Enabling logging for OpenLDAP client?

I'm trying to do some authentication inside a Django application using django-auth-ldap via the OpenLDAP client. It's not working so how do I enable some logging?
I CAN make LDAP queries using ldapsearch so fundamentally my config is correct and I tried enabling logging for django-auth-ldap but it just reports an Error(0) which is completely unhelpful.
So how do I enable logging for the OpenLDAP client part of the equation? Ideally I would like to see what queries it is making and using which config is being passed down from django-auth-ldap. I did find ldap.conf but the syntax man page implies there is no logging or debug option.
Stumbled across the answer...
To enable logging via the Django LDAP library, add the following to the settings.py file for your project
AUTH_LDAP_GLOBAL_OPTIONS = {
ldap.OPT_DEBUG_LEVEL: 255
}
The error that this flushed out was that I was using docker-compose and setting some environment values such as LDAP_SERVER="ldap://an.ldap.server.com" - but I should not have been quoting the string as the double-quotes were made part of the value. Removing these got me moving again.

Save django request logs in a file

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

How can I make hoptoad in Django 1.2 log errors to log file as well as send airbrake notifications?

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?

Catching Django Errors When Client Is Not A Web Browser?

I'm building a Django web service that is called from an application. When it throws an exception, I can't see the Django debug page, and can't get to it because the calling application doesn't behave like a web browser (and I don't have control over that application).
Is there a way to redirect the Django error page to a a log file rather than to the calling client, possibly via changing the FastCGI config (I'm using lighty + FastCGI)? Or maybe a "dump to file" config option or some sort of LogExceptionToFile() method within the framework itself?
You might try just creating custom ExceptionMiddleware. Just change the process_exception method to log the exception and request data somewhere.
Here's an example: http://www.peterbe.com/plog/who-was-logged-in-during-a-django-exception
If the exception in the django app is not caught, and DEBUG = True, then the exception should be sent to the client.
Some options to help you get debugging info:
Enable and configure logging
Set up email error reporting
Use something like Wireshark to inspect the HTTP request and responses.