view pylearn/shared/layers/util.py @ 1496:93b8373c6735

Prefix loggers with 'pylearn.' to ensure there is no conflict when using Pylearn code within another library
author Olivier Delalleau <delallea@iro>
date Mon, 22 Aug 2011 11:28:48 -0400
parents bf2f71084d59
children
line wrap: on
line source

"""A few little internal functions"""
import logging

def update_locals(obj, dct):
    if 'self' in dct:
        del dct['self']
    obj.__dict__.update(dct)

def LogFn(f):
    def rval(*args):
        f(' '.join(str(a) for a in args))
    return staticmethod(rval)

def add_logging(cls, name=None, level=None):
    """ Add logging functions to a class: self._debug, self._info, self._warn, self._warning,
    self._error.

    All of these functions has the same signature.  They accept a variable number of positional
    arguments, and print them all casted to string (and concatenated with a ' '.)

    :type name: str
    :param name: the name of the logger.

    :type level: None, str, type(logging.INFO)
    :param level: a logging level (e.g. logging.INFO), or the name of a logging level (e.g
    'INFO').  If level is None, then this function doesn't set the logging level.

    """
    if name is None:
        name = "pylearn.shared.layers.%s" % cls.__name__
    cls._logger = logging.getLogger(name)
    if level:
        try:
            level = getattr(logging, level)
        except:
            pass
        cls._logger.setLevel(level)

    cls._debug = LogFn(cls._logger.debug)
    cls._info = LogFn(cls._logger.info)
    cls._warn = cls._warning = LogFn(cls._logger.warn)
    cls._error = LogFn(cls._logger.error)
    cls._critical = LogFn(cls._logger.critical)
    cls._fatal = LogFn(cls._logger.fatal)