Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c58dff1cef
Fetching contributors…

Cannot retrieve contributors at this time

file 389 lines (299 sloc) 13.479 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
"""Setup the logging options

Example configuration YAML definition object:

{ 'filters': None,
'formatters': { 'syslog': '%(levelname)s <PID %(process)d:%(processName)s> %(name).%(funcName)s: %(message)s',
'verbose': '%(levelname) -10s %(asctime)s %(name) -30s %(funcName) -25s: %(message)s'},
'handlers': { 'console': { 'class': 'logging.StreamHandler',
'debug_only': True,
'formatter': 'verbose',
'level': 'DEBUG'},
'syslog': { 'address': '/var/run/syslog',
'class': 'logging.handlers.SysLogHandler',
'facility': 'local6',
'formatter': 'syslog',
'level': 'INFO'}},
'loggers': { 'logging_config': { 'level': 'INFO', 'propagate': True},
'psycopg2': { 'level': 'INFO', 'propagate': True}}}

"""
__author__ = 'Gavin M. Roy'
__email__ = 'gmr@myyearbook.com'
__date__ = '2012-01-20'
__version__ = '1.0.1'

import logging
from logging.handlers import SysLogHandler

_ADDRESS = 'address'
_ARGS_IGNORE = ['class', 'formatter', 'filters', 'level', 'debug_only']
_CAPACITY = 1048576
_CLASS = 'class'
_DEBUG_ONLY = 'debug_only'
_DEFAULT_LEVEL = logging.INFO
_FACILITY = 'facility'
_FILTERS = 'filters'
_FORMATTER = 'formatter'
_FORMATTERS = 'formatters'
_HANDLERS = 'handlers'
_LEVEL = 'level'
_LEVELS = 'levels'
_LOGGERS = 'loggers'
_PROPAGATE = 'propagate'


def import_class(module_class): #param: no cover
    """Import the package for the given module and class string and return the
handle to the class in the module for creating new instances of the class.

:param str module_class: The module and class in foo.bar.baz.Class format
:returns: object

"""
    parts = module_class.split('.')
    cls_name = str(parts[-1])
    module = str('.'.join(parts[0:-1]))

    # Return the class handle
    return getattr(__import__(module, fromlist=cls_name), cls_name)


