Exemplo n.º 1
0
def add_handler(name, logging_opt):
    logger = logging.getLogger(LodelContext.get_name())
    if name in handlers:
        raise KeyError("A handler named '%s' allready exists")
    
    logging_opt = logging_opt._asdict()
    if 'filename' in logging_opt and logging_opt['filename'] is not None:
        maxBytes = (1024 * 10) if 'maxbytes' not in logging_opt else logging_opt['maxbytes']
        backupCount = 10 if 'backupcount' not in logging_opt else logging_opt['backupcount']

        handler = logging.handlers.RotatingFileHandler(
                                        logging_opt['filename'],
                                        maxBytes = maxBytes,
                                        backupCount = backupCount,
                                        encoding = 'utf-8')
    else:
        handler = logging.StreamHandler()
    
    if 'level' in logging_opt:
        handler.setLevel(getattr(logging, logging_opt['level'].upper()))

    if 'format' in logging_opt:
        formatter = logging.Formatter(logging_opt['format'])
    else:
        if 'context' in logging_opt and not logging_opt['context']:
            formatter = logging.Formatter(simple_format)
        else:
            formatter = logging.Formatter(default_format)

    handler.setFormatter(formatter)
    handlers[name] = handler
    logger.addHandler(handler)
Exemplo n.º 2
0
def log(lvl, msg, *args, **kwargs):
    if len(handlers) == 0: #late initialisation
        if not __init_from_settings():
            s_kwargs = copy.copy(kwargs)
            s_kwargs.update({'lvl': lvl, 'msg':msg})
            msg_buffer.append((s_kwargs, args))
            return
        else:
            for s_kwargs, args in msg_buffer:
                log(*args, **s_kwargs)
    from lodel.context import LodelContext
    if LodelContext.multisite():
        msg = "CTX(%s) %s" % (LodelContext.get_name(), msg)
    caller = logger.findCaller() # Opti warning : small overhead
    extra = {
        '_pathname': os.path.abspath(caller[0]),
        '_lineno': caller[1],
        '_funcName': caller[2],
    }
    logger.log(lvl, msg, extra = extra, *args, **kwargs)
Exemplo n.º 3
0
import copy
import logging, logging.handlers
import os.path

# Variables & constants definitions
default_format = '%(asctime)-15s %(levelname)s %(_pathname)s:%(_lineno)s:%(_funcName)s() : %(message)s'
simple_format = '%(asctime)-15s %(levelname)s : %(message)s'
SECURITY_LOGLEVEL = 35
logging.addLevelName(SECURITY_LOGLEVEL, 'SECURITY')
handlers = dict() # Handlers list (generated from settings)
##@brief Stores sent messages until module is able to be initialized
msg_buffer = []

# Fetching logger for current context
from lodel.context import LodelContext
logger = logging.getLogger(LodelContext.get_name())

##@brief Module initialisation from settings
#@return True if inited else False
def __init_from_settings():
    from lodel.context import LodelContext
    try:
        LodelContext.expose_modules(globals(), {
            'lodel.settings': ['Settings']})
    except Exception:
        return False
    LodelContext.expose_modules(globals(), {
        'lodel.settings.settings': [('Settings', 'Lodel2Settings')]})
    if not Lodel2Settings.started():
        return False
    # capture warning disabled, because the custom format raises error (unable