Example #1
0
def logging_context(path=None, level=None):
    from logbook import StderrHandler, FileHandler
    from logbook.compat import redirected_logging
    with StderrHandler(level=level or 'INFO').applicationbound():
        if path:
            if not os.path.isdir(os.path.dirname(path)):
                os.makedirs(os.path.dirname(path))
            with FileHandler(path, bubble=True).applicationbound():
                with redirected_logging():
                    yield
        else:
            with redirected_logging():
                yield
Example #2
0
def test_basic_compat(request, set_root_logger_level):
    import logging
    from logbook.compat import redirected_logging

    # mimic the default logging setting
    request.addfinalizer(functools.partial(
        logging.root.setLevel, logging.root.level))
    logging.root.setLevel(logging.WARNING)

    name = 'test_logbook-%d' % randrange(1 << 32)
    logger = logging.getLogger(name)

    with logbook.TestHandler(bubble=True) as handler:
        with capturing_stderr_context() as captured:
            with redirected_logging(set_root_logger_level):
                logger.debug('This is from the old system')
                logger.info('This is from the old system')
                logger.warn('This is from the old %s', 'system')
                logger.error('This is from the old system')
                logger.critical('This is from the old system')
                logger.error('This is a %(what)s %(where)s', {'what': 'mapping', 'where': 'test'})
        assert ('WARNING: %s: This is from the old system' %
                name) in captured.getvalue()
        assert ('ERROR: %s: This is a mapping test' %
                name) in captured.getvalue()
    if set_root_logger_level:
        assert handler.records[0].level == logbook.DEBUG
    else:
        assert handler.records[0].level == logbook.WARNING
        assert handler.records[0].msg == 'This is from the old %s'
Example #3
0
 def wrap_analysis_main(argv=None):
     """
     Actual main function for analysis code, deals with parsers and
     logging
     """
     if argv is None:
         argv = sys.argv[1:]
     cmd_args = {}
     parser = argparse.ArgumentParser(
         description=cmd_description,
         argument_default=argparse.SUPPRESS,
     )
     parser.add_argument(
         '--version', action='version',
         version='%(prog)s ' + skw_version
     )
     parser.add_argument("soln_file")
     logging_options(parser)
     cmd_parser = cmd_parser_func(parser)
     args = vars(cmd_parser.parse_args(argv))
     for name, func in cmd_parser_splitters.items():
         cmd_args[name] = func(args)
     with log_handler(args):
         with redirected_warnings(), redirected_logging():
             with h5open(args["soln_file"], registries) as soln_file:
                 solutions = soln_file["run"].solutions.values()
                 return cmd(
                     *solutions, **cmd_args
                 )
Example #4
0
 def wrap_analysis_main(argv=None):
     """
     Actual main function for analysis code, deals with parsers and
     logging
     """
     if argv is None:
         argv = sys.argv[1:]
     cmd_args = {}
     parser = argparse.ArgumentParser(
         description=cmd_description,
         argument_default=argparse.SUPPRESS,
     )
     parser.add_argument(
         '--version', action='version',
         version='%(prog)s ' + skw_version
     )
     parser.add_argument(
         "-r", "--runs", action="append", nargs=2,
         metavar=("soln_file", "soln_range"), dest="soln_pairs",
     )
     logging_options(parser)
     cmd_parser = cmd_parser_func(parser)
     args = vars(cmd_parser.parse_args(argv))
     for name, func in cmd_parser_splitters.items():
         cmd_args[name] = func(args)
     with log_handler(args):
         with redirected_warnings(), redirected_logging():
             return cmd(args["soln_pairs"], **cmd_args)
Example #5
0
def test_standard_logging_works():
    with TestHandler() as handler:
        logger = logging.getLogger('Dummy')
        with redirected_logging():
            logger.info('hello world')
        assert handler.formatted_records == [
            '[INFO] Dummy: hello world'
        ]
Example #6
0
def stats_main(argv, parser):
    """
    Main entry point for ds-stats
    """
    parser.add_argument("--file", default='-')
    parser.add_argument("--with-stdin", action='store_true', default=False)
    parser.add_argument("solution_files", nargs='*')
    args = parser.parse_args(argv)
    with log_handler(args), redirected_warnings(), redirected_logging():
        return write_stats(get_all_solutions(get_all_files(args)),
                           output_file=args.file)
Example #7
0
def reduce_koala_data_main(argv=None):
    """
    Main entry point for reduce-koala-data
    """
    if argv is None:
        argv = sys.argv[1:]

    args = parse_reduce_koala_data_cli(argv)

    with log_handler(args), redirected_warnings(), redirected_logging():
        config = parse_reduce_koala_data_config(args)

        reduce_koala_data(config)
Example #8
0
def test_mdc_works():
    def inject_extra(record):
        record.extra['ip'] = '127.0.0.1'
        record.extra['username'] = '******'

    with TestHandler() as handler:
        handler.formatter = text_formatter
        logger = logging.getLogger('Dummy')
        with redirected_logging():
            with Processor(inject_extra):
                logger.info('hello world')
        assert len(handler.formatted_records) == 1
        assert 'INFO: Dummy: hello world <ip=127.0.0.1, username=Andrey>' in handler.formatted_records[0]
Example #9
0
def test_exception_text_formatter():
    with TestHandler() as handler:
        handler.formatter = text_formatter
        logger = logging.getLogger('Dummy')
        with redirected_logging():
            try:
                raise Exception('Something bad!')
            except Exception:
                logger.error('hello world', exc_info=True)
        assert len(handler.formatted_records) == 1
        record = handler.formatted_records[0]
        assert 'ERROR: Dummy: hello world' in record
        assert '/logbook_test.py' in record
        assert 'Exception: Something bad!' in record
Example #10
0
    def test_basic_compat(self):
        from logging import getLogger
        from logbook.compat import redirected_logging

        name = 'test_logbook-%d' % randrange(1 << 32)
        logger = getLogger(name)
        with capture_stderr() as captured:
            with redirected_logging():
                logger.debug('This is from the old system')
                logger.info('This is from the old system')
                logger.warn('This is from the old system')
                logger.error('This is from the old system')
                logger.critical('This is from the old system')
            self.assert_(('WARNING: %s: This is from the old system' % name)
                         in captured.getvalue())
Example #11
0
    def test_basic_compat(self):
        from logging import getLogger
        from logbook.compat import redirected_logging

        name = 'test_logbook-%d' % randrange(1 << 32)
        logger = getLogger(name)
        with capture_stderr() as captured:
            with redirected_logging():
                logger.debug('This is from the old system')
                logger.info('This is from the old system')
                logger.warn('This is from the old system')
                logger.error('This is from the old system')
                logger.critical('This is from the old system')
            self.assert_(('WARNING: %s: This is from the old system' % name)
                         in captured.getvalue())
Example #12
0
def test_exception_json_formatter():
    with TestHandler() as handler:
        handler.formatter = json_formatter
        logger = logging.getLogger('Dummy')
        with redirected_logging():
            try:
                raise Exception('Something bad!')
            except Exception:
                logger.error('hello world', exc_info=True)
        assert len(handler.formatted_records) == 1
        record = json.loads(handler.formatted_records[0])
        assert record['level'] == 'ERROR'
        assert record['name'] == 'Dummy'
        assert record['message'] == 'hello world'
        assert '/logbook_test.py' in record['exception']
        assert 'Exception: Something bad!' in record['exception']
Example #13
0
 def wrap_analysis_main(argv, parser):
     """
     Actual main function for analysis code, deals with parsers and
     logging
     """
     cmd_args = {}
     parser.add_argument(
         "-r", "--runs", action="append", nargs=2,
         metavar=("soln_file", "soln_range"), dest="soln_pairs",
     )
     cmd_parser = cmd_parser_func(parser)
     args = vars(cmd_parser.parse_args(argv))
     for name, func in cmd_parser_splitters.items():
         cmd_args[name] = func(args)
     with log_handler(args):
         with redirected_warnings(), redirected_logging():
             return cmd(args["soln_pairs"], **cmd_args)
Example #14
0
def test_json_formatting_works():
    def inject_extra(record):
        record.extra['ip'] = '127.0.0.1'
        record.extra['username'] = '******'

    with TestHandler() as handler:
        handler.formatter = json_formatter
        logger = logging.getLogger('Dummy')
        with redirected_logging():
            with Processor(inject_extra):
                logger.info('hello world')
        assert len(handler.formatted_records) == 1
        record = json.loads(handler.formatted_records[0])
        assert record['level'] == 'INFO'
        assert record['name'] == 'Dummy'
        assert record['message'] == 'hello world'
        assert record['ip'] == '127.0.0.1'
        assert record['username'] == 'Andrey'
Example #15
0
 def wrap_analysis_main(argv, parser):
     """
     Actual main function for analysis code, deals with parsers and
     logging
     """
     cmd_args = {}
     parser.add_argument("soln_file")
     parser.add_argument("soln_range")
     cmd_parser = cmd_parser_func(parser)
     args = vars(cmd_parser.parse_args(argv))
     for name, func in cmd_parser_splitters.items():
         cmd_args[name] = func(args)
     with log_handler(args):
         with redirected_warnings(), redirected_logging():
             return cmd(
                 args["soln_file"],
                 soln_range=args["soln_range"],
                 **cmd_args
             )
