Saturday, January 12, 2019

Re: Logging debug & error messages to file

Hi,


That way you don't have to deal with rotating log files (not sure if 

djangos file logger handles that though) and permission problems.


There is no "Django logger". Django uses the Python native logging module. This module defines several handler types, including the RotatingFileHandler one, which takes care of rotating logs files automatically on systems which don't support this out of the box (Windows for instance). Have a look at https://docs.python.org/3.7/library/logging.handlers.html#rotatingfilehandler for detail.


However, if your application is supposed to be deployed and executed on *nix servers, just log to standard files (or to stdout/stderr and redirect them to a log file) and add the involved log files in the logrotate configuration (see https://linux.die.net/man/8/logrotate). logrotate takes care of a lot of things, such as :

  • rotating files
  • purging oldest ones
  • compress oldest ones
All these actions can be freely configured based on your retention policy for logs.


logrotate is rock solid and at the heart of *nix since ages.


Best


Eric



From: django-users@googlegroups.com <django-users@googlegroups.com> on behalf of Kasper Laudrup <laudrup@stacktrace.dk>
Sent: Saturday, January 12, 2019 2:14:39 PM
To: django-users@googlegroups.com
Subject: Re: Logging debug & error messages to file
 
Hi Uri,

On 12/01/2019 11.34, אורי wrote:
> I'm trying to log debug & error messages to a file. But I receive this
> exception on the server:
>
> ValueError: Unable to configure handler 'file': [Errno 13] Permission
> denied: '/var/log/speedy_net_django.log'
>

The user running django does not have write access to '/var/log'. This
is a good thing. You could give the user running django write access to
'/var/log' or run django as another user, but I definitely wouldn't
recommend that.

> If I change the path to '/tmp/speedy_net_django.log' then it works.
>

That's because every user on the system has write access to '/tmp'.

> How do I configure the server to write messages
> to '/var/log/speedy_net_django.log'? We use Ubuntu and nginx.
>

You could create a directory under '/var/log', eg. '/var/log/speedy_net'
and give the user/group your running django as write access to that
directory and write your logs to '/var/log/speedy_net/django.log'.

> Our logging settings:
> https://github.com/speedy-net/speedy-net/blob/staging/speedy/core/settings/base.py
>
> By the way, are there friendlier ways to view logging messages than with
> text files?
>

Yes indeed. A much better solution would be to use the log system
already available on the system (eg. syslog or journald).

That way you don't have to deal with rotating log files (not sure if
djangos file logger handles that though) and permission problems.

Something like this might be useful:

https://www.simonkrueger.com/2015/05/27/logging-django-apps-to-syslog.html

Kind regards,

Kasper Laudrup

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a872ce04-115d-ec66-de7a-1a0212308d31%40stacktrace.dk.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment