{"id":13449267,"url":"https://github.com/metakermit/fail-nicely-django","last_synced_at":"2025-12-30T01:27:18.369Z","repository":{"id":44599416,"uuid":"55533714","full_name":"metakermit/fail-nicely-django","owner":"metakermit","description":"An example Django project with nice logging settings for debugging failures.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:01:51.000Z","size":19,"stargazers_count":45,"open_issues_count":1,"forks_count":16,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-01T06:21:26.797Z","etag":null,"topics":["django","docker","heroku","logging"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metakermit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-05T18:46:09.000Z","updated_at":"2022-10-25T16:22:05.000Z","dependencies_parsed_at":"2022-09-06T20:41:24.471Z","dependency_job_id":null,"html_url":"https://github.com/metakermit/fail-nicely-django","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metakermit%2Ffail-nicely-django","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metakermit%2Ffail-nicely-django/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metakermit%2Ffail-nicely-django/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metakermit%2Ffail-nicely-django/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metakermit","download_url":"https://codeload.github.com/metakermit/fail-nicely-django/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221840619,"owners_count":16889831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","docker","heroku","logging"],"created_at":"2024-07-31T06:00:34.743Z","updated_at":"2025-12-30T01:27:18.341Z","avatar_url":"https://github.com/metakermit.png","language":"Python","funding_links":[],"categories":["Python","docker"],"sub_categories":[],"readme":"fail-nicely-django\n==================\n\nAn example Django project with nice logging settings for debugging failures.\n\n![Bugs falling down](https://media.giphy.com/media/6WMXVZxdSIONW/giphy.gif)\n\nFeatures\n--------\n\n- [x] stdout \u0026 rotating file logging\n- [x] timestamps in the log format\n- [ ] logs visible from runserver, gunicorn, uwsgi, systemd, honcho, Docker\n- [x] deploy to Heroku button which just works with `heroku logs`\n- [ ] show how to upgrade to Sentry\n\nTested in Django 1.9, but should work since 1.3, though 1.9 made\n[some changes](https://docs.djangoproject.com/en/1.9/releases/1.9/#default-logging-changes-19)\nto the default Django loggers.\n\nSample log file output:\n\n```\n2016-04-05 22:12:32,984 [Thread-1    ] [INFO ] [djangoproject.logger]  This is a manually logged INFO string.\n2016-04-05 22:12:32,984 [Thread-1    ] [DEBUG] [djangoproject.logger]  This is a manually logged DEBUG string.\n2016-04-05 22:12:32,984 [Thread-1    ] [ERROR] [django.request      ]  Internal Server Error: /\nTraceback (most recent call last):\n  File \"/Users/kermit/.virtualenvs/fail-nicely-django/lib/python3.5/site-packages/django/core/handlers/base.py\", line 149, in get_response\n    response = self.process_exception_by_middleware(e, request)\n  File \"/Users/kermit/.virtualenvs/fail-nicely-django/lib/python3.5/site-packages/django/core/handlers/base.py\", line 147, in get_response\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\n  File \"/Users/kermit/projekti/git/fail-nicely-django/djangoproject/brokenapp/views.py\", line 12, in brokenview\n    raise Exception('This is an exception raised in a view.')\nException: This is an exception raised in a view.\n```\n\n\nWhy?\n----\n\nDjango by default seems to prefer not to write the things you log to it\nanywhere. It instead lets its loggers shortly meditate on the messages sent\ntheir way, after which they silently discard them.\nThe messages are simply disintegrated...\nThey evaporate into the circuitry's chasm of nothingness... They cease to be.\n\n\nIt somehow seems odd to have “the default” and\n“it's probably not what you want” describe the same thing :smiley: ...\n\n\u003e “If the disable_existing_loggers key in the LOGGING dictConfig is set to True\n(which is the default) then all loggers from the default configuration will be\ndisabled. Disabled loggers are not the same as removed; the logger will still\nexist, but will silently discard anything logged to it, not even propagating\nentries to a parent logger. Thus you should be very careful using\n'disable_existing_loggers': True; it’s probably not what you want.\nInstead, you can set disable_existing_loggers to False and redefine\nsome or all of the default loggers [...].”\n\u003e\n\u003e  — Excerpt From: The Official Django Documentation\n\nSecond, if you do use loggers, the `runserver`/`gunicorn` process doesn't output\nlog messages in your console. Things like Docker kind of religiously depend on\nthe executable outputting its own log files to stdout,\nso why not do it this way?\n\n[Others](http://stackoverflow.com/questions/5739830/simple-log-to-file-example-for-django-1-3/7045981)\nseem to be missing \"complete working example code\" for effectively using\nDjango's logging capabilities too.\n\nFor these reason and after having\n[some problems of my own](https://github.com/benoitc/gunicorn/issues/1124#issuecomment-161990634)\ngetting Django/Flask, `gunicorn` and `supervisord`\nto behave nicely with the log output, I started assembling\nthe best options I found into a nice example configuration. This is still\nwork in progress, so suggestions and patches are welcome :smiley:.\n\n\nCan I try it?\n-------------\n\nTo test the setup locally:\n\n    python3 -m venv venv\n    source ./venv/bin/activate\n    pip install -r requirements.txt\n    python djangoproject/manage.py migrate\n    python djangoproject/manage.py runserver\n\nAnd in another tab admire your logs:\n\n    tail -f djangoproject/djangoproject.log\n\nTo trigger some errors and log messages\njust open/refresh \u003chttp://localhost:8000/\u003e.\n\nTo see gunicorn output, issue `honcho start`\nand open/refresh \u003chttp://localhost:5000/\u003e\n\nYou can also try it on Heroku:\n\n[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)\n\nAnd you should be able to examine the logs after you refresh the page.\n\n    heroku logs -t --app myherokuapp\n\nOpen \u003chttps://myherokuapp.herokuapp.com\u003e in your browser\nto generate some exceptions (substitute your real app name).\n\n\nNice, I want this!\n------------------\n\nCool, then just copy the\n[*djangoproject/djangoproject/logger.py*](https://github.com/metakermit/fail-nicely-django/blob/master/djangoproject/djangoproject/logger.py)\nfile to your project (into the same folder where your *settings.py* is located)\nand add the following line to the bottom of your *settings.py* file:\n\n    from .logger import LOGGING\n\nThen in every part of your project where you wanna log something manually,\neither import the logger like in\n[*djangoproject/brokenapp/views.py*](https://github.com/metakermit/fail-nicely-django/blob/master/djangoproject/brokenapp/views.py).\nAlternatively, if you want the logger name to be something other\nthan `djangoproject.logger`, add to the top of every module\nwhere you want to log something:\n\n    import logging\n    log = logging.getLogger(__name__)\n\nProfit:\n\n    log.info('this is very important to log')\n\nThat's it, rock on!\n\nReferences\n----------\n\n- https://docs.djangoproject.com/en/1.9/topics/logging/\n- https://docs.python.org/3.5/library/logging.config.html\n- http://stackoverflow.com/questions/5739830/simple-log-to-file-example-for-django-1-3\n- http://stackoverflow.com/questions/12942195/django-verbose-request-logging\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetakermit%2Ffail-nicely-django","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetakermit%2Ffail-nicely-django","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetakermit%2Ffail-nicely-django/lists"}