I want to write some log informations from a python's main script into a file in /var/log.
When I call logger.info("Starting")
, I get a PermissionError on the file, what is quite normal since files in /var/log belong to root and my program is not run as root.
I could of course set /var/log/my.log
's rights in order to let myapp write into it. (Set the same group for instance). But it doesn't look like a good practice to me: what if I install myapp on another computer? Should I then change the rights on the log file during the install process? Or is there another more generic way to do that? (Like a generic way to send the logs to "the system"? By generic I mean also portable, what would work on linux, freebsd etc.)
Though I'm not sure it's relevant, for information, here are some portions of my code:
Main script:
import logging, logging.config
from lib import settings
settings.init()
logging.config.fileConfig(settings.logging_conf_file)
logger = logging.getLogger(__name__)
The handler matching settings.logging_conf_file
, in the logging config file:
[handler_mainHandler]
class=FileHandler
level=INFO
formatter=defaultFormatter
filemode=w
args=('/var/log/myapp.log',)
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?