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()
Esempio n. 2
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()
Esempio 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()
Esempio n. 4
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'
            }
        })
Esempio n. 5
0
def config_logging(use_locking):
    logfilename = 'logfile (%s).log' % ('LOCKING'
                                        if use_locking else 'NOLOCKING')
    lcd = LCDict(log_path='_log/mproc_deco/',
                 root_level='DEBUG',
                 attach_handlers_to_root=True,
                 locking=use_locking)
    # Set up console handler to show process name, time, handler name
    lcd.add_stderr_handler('console',
                           formatter='process_time_logger_level_msg',
                           level='INFO')
    # Add main file handler, which will write to log_path + '/' + logfilename
    lcd.add_file_handler(
        'app_file',
        filename=logfilename,
        mode='w',
        formatter='process_time_logger_level_msg',
    )
    lcd.config()