class Logging(object):
    """Setup and configure logging for the application allowing flexible output
and better use of the logging module in Python. Closely resembles the
logging.config.DictConfig functionality in Python 2.7.

"""
    # Used for Python 2.5+
    _HANDLERS = dict()

    def __init__(self, config, debug=False):
        """Create a new instance of the logging object.

:param dict config: The configuration data
:param dict debug: Flag to specify app is in debug mode

"""
        # Default values
        self._config = config
        self._debug = debug
        self._handlers = list()

        # Setup the internal stream handler and logger
        self._handler = self._internal_handler()
        self._logger = logging.getLogger('logging_config')
        self._setup_internal_handler()

        # Set a level for this logger
        self._logger.setLevel(logging.DEBUG if debug else logging.INFO)

    def _add_logger_handler(self, logger, handler, level=None):
        """Add the handler to the logger.

:param logging.Logger logger: The logger to add the handler to
:param logging.Handler handler: The handler to add
:param logging.Level level: An optional logger level

"""
        handler_names = self._get_handler_names(logger)
        if self._get_handler_name(handler) not in handler_names:
            logger.addHandler(handler)
        if isinstance(logger, logging.Logger) and level:
            logger.setLevel(self._get_level(level))

    def _build_arguments(self, config):
        """For the given config dictionary, build out the arguments that will
be passed to the constructor of the handler.

:param dict config: The configuration options
:returns: dict

"""
        out = dict()

        # Determine if this is for the syslog handler
        syslog = (config['class'] == 'logging.handlers.SysLogHandler')

        # Iterate through the keys in the config
        for key in config:

            # Set the logging level
            if key == _LEVEL:
                out[_LEVEL] = self._get_level(config[key])

            # Set the address to the proper format for syslog
            elif key == _ADDRESS:
                if isinstance(config[key], basestring):
                    out[_ADDRESS] = str(config[key])
                elif isinstance(config[key], list) or\
                     isinstance(config[key], tuple):
                    out[_ADDRESS] = config[key][0], config[key][1]

            # If it's facility and a syslog handler, set facility from the name
            elif key == _FACILITY and syslog:
                out[_FACILITY] = SysLogHandler.facility_names[config[key]]

            # It's not special and not in ignore, add it
            elif key not in _ARGS_IGNORE:
                out[key] = config[key]

        self._logger.debug('Arguments: %r', out)
        return out

    def _get_logger(self, logger):
        """Return the logger for the specific logger text.

:param str logger: The logger name
:return: logging.Logger

"""
        return logging.getLogger(None if logger == 'root' else logger)

    def _get_handler_name(self, handler):
        """Get the handler name for the specified handler.

:param logging.Handler handler: The handler
:return: str

"""
        try:
            return handler.get_name()
        except AttributeError:
            if handler in Logging._HANDLERS:
                return Logging._HANDLERS[handler]

    def _get_handler_names(self, logger):
        """Return a list of handlers for the given logger.

:param logger.Logger logger: The logger to return the handlers for
:return list: List of handlers

"""
        out = list()
        for handler in logger.handlers:
            out.append(self._get_handler_name(handler))
        return out

    def _get_level(self, level_name):
        """Get the logging level for the given level name.

:param str level_name: The level name
:returns: int

"""
        self._logger.debug('Fetching constant mapping for %s', level_name)
        return logging._levelNames[level_name]

    def _get_logger_name(self, class_handle, arguments):
        """Get a logger name for when we are creating a logger.

:param Object class_handle: The class_handle
:param dict arguments: The arguments for the class
:returns: str

"""

        logger = dict({'class': class_handle.__name__}.items() +
                      arguments.items())
        return 'logging_config:%x' % (hash(frozenset(logger.items())) &
                                      0xffffffff)

    def _internal_handler(self):
        """Return a StreamHandler for internal logging.

:returns: logging.MemoryHandler

"""
        return logging.handlers.MemoryHandler(_CAPACITY)

    def _new_handler(self, class_handle, arguments):
        """Return an instance of the specified handler.

:param Object class_handle: The class to create an instance of
:param dict arguments: The arguments to pass in
:returns: Object

"""
        name = self._get_logger_name(class_handle, arguments)

        if name in logging._handlers:
            return logging._handlers[name]

        handler = class_handle(**arguments)
        self._set_handler_name(handler, name)
        return handler

    def _set_handler_name(self, handler, name):
        """Set the handler name for the specified handler.

:param logging.Handler handler: The handler
:param str name: The handler name
:return: str

"""
        try:
            handler.set_name(name)
        except AttributeError:
            Logging._HANDLERS[handler] = name

    def _setup_filters(self, filters):
        """Iterate through the filters configuration and create a new
logging.Filter for each filter.

:param dict filters: Configuration data
:returns: dict

"""
        out = dict()
        for name in filters or dict():
            self._logger.debug('Creating filter %s: %s', name, filters[name])
            out[name] = logging.Filter(filters[name])
        return out

    def _setup_formatters(self, formatters):
        """Iterate through the formatters configuration and create new
logging.Formatter for each format.

:param dict formatters: Configuration data
:returns: dict

"""
        out = dict()
        for name in formatters or dict():
            self._logger.debug('Creating formatter %s: %s',
                               name, formatters[name])
            out[name] = logging.Formatter(formatters[name])
        return out

    def _setup_handlers(self, handlers, filters, formatters, loggers):
        """Setup the logging handlers with the given filters, formatters and
configuration options.

:param dict handlers: The configuration for the handlers
:param dict filters: Filters to be applied to the handlers
:params dict formatters: Formatters to be applied to the handlers
:param list loggers: A list of loggers to add the handlers to
:returns: list

"""
        out = list()

        # Iterate through the handlers
        for handler_name in handlers:

            # If it's a debug only handler, validate the class is in debug mode
            if handlers[handler_name].get(_DEBUG_ONLY) and not self._debug:
                self._logger.debug('Skipping debug only handler: %s',
                                   handler_name)
                continue

            # Get the class handle
            try:
                class_handle = import_class(handlers[handler_name][_CLASS])
            except KeyError:
                self._logger.debug('Invalid Logging Configuration for %s\n',
                                   handler_name)
                continue

            # Process the dictionary to make cli arguments
            arguments = self._build_arguments(handlers[handler_name])

            # Pull out levels if they're there
            if _LEVEL in arguments:
                level = arguments[_LEVEL]
                del arguments[_LEVEL]
            else:
                level = logging.INFO

            # Create a new instance of the Handler
            handler = self._new_handler(class_handle, arguments)

            # Set the formatter for the handler if specified
            if _FORMATTER in handlers[handler_name]:
                name = handlers[handler_name][_FORMATTER]
                if name in formatters:
                    handler.setFormatter(formatters[name])

            # Set filters for the handler if specified
            if _FILTERS in handlers[handler_name]:
                filter_names = handlers[handler_name][_FILTERS]
                for name in filter_names:
                    if name in filters:
                        handler.addFilter(filters[name])

            # Add the handler to all the specified loggers
            for logger in loggers:
                self._add_logger_handler(logger, handler, level)

            # Add the logger_config logger to this handler
            self._add_logger_handler(self._logger, handler, _DEFAULT_LEVEL)

            # Append the handler to the list
            out.append(handler)

        # Return the handlers
        return out

    def _setup_internal_handler(self):
        """Setup the internal handler, adding to the logger for the module."""
        if not self._logger.handlers:
            self._logger.addHandler(self._handler)

    def _setup_loggers(self, loggers):
        """Iterate through the loggers configuration getting the Logger object
for each of the named loggers.

:param dict loggers: Configuration data
:returns: list

"""
        out = list()
        for logger in loggers or dict():
            level = loggers[logger].get(_LEVEL, logging.INFO)
            self._logger.debug('Fetching %s and setting to %s', logger, level)
            logger_obj = self._get_logger(logger)
            logger_obj.setLevel(self._get_level(level))
            logger_obj.propagate = loggers[logger].get(_PROPAGATE, True)
            out.append(logger_obj)
        return out

    def remove_existing_loggers(self):
        """Remove the existing loggers from the logging module."""
        logging._handlers.clear()
        logging._handlerList = list()

    def remove_root_logger(self):
        """Remove the root logger handlers"""
        for handler in logging.root.handlers[:]:
            logging.root.removeHandler(handler)

    def setup(self):
        """Setup the loggers per the configuration that was passed in when
the object was created.

"""
        # Setup the filter objects
        filters = self._setup_filters(self._config.get(_FILTERS))

        # Setup the formatter objects
        formatters = self._setup_formatters(self._config.get(_FORMATTERS))

        # Get the loggers from the logger text list
        loggers = self._setup_loggers(self._config.get(_LOGGERS))

        # Setup the handler objects
        self._handlers = self._setup_handlers(self._config.get(_HANDLERS),
                                              filters,
                                              formatters,
                                              loggers)
Something went wrong with that request. Please try again.