Example #1
0
def setup_logger():
    """
    Has to be called separately in every process.
    """
    import argparse
    from pysthal.command_line_util import init_logger
    import pylogging

    logger = pylogging.get(__name__)

    init_logger(
        "DEBUG",
        [
            (__name__, "TRACE"),
            ("halbe.fgwriter", "INFO"),
            ("hicann-system", "INFO"),
            ("Default", "INFO"),  # unset logger name, mostly hicann-system
        ])

    parser = argparse.ArgumentParser()
    parser.add_argument("--output-dir", default=".")
    args, _ = parser.parse_known_args()

    pylogging.append_to_file(os.path.join(args.output_dir, LOGFILE))

    return logger
Example #2
0
    def test_default_logger(self):
        log_all = os.path.join(self.temp, 'test_default_logger_all.log')
        log_default = os.path.join(self.temp,
                                   'test_default_logger_default.log')

        logger1 = logger.get("test")

        logger.default_config(logger.LogLevel.DEBUG,
                              log_all,
                              date_format="NULL")

        # Loglevel should be ignored, because the root logger is configured
        logger_default = logger.get_old_logger(logger.LogLevel.TRACE)
        logger.append_to_file(log_default, logger_default)

        for l in (logger_default, logger1):
            logger.LOG4CXX_FATAL(l, "FATAL")
            logger.LOG4CXX_ERROR(l, "ERROR")
            logger.LOG4CXX_WARN(l, "WARN")
            logger.LOG4CXX_INFO(l, "INFO")
            logger.LOG4CXX_DEBUG(l, "DEBUG")
            logger.LOG4CXX_TRACE(l, "TRACE")

        logger.reset()
        with open(log_all) as f:
            expected = \
"""FATAL PyLogging FATAL
ERROR PyLogging ERROR
WARN  PyLogging WARN
INFO  PyLogging INFO
DEBUG PyLogging DEBUG
FATAL test FATAL
ERROR test ERROR
WARN  test WARN
INFO  test INFO
DEBUG test DEBUG
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log_default) as f:
            expected = \
"""FATAL PyLogging FATAL
ERROR PyLogging ERROR
WARN  PyLogging WARN
INFO  PyLogging INFO
DEBUG PyLogging DEBUG
"""
            self.assertEqualLogLines(expected, f.read())
    def test_default_logger(self):
        log_all = os.path.join(self.temp, 'test_default_logger_all.log')
        log_default = os.path.join(self.temp, 'test_default_logger_default.log')

        logger1 = logger.get("test")

        logger.default_config(logger.LogLevel.DEBUG, log_all,
                date_format="NULL")

        # Loglevel should be ignored, because the root logger is configured
        logger_default = logger.get_old_logger(logger.LogLevel.TRACE)
        logger.append_to_file(log_default, logger_default)

        for l in (logger_default, logger1):
            logger.LOG4CXX_FATAL(l, "FATAL")
            logger.LOG4CXX_ERROR(l, "ERROR")
            logger.LOG4CXX_WARN (l, "WARN")
            logger.LOG4CXX_INFO (l, "INFO")
            logger.LOG4CXX_DEBUG(l, "DEBUG")
            logger.LOG4CXX_TRACE(l, "TRACE")

        logger.reset()
        with open(log_all) as f:
            expected = \
"""FATAL PyLogging FATAL
ERROR PyLogging ERROR
WARN  PyLogging WARN
INFO  PyLogging INFO
DEBUG PyLogging DEBUG
FATAL test FATAL
ERROR test ERROR
WARN  test WARN
INFO  test INFO
DEBUG test DEBUG
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log_default) as f:
            expected = \
"""FATAL PyLogging FATAL
ERROR PyLogging ERROR
WARN  PyLogging WARN
INFO  PyLogging INFO
DEBUG PyLogging DEBUG
"""
            self.assertEqualLogLines(expected, f.read())
def setup_logger():
    """
    Has to be called separately in every process.
    """
    import argparse
    from pysthal.command_line_util import init_logger
    import pylogging

    logger = pylogging.get(__name__)

    init_logger("DEBUG", [
        (__name__, "TRACE"),
        ("halbe.fgwriter", "INFO"),
        ("hicann-system", "INFO"),
        ("Default", "INFO"),  # unset logger name, mostly hicann-system
    ])

    parser = argparse.ArgumentParser()
    parser.add_argument("--output-dir", default=".")
    args, _ = parser.parse_known_args()

    pylogging.append_to_file(os.path.join(args.output_dir, LOGFILE))

    return logger
Example #5
0
    def test_file_logging_with_filter(self):
        logger1 = logger.get("test")
        logger2 = logger.get("xyz")
        logger3 = logger.get("xyz.test")

        logger.set_loglevel(logger1, logger.LogLevel.WARN)
        logger.set_loglevel(logger2, logger.LogLevel.DEBUG)
        logger.set_loglevel(logger3, logger.LogLevel.INFO)

        # Test different filter
        log = os.path.join(self.temp, 'test_file_logging_with_filter.log')
        app = logger.append_to_file(log, logger.get_root())
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.DEBUG)
        f.setLevelMax(logger.LogLevel.WARN)
        app.addFilter(f)

        log2 = os.path.join(self.temp, 'test_file_logging_with_filter2.log')
        app = logger.append_to_file(log2, logger.get_root())
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.ERROR)
        f.setLevelMax(logger.LogLevel.FATAL)
        app.addFilter(f)

        log3 = os.path.join(self.temp, 'test_file_logging_with_filter3.log')
        app = logger.append_to_file(log3, logger2)
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.ERROR)
        f.setLevelMax(logger.LogLevel.FATAL)
        app.addFilter(f)

        for l in (logger1, logger2, logger3):
            logger.LOG4CXX_FATAL(l, "FATAL")
            logger.LOG4CXX_ERROR(l, "ERROR")
            logger.LOG4CXX_WARN(l, "WARN")
            logger.LOG4CXX_INFO(l, "INFO")
            logger.LOG4CXX_DEBUG(l, "DEBUG")
            logger.LOG4CXX_TRACE(l, "TRACE")

        logger.reset()
        with open(log) as f:
            expected = """WARN  test WARN
WARN  xyz WARN
INFO  xyz INFO
DEBUG xyz DEBUG
WARN  xyz.test WARN
INFO  xyz.test INFO
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log2) as f:
            expected = """FATAL test FATAL
ERROR test ERROR
FATAL xyz FATAL
ERROR xyz ERROR
FATAL xyz.test FATAL
ERROR xyz.test ERROR
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log3) as f:
            expected = """FATAL xyz FATAL
ERROR xyz ERROR
FATAL xyz.test FATAL
ERROR xyz.test ERROR
"""
            self.assertEqualLogLines(expected, f.read())
    def test_file_logging_with_filter(self):
        logger1 = logger.get("test");
        logger2 = logger.get("xyz");
        logger3 = logger.get("xyz.test");

        logger.set_loglevel(logger1, logger.LogLevel.WARN)
        logger.set_loglevel(logger2, logger.LogLevel.DEBUG)
        logger.set_loglevel(logger3, logger.LogLevel.INFO)

        # Test different filter
        log = os.path.join(self.temp, 'test_file_logging_with_filter.log')
        app = logger.append_to_file(log, logger.get_root())
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.DEBUG)
        f.setLevelMax(logger.LogLevel.WARN)
        app.addFilter(f)

        log2 = os.path.join(self.temp, 'test_file_logging_with_filter2.log')
        app = logger.append_to_file(log2, logger.get_root())
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.ERROR)
        f.setLevelMax(logger.LogLevel.FATAL)
        app.addFilter(f)

        log3 = os.path.join(self.temp, 'test_file_logging_with_filter3.log')
        app = logger.append_to_file(log3, logger2)
        f = logger.LevelRangeFilter()
        f.setLevelMin(logger.LogLevel.ERROR)
        f.setLevelMax(logger.LogLevel.FATAL)
        app.addFilter(f)


        for l in (logger1, logger2, logger3):
            logger.LOG4CXX_FATAL(l, "FATAL")
            logger.LOG4CXX_ERROR(l, "ERROR")
            logger.LOG4CXX_WARN (l, "WARN")
            logger.LOG4CXX_INFO (l, "INFO")
            logger.LOG4CXX_DEBUG(l, "DEBUG")
            logger.LOG4CXX_TRACE(l, "TRACE")

        logger.reset()
        with open(log) as f:
            expected =  """WARN  test WARN
WARN  xyz WARN
INFO  xyz INFO
DEBUG xyz DEBUG
WARN  xyz.test WARN
INFO  xyz.test INFO
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log2) as f:
            expected = """FATAL test FATAL
ERROR test ERROR
FATAL xyz FATAL
ERROR xyz ERROR
FATAL xyz.test FATAL
ERROR xyz.test ERROR
"""
            self.assertEqualLogLines(expected, f.read())

        with open(log3) as f:
            expected = """FATAL xyz FATAL
ERROR xyz ERROR
FATAL xyz.test FATAL
ERROR xyz.test ERROR
"""
            self.assertEqualLogLines(expected, f.read())