Ejemplo n.º 1
0
def logging_config(log_path, logfilename=''):
    """Create a root logger with a stdout console handler with level=DEBUG,
    and, if logfilename is not empty, a file handler with level=INFO *and*
    with delay=True, so that the logfile is created only when written to.
    Root logger level will be DEBUG.
    """
    lcd = LCDict(log_path=log_path,
                 root_level='DEBUG',
                 attach_handlers_to_root=True)
    lcd.add_stdout_handler('con', formatter='msg', level='DEBUG')

    if logfilename:
        # add a file handler, which will write to log_path + '/' + logfilename
        lcd.add_formatter('my_file_formatter',
                          format='%(levelname)-8s: %(message)s')
        # Defaults:
        #   level='DEBUG',
        #   attach_to_root=True
        lcd.add_file_handler(
            'app_file',
            filename=logfilename,
            formatter='my_file_formatter',
            level='INFO',
            mode='w',  # default, just as a reminder
            delay=True  # default: False
        )
    # lcd.dump()           # | DEBUG

    lcd.config()
Ejemplo n.º 2
0
def config_logging(use_locking):
    """
    """
    logfilename = ('mproc_LOCKING.log'
                   if use_locking else 'mproc_NOLOCKING.log')
    # add handlers to root == False, default
    lcd = LCDict(log_path=LOG_PATH, locking=use_locking)

    ## Add 'console' handler with level higher than 'DEBUG';
    # add main file handler, which will write to '_log/' + logfilename;
    # add logger that uses them.
    lcd.add_stdout_handler(
        'console', formatter='msg', level='INFO'
    ).add_formatter(
        'my_file_formatter',
        format=
        '%(processName)-12s: %(name)-14s: %(levelname)-8s: %(asctime)24s: %(message)s',
    ).add_file_handler(
        'app_file',
        filename=logfilename,
        mode='w',
        level='DEBUG',
        formatter='my_file_formatter').add_logger(
            __name__,
            handlers=('app_file', 'console'),
            level='DEBUG',
            propagate=False  # so it DOESN'T propagate to root logger
        )
    lcd.config()
Ejemplo n.º 3
0
def logging_config(log_path, logfilename=''):
    """Create a root logger with a stdout console handler with level=INFO,
    and, if logfilename is not empty, a file handler with level=DEBUG.
    Root logger level will be INFO.
    """
    # . Just for coverage:
    # .     root_level='CRITICAL')
    # . Temporary, cuz of lcd.set_logger_level(...) below.
    lcd = LCDict(log_path=log_path,
                 attach_handlers_to_root=True,
                 root_level='CRITICAL')
    lcd.add_stdout_handler('con', level='WARNING', formatter="msg")

    lcd.set_logger_level(None, 'INFO')  # . coverage ho'

    if logfilename:
        # add a file handler, which will write to log_path + '/' + logfilename
        # Of course, we don't have to add a formatter, we could just
        # use formatter='level_msg' in add_file_handler(...)
        lcd.add_formatter('my_file_formatter',
                          format='%(levelname)-8s: %(message)s')
        # Defaults:
        #   level='DEBUG',
        lcd.add_file_handler(
            'app_file',
            filename=logfilename,
            mode='w',
            locking=True,  # for kicks 'n' coverage
            formatter='my_file_formatter',
        )
    # lcd.dump()           # | DEBUG

    lcd.config()
def config_1():
    """ Attach stdout handler to root. """
    if USE_PRELOGGING:
        lcd = LCDict()
        lcd.add_formatter('my-fmt', format='** %(message)s')
        lcd.add_stdout_handler('root-out', formatter='my-fmt')
        lcd.attach_root_handlers('root-out')
        lcd.config()
        # lcd.dump()    # generates the dict below
    else:
        d = {
            'disable_existing_loggers': False,
            'filters': {},
            'formatters': {
                'my-fmt': {
                    'class': 'logging.Formatter',
                    'format': '** %(message)s'
                }
            },
            'handlers': {
                'root-out': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'my-fmt',
                    'stream': 'ext://sys.stdout'
                }
            },
            'incremental': False,
            'loggers': {},
            'root': {
                'handlers': ['root-out'],
                'level': 'WARNING'
            },
            'version': 1
        }
        logging.config.dictConfig(d)
Ejemplo n.º 5
0
def config_logging():
    """Create a root logger with a stdout console handler with level=WARNING,
    and a file handler with level=DEBUG.
    Root logger level will be DEBUG.
    """
    # Defaults:
    #   attach_handlers_to_root=False,
    lcd = LCDict(log_path=LOG_PATH,
                 attach_handlers_to_root=True,
                 locking=True,
                 root_level='DEBUG')

    lcd.add_stdout_handler('console', level='WARNING', formatter='msg')

    # add a file handler, which will write to log_path + '/' + logfilename
    lcd.add_formatter(
        'my_file_formatter',
        format='%(levelname)-8s: %(message)s',
    )
    lcd.add_rotating_file_handler(
        'rot_fh',
        filename=LOGFILENAME,
        formatter='my_file_formatter',
        max_bytes=200,
        backup_count=10,
        mode='w',
    )
    # lcd.dump()           # | DEBUG

    lcd.config()
Ejemplo n.º 6
0
    def test_no_lock_clone_handler(self):
        """
        clone handler with locking=False (so 'class' is in its dict)
        """
        lcd = LCDict()

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

        lcd.add_stdout_handler('con', level='WARNING', formatter='msg')
        lcd.clone_handler(clone='con2', handler='con')

        # lcd.dump()      # | DEBUG comment out

        self.assertEqual(
            lcd['handlers'], {
                'con': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'msg',
                    'level': 'WARNING',
                    'stream': 'ext://sys.stdout'
                },
                'con2': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'msg',
                    'level': 'WARNING',
                    'stream': 'ext://sys.stdout'
                }
            })
