Repository: learningequality/kolibri

Issue:
print detailed exception info to server console on 500 errors

Currently, the web server middleware swallows all Python exceptions and returns the traceback information to the client in a 500 error. This makes debugging difficult.

It should be printed to the console and saved to log files.



Discussion:
Is this for devserver or production server?
probably both? might be helpful for folks reporting bugs from the "real world"
500s should produce a visible traceback in dev mode already, which is why I was checking if this issue was about that not working, or enhancing the reporting in production. 
Seems to not be producing a traceback in devmode. Steps to repro:

Update the Learn view.py to raise an exception:

```python
class LearnView(TemplateView):
    template_name = "learn/learn.html"

    def get_context_data(self, **kwargs):
        raise Exception("hell")
```

Start server with `kolibri manage devserver --debug` and navigate to `/learn`

Output:

```
DEBUG    Debug mode is on!
Performing system checks...

System check identified no issues (1 silenced).
September 03, 2017 - 15:53:29
Django version 1.9.7, using settings 'kolibri.deployment.default.settings.base'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[03/Sep/2017 15:53:30] "GET /learn/ HTTP/1.1" 500 80896
```

Note that the server _does_ return an HTML-formatted traceback page. However this is not as useful for debugging API requests, which are the majority of 500s.

![image](https://user-images.githubusercontent.com/2367265/30007341-3720b904-90c1-11e7-87e1-d8b5938d0f49.png)



And no traceback in the browser? 
Looks like we should probably add this in dev mode: https://docs.djangoproject.com/en/1.11/topics/email/#console-backend

In Chrome I have always been able to see these error pages on the preview tab of the network response in question, however it is a bit small by default. 
Sorry - to clarify, the browser-based error pages work as expected. Those I would expect to be turned off in a production environment.

This issue was only about adding stack traces to the server console and logs.

> In Chrome I have always been able to see these error pages on the preview tab of the network response in question

good point, I always forget that

Resolving patch:
diff --git a/kolibri/deployment/default/settings/base.py b/kolibri/deployment/default/settings/base.py
--- a/kolibri/deployment/default/settings/base.py
+++ b/kolibri/deployment/default/settings/base.py
@@ -215,6 +215,12 @@
             'class': 'django.utils.log.AdminEmailHandler',
             'filters': ['require_debug_false'],
         },
+        'request_debug': {
+            'level': 'ERROR',
+            'class': 'logging.StreamHandler',
+            'formatter': 'color',
+            'filters': ['require_debug_true'],
+        },
         'file_debug': {
             'level': 'DEBUG',
             'filters': ['require_debug_true'],
@@ -236,7 +242,7 @@
             'propagate': True,
         },
         'django.request': {
-            'handlers': ['mail_admins', 'file'],
+            'handlers': ['mail_admins', 'file', 'request_debug'],
             'level': 'ERROR',
             'propagate': False,
         },


Test patch: