python logging: how to ensure logfile directory is created?

Subclass FileHandler (or whatever handler you are using) to call mkdir_p during initialization: import logging import os import errno def mkdir_p(path): “””http://stackoverflow.com/a/600612/190597 (tzot)””” try: os.makedirs(path, exist_ok=True) # Python>3.2 except TypeError: try: os.makedirs(path) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise class MakeFileHandler(logging.FileHandler): def __init__(self, filename, mode=”a”, encoding=None, … Read more

Python logging: create log if not exists or open and continue logging if it does

The logging module’s FileHandler takes care of that for you. No need for complexity. The handler takes an optional mode parameter, to specify whether it starts writing or appending data to it. From the docs: class logging.FileHandler(filename, mode=”a”, encoding=None, delay=False) The specified file is opened and used as the stream for logging. If mode is … Read more

django.request logger not propagated to root?

The solution is to prevent Django from configuring logging and handle it ourselves. Fortunately this is easy. In settings.py: LOGGING_CONFIG = None LOGGING = {…} # whatever you want, as you already have import logging.config logging.config.dictConfig(LOGGING) UPDATE ~March 2015: Django has clarified their documentation: If the disable_existing_loggers key in the LOGGING dictConfig is set to … Read more

Python logging split between stdout and stderr [duplicate]

This seems to do what I want: #!/usr/bin/python import sys import logging class InfoFilter(logging.Filter): def filter(self, rec): return rec.levelno in (logging.DEBUG, logging.INFO) logger = logging.getLogger(“__name__”) logger.setLevel(logging.DEBUG) h1 = logging.StreamHandler(sys.stdout) h1.setLevel(logging.DEBUG) h1.addFilter(InfoFilter()) h2 = logging.StreamHandler() h2.setLevel(logging.WARNING) logger.addHandler(h1) logger.addHandler(h2)

Python best practice in terms of logging

Best practice is to follow Python’s rules for software (de)composition – the module is the unit of Python software, not the class. Hence, the recommended approach is to use logger = logging.getLogger(__name__) in each module, and to configure logging (using basicConfig() or dictConfig()) from the main script. Loggers are singletons – there is no point … Read more

Python logging exception

There is nothing wrong with catching to log. However, I’d recommend: try: response = self.call_api_method() except APIException, e: # or ‘as e’ depending on your Python version self.log.exception(‘Oh noes!’) raise #Throw exception again so calling code knows it happened else: return response.someData By just doing a bare raise you preserve the full traceback info. It’s … Read more

Showing the right funcName when wrapping logger functionality in a custom class

Essentially, the code to blame lies in the Logger class: This method def findCaller(self): “”” Find the stack frame of the caller so that we can note the source file name, line number and function name. “”” f = currentframe() #On some versions of IronPython, currentframe() returns None if #IronPython isn’t run with -X:Frames. if … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)