示例#1
0
    def test_no_root_handlers_no_lock(self):
        """
        DON'T add handlers to root, locking=False
        """
        lcd = LCDict(root_level='DEBUG')

        self.assertEqual(lcd.locking, False)
        self.assertEqual(lcd.attach_handlers_to_root, False)

        # lcd.dump()      # | DEBUG comment out

        expected = self.get_expected_starting_dict('DEBUG')
        self.assertEqual(lcd, expected)

        lcd.add_stderr_handler('console', level='WARNING', formatter='msg')
        # lcd.dump()      # | DEBUG comment out

        self.assertEqual(lcd['formatters'], {
            'msg': {
                'format': '%(message)s'
            },
        })

        lcd.add_file_handler('default_file',
                             filename='blather.log',
                             formatter='msg')

        # lcd.dump()      # | DEBUG comment out

        self.assertEqual(
            lcd['handlers'],
            {
                'console': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'msg',
                    'level': 'WARNING',
                    'stream': 'ext://sys.stderr'
                },
                'default_file': {
                    'class': 'logging.FileHandler',
                    'delay': False,
                    'filename': 'blather.log',
                    'formatter': 'msg',
                    # 'level': 'NOTSET',
                    'mode': 'a'
                }
            })

        lcd.add_logger('default',
                       handlers=('console', 'default_file'),
                       level='DEBUG')

        # lcd.dump()      # | DEBUG comment out

        self.assertEqual(lcd['loggers'], {
            'default': {
                'handlers': ['console', 'default_file'],
                'level': 'DEBUG'
            }
        })
def config_2():
    """ Attach a stdout handler to logger 'L'. """
    if USE_PRELOGGING:
        lcd = LCDict()
        lcd.add_formatter('my-other-fmt',
                          format='%(name)s - %(levelname)s - %(message)s')
        lcd.add_stdout_handler('L-out', formatter='my-other-fmt')
        lcd.add_logger('L', handlers='L-out', propagate=False)
        if preserve_root:
            lcd['root'] = {}
        lcd.config()
        # lcd.dump()    # generates the dict below
    else:
        root_dict = ({} if preserve_root else {
            'handlers': [],
            'level': 'WARNING'
        })
        d = {
            'disable_existing_loggers': False,
            'filters': {},
            'formatters': {
                'my-other-fmt': {
                    'class': 'logging.Formatter',
                    'format': '%(name)s - %(levelname)s - '
                    '%(message)s'
                }
            },
            'handlers': {
                'L-out': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'my-other-fmt',
                    'stream': 'ext://sys.stdout'
                }
            },
            'incremental': False,
            'loggers': {
                'L': {
                    'handlers': ['L-out'],
                    'level': 'NOTSET',
                    'propagate': False
                }
            },
            'root': root_dict,
            'version': 1
        }
        logging.config.dictConfig(d)
示例#3
0
__author__ = 'brianoneill'

import logging
try:
    import prelogging
except ImportError:
    import sys
    sys.path[0:0] = ['../..']
from prelogging import LCDict

from .module import *
from . import module
__all__ = module.__all__

# configure logging
lcd = LCDict()  # default: disable_existing_loggers=False
lcd.add_null_handler('library-nullhandler')  # default: level='NOTSET'
lcd.add_logger('library', handlers='library-nullhandler', level='INFO')
lcd.config()