Exemplo n.º 1
0
def init_logging_config(logfilename):

    lcd = LCDict(log_path=LOG_PATH, attach_handlers_to_root=True)
    lcd.set_root_level('DEBUG')

    # Set format of console handler to show logger name and loglevel.
    ## Make sure 'console' handler level is higher than DEBUG
    lcd.add_formatter('busier_console_fmt',
                      format='%(name)-34s: %(levelname)-8s: %(message)s'
    ).add_stderr_handler('console',
                         formatter='busier_console_fmt',
                         level='INFO'
    )
    # Add main file handler that writes to '_log/child_loggers2/' + logfilename,
    # and add a logger that uses it
    lcd.add_formatter(
        'my_file_formatter',
        format='%(name)-34s: %(levelname)-8s: %(asctime)24s: %(message)s'
    ).add_file_handler(
        'app_file',
        filename=logfilename,
        mode='w',
        level='DEBUG',
        formatter='my_file_formatter'
    )
    return lcd
def main_process_config_logging():
    # DON'T attach handlers to root
    lcd = LCDict(log_path='_log/mproc_QHLT', root_level='DEBUG')

    lcd.add_formatter(
        'detailed',
        format='%(asctime)s %(name)-15s %(levelname)-8s '
        '%(processName)-10s %(message)s',
    ).add_formatter('less-detailed',
                    format='%(name)-15s %(levelname)-8s '
                    '%(processName)-10s %(message)s')

    # lcd.add_stderr_handler('console', level='INFO',
    #                                   formatter='less-detailed')

    lcd.add_file_handler(
        'file', filename='mplog.log', mode='w',
        formatter='detailed').add_file_handler(
            'errors',
            level='ERROR',
            filename='mplog-errors.log',
            mode='w',
            formatter='detailed').attach_root_handlers(  # 'console',
                'file', 'errors')

    lcd.add_file_handler('foofile',
                         filename='mplog-foo.log',
                         mode='w',
                         formatter='detailed').add_logger('foo',
                                                          handlers='foofile')

    lcd.config()
Exemplo 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()
Exemplo n.º 4
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()
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)
Exemplo n.º 6
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()
Exemplo n.º 7
0
def init_logging_config(loggername, logfilename):
    # add handlers to root == False (default)
    lcd = LCDict(log_path=LOG_PATH)

    # Create stderr console handler; output shows logger name and loglevel;
    ## loglevel higher than DEBUG.
    lcd.add_formatter('busier_console_fmt',
                      format='%(name)-40s: %(levelname)-8s: %(message)s'
    ).add_stderr_handler('console',
                         formatter='busier_console_fmt',
                         level='INFO'
    )
    # Add main file handler, which will write to LOG_PATH + '/' + logfilename,
    # and add logger (loggername == __name__) that uses it
    lcd.add_formatter('my_file_formatter',
                      format='%(name)-40s: %(levelname)-8s: '
                             '%(asctime)24s: %(message)s'
    ).add_file_handler('app_file',
                       filename=logfilename,
                       mode='w',
                       level='DEBUG',
                       formatter='my_file_formatter'
    ).add_logger(loggername,
                 handlers=('app_file', 'console'),
                 level='DEBUG',
                 propagate=False    # so it DOESN'T propagate to parent logger
    )
    return lcd
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)
Exemplo n.º 10
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()
Exemplo n.º 11
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!')
Exemplo n.º 12
0
        self.value = kwargs.pop(KEYWORD, '')
        kwargs.pop('class', None)
        s = super(MyFormatter, self).__init__(**kwargs)

    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.

def formatter_factory(**kwargs):
    format = kwargs.pop('format')
    formatter = lambda record: callable_formatter(record, format, **kwargs)
    formatter.format = formatter  # `logging` requires this
    return formatter


if __name__ == '__main__':
    lcd = LCDict(attach_handlers_to_root=True)
    lcd.add_formatter(
        'my_formatter',
        format='%(name)s - %(levelname)s - %(msg)s',
        # dateformat=...,
        # style='%',
        **{
            '()': formatter_factory,
            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.")
    """
    callable_formatter [ 'my_keyword': 'my_value']: root - WARNING - Warning.