Ejemplo n.º 7
0
def config_logging():
    lcd = LCDict(attach_handlers_to_root=True, root_level='DEBUG')
    lcd.add_stdout_handler('console-out', level='DEBUG', formatter='level_msg')
    lcd.add_callable_filter(
        'count_info',
        filter_fn,
        # extra, static data
        filtername='count_info',
        loglevel_to_count=logging.INFO)
    lcd.attach_root_filters('count_info')

    lcd.config()
def config_logging():
    lcd = LCDict(attach_handlers_to_root=True, root_level='DEBUG')
    lcd.add_formatter(
        'user_ip_level_msg',
        format='User: %(user)-10s  IP: %(ip)-15s  %(levelname)-8s  %(message)s'
    )
    lcd.add_stdout_handler('console-out',
                           level='DEBUG',
                           formatter='user_ip_level_msg')
    lcd.add_class_filter(
        'field-adding_filter',
        FilterThatAddsFields,
        # extra, static data
        datasource=get_data)
    lcd.attach_root_filters('field-adding_filter')

    lcd.config()
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)
def config_logging():
    lcd = LCDict(attach_handlers_to_root=True, root_level='DEBUG')
    lcd.add_stdout_handler('console-out', level='DEBUG', formatter='level_msg')
    lcd.add_class_filter(
        'count_debug',
        CountAndSquelchOdd,
        # extra, static data
        filtername='count_debug',
        loglevel_to_count=logging.DEBUG)
    lcd.add_class_filter(
        'count_info',
        CountAndSquelchOdd,
        # extra, static data
        filtername='count_info',
        loglevel_to_count=logging.INFO)
    lcd.attach_root_filters('count_debug', 'count_info')

    lcd.config()
Ejemplo n.º 11
0
def config_logging(log_path, logfilename=''):
    """Create a root logger with a stdout console handler with loglevel=INFO,
    and, if logfilename is not empty, a file handler with default loglevel=NOTSET.
    Root loglevel will be INFO.
    """
    lcd = LCDict(log_path=log_path,
                 attach_handlers_to_root=True,
                 root_level='INFO')
    lcd.add_stdout_handler('console', level='INFO', formatter='msg')
    if logfilename:
        # add a file handler, which will write to log_path + '/' + logfilename
        lcd.add_formatter(
            'my_file_formatter',
            format='%(levelname)-8s: %(message)s',
        ).add_file_handler('app_file',
                           filename=logfilename,
                           mode='w',
                           formatter='my_file_formatter')
    # lcd.dump()           # | DEBUG

    lcd.config()
Ejemplo n.º 12
0
def main():
    """
    Not a test because... your mileage *will* vary (hard to test).
    """
    lcd = LCDict(attach_handlers_to_root=True)

    # style='%' is the default
    lcd.add_formatter('fmtr1',
                      format='%(asctime)s %(levelname)s: %(message)s',
                      dateformat='%H:%M:%S')

    lcd.add_formatter(
        'fmtr2', fmt='%(asctime)s -- %(message)s',
        datefmt='%Y.%m.%d')  # %Y: 4-digit years; %y: 2-digit years

    lcd.add_stdout_handler('con1', formatter='fmtr1')
    lcd.add_stdout_handler('con2', formatter='fmtr2')

    lcd.config()

    logging.getLogger().warning('Danger, Will Robinson!')
Ejemplo n.º 13
0
def config_logging():
    """Add a stdout console handler with level=WARNING to the root logger,
    and a syslog handler with default level=NOTSET.
    Root logger level will be DEBUG.
    """
    # Defaults:
    #   attach_handlers_to_root=False,
    lcd = LCDict(attach_handlers_to_root=True,
                 locking=True,
                 root_level='DEBUG')

    lcd.add_stdout_handler('console', level='WARNING', formatter='msg')

    if not sys.platform.startswith('darwin'):
        raise NotImplementedError(
            "This example is currently implemented only for OS X / macOS")

    # add a syslog handler
    lcd.add_syslog_handler(
        'h_syslog',
        formatter='logger_level_msg',
        address='/var/run/syslog',
    )
    lcd.config()
Ejemplo n.º 14
0
    def format(self, logrecord, *args, **kwds):
        message = super(MyFormatter, self).format(logrecord, *args, **kwds)
        return 'MyFormatter [%r: %r] says: %s' % (KEYWORD, self.value, message)


if __name__ == '__main__':
    lcd = LCDict(attach_handlers_to_root=True)
    lcd.add_formatter(
        'my_formatter',
        format='%(name)s - %(levelname)s - %(message)s',
        # dateformat=...,
        # style=?,
        **{
            '()': MyFormatter,
            KEYWORD: 'my_value'
        })
    lcd.add_stdout_handler('out', formatter='my_formatter')
    lcd.config()

    root = logging.getLogger()
    root.debug("Debug.")
    root.info("Info.")
    root.warning("Warning.")
    root.error("Error.")
    root.critical("Critical.")
    """
    MyFormatter ['my_keyword': 'my_value'] says: root - WARNING - Warning.
    MyFormatter ['my_keyword': 'my_value'] says: root - ERROR - Error.
    MyFormatter ['my_keyword': 'my_value'] says: root - CRITICAL - Critical.
    """