Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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',)
like image 774
zezollo Avatar asked Apr 21 '16 07:04

zezollo


People also ask

How do I keep my log in Python?

Configuring Logging Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above. Creating a logging config file and reading it using the fileConfig() function. Creating a dictionary of configuration information and passing it to the dictConfig() function.

How do I create a multiple logging level in Python?

You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO.

What are the five levels of logging in Python?

Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions.


1 Answers

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?

like image 145
Michael Zhilin Avatar answered Oct 13 '22 01:10

Michael Zhilin