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

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

Avoid `logger=logging.getLogger(__name__)`

You can use logging.basicConfig to define the default interface available through logging as follows: import logging logging.basicConfig(level=logging.DEBUG, format=”%(asctime)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s”, ) This definition will now be used whenever you do the following anywhere in your application: import logging logging.error(…) While __name__ is not available, the equivalent (and other options) are available through … Read more

How can INFO and DEBUG logging message be sent to stdout and higher level message to stderr

import logging import sys class LessThanFilter(logging.Filter): def __init__(self, exclusive_maximum, name=””): super(LessThanFilter, self).__init__(name) self.max_level = exclusive_maximum def filter(self, record): #non-zero return means we log this message return 1 if record.levelno < self.max_level else 0 #Get the root logger logger = logging.getLogger() #Have to set the root logger level, it defaults to logging.WARNING logger.setLevel(logging.NOTSET) logging_handler_out = logging.StreamHandler(sys.stdout) … Read more

install filter on logging level in python using dictConfig

Actually, Tupteq‘s answer is not correct in general. The following script: import logging import logging.config import sys class MyFilter(logging.Filter): def __init__(self, param=None): self.param = param def filter(self, record): if self.param is None: allow = True else: allow = self.param not in record.msg if allow: record.msg = ‘changed: ‘ + record.msg return allow LOGGING = { … Read more

Python logging.Formatter(): is there any way to fix the width of a field and justify it left/right?

Field width can be specified by adding a number in front of the type specifier: >>> “%(foo)8s” % {‘foo’: ‘bar’} ‘ bar’ You can use this in your format string passed to the formatter. For your example, that’d be: “%(asctime)s %(filename)s: %(levelname)8s %(message)s”