Example #16
0
def test_asyncio():
    logger = logging.getLogger('Dummy')

    async def util():
        for i in range(3):
            logger.info('I am the util function', extra={'iteration': i+1})
            await asyncio.sleep(0)

    async def task(task_name: str):
        def inject_extra(record):
            record.extra['task_name'] = task_name
            record.extra['task_id'] = id(asyncio.current_task())

        with Handler(bubble=True).contextbound():
            with Processor(inject_extra).contextbound():
                logger.info('I am the task')
                await asyncio.sleep(0)
                await util()
                logger.info('I am still the task')

    root_handler = TestHandler()
    root_handler.formatter = text_formatter
    with root_handler.applicationbound():
        with redirected_logging():
            asyncio.get_event_loop().run_until_complete(asyncio.gather(task('one'), task('two'), task('three')))

    records = root_handler.formatted_records
    assert 'INFO: Dummy: I am the task <task_name=one' in records[0]
    assert 'INFO: Dummy: I am the task <task_name=two' in records[1]
    assert 'INFO: Dummy: I am the task <task_name=three' in records[2]
    assert 'INFO: Dummy: I am the util function <iteration=1, task_name=one' in records[3]
    assert 'INFO: Dummy: I am the util function <iteration=1, task_name=two' in records[4]
    assert 'INFO: Dummy: I am the util function <iteration=1, task_name=three' in records[5]
    assert 'INFO: Dummy: I am the util function <iteration=2, task_name=one' in records[6]
    assert 'INFO: Dummy: I am the util function <iteration=2, task_name=two' in records[7]
    assert 'INFO: Dummy: I am the util function <iteration=2, task_name=three' in records[8]
    assert 'INFO: Dummy: I am the util function <iteration=3, task_name=one' in records[9]
    assert 'INFO: Dummy: I am the util function <iteration=3, task_name=two' in records[10]
    assert 'INFO: Dummy: I am the util function <iteration=3, task_name=three' in records[11]
    assert 'INFO: Dummy: I am still the task <task_name=one' in records[12]
    assert 'INFO: Dummy: I am still the task <task_name=two' in records[13]
    assert 'INFO: Dummy: I am still the task <task_name=three' in records[14]
Example #17
0
def main(argv, parser):
    """
    Entry point for ds-resoln
    """
    add_solver_arguments(parser)
    parser.add_argument("soln_filename", type=expanded_path)
    parser.add_argument("soln_range")

    args = parser.parse_args(argv)

    overrides = validate_overrides(args.override)

    with log_handler(args), redirected_warnings(), redirected_logging():
        filename, succeeded = resolve(
            soln_filename=args.soln_filename, soln_range=args.soln_range,
            output_file=args.output_file, sonic_method=args.sonic_method,
            output_dir=args.output_dir, store_internal=args.store_internal,
            overrides=overrides,
        )
        print(filename)
        return int(not succeeded)
Example #18
0
    def test_basic_compat(self):
        from logging import getLogger
        from logbook.compat import redirected_logging

        name = 'test_logbook-%d' % randrange(1 << 32)
        logger = getLogger(name)
        captured = capture_stderr.start()
        try:
            redirector = redirected_logging()
            redirector.start()
            try:
                logger.debug('This is from the old system')
                logger.info('This is from the old system')
                logger.warn('This is from the old system')
                logger.error('This is from the old system')
                logger.critical('This is from the old system')
            finally:
                redirector.end()
            self.assert_(('WARNING: %s: This is from the old system' % name)
                         in captured.getvalue())
        finally:
            capture_stderr.end()
Example #19
0
 def wrap_analysis_main(argv, parser):
     """
     Actual main function for analysis code, deals with parsers and
     logging
     """
     cmd_args = {}
     parser.add_argument("soln_file")
     cmd_parser = cmd_parser_func(parser)
     args = vars(cmd_parser.parse_args(argv))
     for name, func in cmd_parser_splitters.items():
         cmd_args[name] = func(args)
     with log_handler(args):
         with redirected_warnings(), redirected_logging():
             with h5open(
                 args["soln_file"], registries, mode='r'
             ) as soln_file:
                 solutions = sorted(
                     soln_file["run"].solutions.items(),
                     key=lambda x: int(x[0])
                 )
                 return cmd(
                     solutions, **cmd_args
                 )
Example #20
0
def main():
    """
    Entry point for skw-full-soln
    """
    parser = argparse.ArgumentParser(description='Solver for skw_full_code')
    add_solver_arguments(parser)
    parser.add_argument("config_file")

    args = vars(parser.parse_args())

    config_file = expanded_path(args["config_file"])
    output_dir = expanded_path(args["output_dir"])
    output_file = args.get("output_file", None)
    overrides = validate_overrides(args.get("override", []))

    with log_handler(args), redirected_warnings(), redirected_logging():
        print(
            solve(
                output_file=output_file,
                config_file=config_file,
                output_dir=output_dir,
                overrides=overrides,
            ))