Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python logging.handlers.RotatingFileHandler allow creation of a group writable log file?

I'm using the standard python (2.5.2) logging module, specifically the RotatingFileHandler, on a linux system. My application supports both a command-line interface and a web-service interface. I would like to have both write to the same log file. However, when the log file gets rotated, the new file has 644 permissions and is owned by the web server user which prevents the command-line user from writing to it. Can I specify that new log files should be group-writable in the logging configuration or during logging initialization?

I have looked into the mode setting (r/w/a), but it doesn't seem to support any file permissions.

like image 600
Cory Engebretson Avatar asked Sep 10 '09 20:09

Cory Engebretson


People also ask

What are logging handlers Python?

Python Logging Handler The log handler is the component that effectively writes/displays a log: Display it in the console (via StreamHandler), in a file (via FileHandler), or even by sending you an email via SMTPHandler, etc. Each log handler has 2 important fields: A formatter which adds context information to a log.

Which functions are performed by logging in Python?

Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don't have any logging record and your program crashes, there are very few chances that you detect the cause of the problem.

What is logging logger in Python?

Logger : This is the class whose objects will be used in the application code directly to call the functions. LogRecord : Loggers automatically create LogRecord objects that have all the information related to the event being logged, like the name of the logger, the function, the line number, the message, and more.


1 Answers

Here is a slightly better solution. this overrides the _open method that is used. setting the umask before creating then returning it back to what it was.

class GroupWriteRotatingFileHandler(logging.handlers.RotatingFileHandler):         def _open(self):         prevumask=os.umask(0o002)         #os.fdopen(os.open('/path/to/file', os.O_WRONLY, 0600))         rtv=logging.handlers.RotatingFileHandler._open(self)         os.umask(prevumask)         return rtv 
like image 160
rob Avatar answered Sep 21 '22 08:09

rob