3

Alright so the development phase is over and now my website is live in prod. However, I have not set up the logging. My website is located in /var/www/html dir. Ideally I would like to have Django logging in /var/log/django but that would require permissions.

  1. Is it standard practice to keep Django logs in /var/log/?
  2. What type of permissions I need for the logs to store in /var/log.
  3. I just want logs for last 2 days and today, rest can automatically be removed if I can.

Setup: RHEL7, Apache2.4, python3.5, Django 1.10, mod_wsgi, mySQL

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
nomad
  • 973
  • 2
  • 9
  • 22

1 Answers1

0
  1. No its not standard. Add the following statements to settings.py and modify the log file path to your needs:

     LOGGING = {
         'version': 1,
         'disable_existing_loggers': False,
         'handlers': {
             'file': {
                 'level': 'DEBUG',
                 'class': 'logging.FileHandler',
                 'filename': '/path/to/django/debug.log',
             },
         },
         'loggers': {
             'django': {
                 'handlers': ['file'],
                 'level': 'DEBUG',
                 'propagate': True,
             },
         },
     }
    
  2. Refer this for setting up suitable permissions for log files: Permission Denied when writing log file

  3. You will have to use logrotate otherwise make use of kafka server to maintain logs.

ABN
  • 1,024
  • 13
  • 26
Abijith Mg
  • 2,647
  • 21
  • 35
  • You don't have to write cron jobs to delete log files - you can use `logrotate`. – Alasdair Mar 21 '17 at 16:00
  • Oh didn't knew that. Thanks for the info. Will edit my answer. – Abijith Mg Mar 21 '17 at 16:03
  • It is more normal to set up ``LOGGING`` so that messages are output to the console stream handler. That way they end up in the Apache error log. – Graham Dumpleton Mar 21 '17 at 20:03
  • @Alasdair What do say about Graham comment? Is it better to use python logging or print to console to display on apache error logs? – Abijith Mg Mar 22 '17 at 07:16
  • I am not an expert on logging, all I had to contribute to the comments was the logrotate suggestion. Graham is the author of modwsgi, I trust his advice! – Alasdair Mar 22 '17 at 07:38
  • @GrahamDumpleton Can you plz share any link which has info about efficient logging system for django projects. Even I am curious to learn more on the same. Thanks in advance. – Abijith Mg Mar 22 '17 at 08:00
  • 3
    When using mod_wsgi it doesn't care whether you use the Python logging module or use ``print()`` to console. In either case, mod_wsgi intercepts the ``sys.stdout`` and ``sys.stderr`` streams and redirects the output to the Apache error log. Since Django is integrated with the logging module, and by using the logging module and you have control over what is logged by setting a log level, that would provide a more flexible system. – Graham Dumpleton Mar 22 '17 at 08:55
  • Thanks for suggestions. Will implement it accordingly. – Abijith Mg Mar 22 '17 at 12:04