Best practice to write logs in /var/log from a python script? Best practice to write logs in /var/log from a python script? linux linux

Best practice to write logs in /var/log from a python script?


If syslogd is running on your box, you can try to use SysLogHandler to avoid issue with folder permissions(https://docs.python.org/2/library/logging.handlers.html#sysloghandler).

To specify your category, you need to set facility parameter to desired, for example LOG_LOCAL5. In this case, it will correspond to local5.* category of syslogd.

As you specify facility as handler parameter, but not file name, you need to adjust syslog configuration to say syslogd to write log records to particular file. In FreeBSD, syslog conf file is /etc/syslog.conf (syslog.conf(5)).

Also you can add syslog mapping like . to /var/log/all.log to handle all logs from all syslog producers. It's helpful to determine if logging works and what is your application category, if there is a doubt.

For rsyslogd, it's possible to get more informations here: How to configure rsyslog for use with SysLogHandler logging class?