Python loguru.logger.configure() Examples

The following are 25 code examples of loguru.logger.configure(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module loguru.logger , or try the search function .
Example #1
Source File: test_configure.py    From loguru with MIT License 6 votes vote down vote up
def test_handlers(capsys, tmpdir):
    file = tmpdir.join("test.log")

    handlers = [
        {"sink": str(file), "format": "FileSink: {message}"},
        {"sink": sys.stdout, "format": "StdoutSink: {message}"},
    ]

    logger.configure(handlers=handlers)
    logger.debug("test")

    out, err = capsys.readouterr()

    assert file.read() == "FileSink: test\n"
    assert out == "StdoutSink: test\n"
    assert err == "" 
Example #2
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_dont_reset_by_default(writer):
    logger.configure(extra={"a": 1}, patcher=lambda r: r["extra"].update(b=2))
    logger.level("b", no=30)
    logger.add(writer, format="{level} {extra[a]} {extra[b]} {message}")

    logger.configure()

    logger.log("b", "Test")

    assert writer.read() == "b 1 2 Test\n" 
Example #3
Source File: test_patch.py    From loguru with MIT License 5 votes vote down vote up
def test_add_using_patched(writer):
    logger.configure(patcher=lambda r: r["extra"].update(a=-1))
    logger_patched = logger.patch(lambda r: r["extra"].update(a=0))
    logger_patched.add(writer, format="{extra[a]} {message}")
    logger.debug("A")
    logger_patched.debug("B")

    assert writer.read() == "-1 A\n0 B\n" 
Example #4
Source File: test_contextualize.py    From loguru with MIT License 5 votes vote down vote up
def test_contextualize_after_configure(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    with logger.contextualize(foobar="baz"):
        logger.configure(extra={"foobar": "baz_2"})
        logger.info("A")

    logger.info("B")

    assert writer.read() == "A baz\nB baz_2\n" 
Example #5
Source File: test_contextualize.py    From loguru with MIT License 5 votes vote down vote up
def test_contextualize_before_configure(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    logger.configure(extra={"foobar": "baz"})

    with logger.contextualize(foobar="baz_2"):
        logger.info("A")

    logger.info("B")

    assert writer.read() == "A baz_2\nB baz\n" 
Example #6
Source File: test_bind.py    From loguru with MIT License 5 votes vote down vote up
def test_override_configured(writer):
    logger.configure(extra={"a": 1})
    logger2 = logger.bind(a=2)

    logger2.add(writer, format="{extra[a]} {message}")

    logger2.debug("?")

    assert writer.read() == "2 ?\n" 
Example #7
Source File: test_bind.py    From loguru with MIT License 5 votes vote down vote up
def test_add_using_bound(writer):
    logger.configure(extra={"a": -1})
    logger_bound = logger.bind(a=0)
    logger_bound.add(writer, format="{extra[a]} {message}")
    logger.debug("A")
    logger_bound.debug("B")

    assert writer.read() == "-1 A\n0 B\n" 
Example #8
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_configure_after_bind(writer):
    logger_a = logger.bind(a="A")
    logger_b = logger.bind(b="B")

    logger.configure(extra={"a": "default_a", "b": "default_b"})
    logger.add(writer, format="{extra[a]} {extra[b]} {message}")

    logger.debug("init")

    logger_a.debug("aaa")
    logger_b.debug("bbb")

    assert writer.read() == ("default_a default_b init\n" "A default_b aaa\n" "default_a B bbb\n") 
Example #9
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_configure_before_bind(writer):
    logger.configure(extra={"a": "default_a", "b": "default_b"})
    logger.add(writer, format="{extra[a]} {extra[b]} {message}")

    logger.debug("init")

    logger_a = logger.bind(a="A")
    logger_b = logger.bind(b="B")

    logger_a.debug("aaa")
    logger_b.debug("bbb")

    assert writer.read() == ("default_a default_b init\n" "A default_b aaa\n" "default_a B bbb\n") 
Example #10
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_configure_filter_using_new_level(writer):
    logger.configure(
        levels=[{"name": "CONF_LVL_2", "no": 33, "icon": "", "color": ""}],
        handlers=[
            {"sink": writer, "level": 0, "filter": {"tests": "CONF_LVL_2"}, "format": "{message}"}
        ],
    )

    logger.log("CONF_LVL_2", "Custom")
    assert writer.read() == "Custom\n" 
Example #11
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_configure_handler_using_new_level(writer):
    logger.configure(
        levels=[{"name": "CONF_LVL", "no": 33, "icon": "", "color": ""}],
        handlers=[
            {"sink": writer, "level": "CONF_LVL", "format": "{level.name} {level.no} {message}"}
        ],
    )

    logger.log("CONF_LVL", "Custom")
    assert writer.read() == "CONF_LVL 33 Custom\n" 
Example #12
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_dont_reset_previous_levels(writer):
    logger.level("abc", no=30)

    logger.configure(levels=[])

    logger.add(writer, format="{level} {message}")

    logger.log("abc", "Test")

    assert writer.read() == "abc Test\n" 
Example #13
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_reset_previous_extra(writer):
    logger.configure(extra={"a": 123})
    logger.add(writer, format="{extra[a]}", catch=False)

    logger.configure(extra={})

    with pytest.raises(KeyError):
        logger.debug("Nope") 
Example #14
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_reset_previous_handlers(writer):
    logger.add(writer, format="{message}")

    logger.configure(handlers=[])

    logger.debug("Test")

    assert writer.read() == "" 
Example #15
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_returned_ids(capsys):
    ids = logger.configure(
        handlers=[
            {"sink": sys.stdout, "format": "{message}"},
            {"sink": sys.stderr, "format": "{message}"},
        ]
    )

    assert len(ids) == 2

    logger.debug("Test")

    out, err = capsys.readouterr()

    assert out == "Test\n"
    assert err == "Test\n"

    for i in ids:
        logger.remove(i)

    logger.debug("Nope")

    out, err = capsys.readouterr()

    assert out == ""
    assert err == "" 
Example #16
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_dict_unpacking(writer):
    config = {
        "handlers": [{"sink": writer, "format": "{level.no} - {extra[x]} {extra[z]} - {message}"}],
        "levels": [{"name": "test", "no": 30}],
        "extra": {"x": 1, "y": 2, "z": 3},
    }

    logger.debug("NOPE")

    logger.configure(**config)

    logger.log("test", "Yes!")

    assert writer.read() == "30 - 1 3 - Yes!\n" 
Example #17
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_patcher(writer):
    logger.add(writer, format="{extra[a]} {extra[b]}")
    logger.configure(patcher=lambda record: record["extra"].update(a=1, b=2))

    logger.debug("")

    assert writer.read() == "1 2\n" 
Example #18
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_extra(writer):
    extra = {"a": 1, "b": 9}

    logger.add(writer, format="{extra[a]} {extra[b]}")
    logger.configure(extra=extra)

    logger.debug("")

    assert writer.read() == "1 9\n" 
Example #19
Source File: test_configure.py    From loguru with MIT License 5 votes vote down vote up
def test_levels(writer):
    levels = [{"name": "my_level", "icon": "X", "no": 12}, {"name": "DEBUG", "icon": "!"}]

    logger.add(writer, format="{level.no}|{level.name}|{level.icon}|{message}")
    logger.configure(levels=levels)

    logger.log("my_level", "test")
    logger.debug("no bug")

    assert writer.read() == ("12|my_level|X|test\n" "10|DEBUG|!|no bug\n") 
Example #20
Source File: test_opt.py    From loguru with MIT License 5 votes vote down vote up
def test_keep_extra(writer):
    logger.configure(extra=dict(test=123))
    logger.add(writer, format="{extra[test]}")
    logger.opt().debug("")
    logger.opt().log(50, "")

    assert writer.read() == "123\n123\n" 
Example #21
Source File: __init__.py    From aria2p with ISC License 5 votes vote down vote up
def enable_logger(sink=sys.stderr, level="WARNING"):
    """
    Enable the logging of messages.

    Configure the ``logger`` variable imported from ``loguru``.

    Args:
        sink (file): An opened file pointer, or stream handler. Default to standard error.
        level (str): The log level to use. Possible values are TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL.
            Default to WARNING.
    """
    logger.remove()
    logger.configure(handlers=[{"sink": sink, "level": level}])
    logger.enable("aria2p") 
Example #22
Source File: run.py    From ScaffoldGraph with MIT License 5 votes vote down vote up
def configure_logger(verbosity):
    """Configure the scaffoldgraph cli logger to use tqdm handler"""

    config = {'handlers': []}
    logger.enable('scaffoldgraph')

    if verbosity == 0:
        tqdm_handler['sink'].level = logging.CRITICAL
        tqdm_handler['level'] = 'CRITICAL'
    elif verbosity == 1:
        tqdm_handler['sink'].level = logging.ERROR
        tqdm_handler['level'] = 'ERROR'
    elif verbosity == 2:
        tqdm_handler['sink'].level = logging.WARNING
        tqdm_handler['level'] = 'WARNING'
    elif verbosity == 3:
        tqdm_handler['sink'].level = logging.INFO
        tqdm_handler['level'] = 'INFO'
    elif verbosity == 4:
        tqdm_handler['sink'].level = logging.DEBUG
        tqdm_handler['level'] = 'DEBUG'
    else:  # if < 0 or > 4 is supplied set logger to max level (DEBUG)
        tqdm_handler['sink'].level = logging.DEBUG
        tqdm_handler['level'] = 'DEBUG'

    config["handlers"].append(tqdm_handler)
    logger.configure(**config) 
Example #23
Source File: logger.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def configure_logging(level=logging.INFO, enable_loguru=True):
    faulthandler.enable()
    # https://stackoverflow.com/questions/45522159/dont-log-certificate-did-not-match-expected-hostname-error-messages
    logging.getLogger('urllib3.connection').setLevel(logging.CRITICAL)
    logging.getLogger('readability.readability').setLevel(logging.WARNING)
    if enable_loguru:
        loguru_patch()
        logging.basicConfig(
            handlers=[InterceptHandler()], level=level,
            format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT)
        loguru_logger.configure(handlers=[LOGURU_HANDLER])
    else:
        logging.basicConfig(
            level=level, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT) 
Example #24
Source File: __init__.py    From ThreatIngestor with GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, config_file):
        # Load config.
        try:
            logger.debug(f"Reading config from '{config_file}'")
            self.config = config.Config(config_file)
        except (OSError, threatingestor.exceptions.IngestorError):
            # Error loading config.
            logger.exception("Couldn't read config")
            sys.exit(1)

        # Configure logging with optional notifiers.
        logger.configure(**self.config.logging())
        logger.level("NOTIFY", no=35, color="<yellow>", icon="\U0001F514")

        if notifiers:
            notifier_config = self.config.notifiers()
            notifier = notifiers.get_notifier(notifier_config.get('provider'))

            if notifier:
                logger.debug(f"Adding notification handler '{notifier_config.get('provider')}'")
                # Notifier 'provider_name' is set and valid.
                handler = NotificationHandler(**notifier_config)
                logger.add(handler, level="NOTIFY")

        logger.debug("Log handler reconfigured")

        # Configure statsd.
        try:
            self.statsd = statsd.StatsClient(**self.config.statsd())
            self.statsd.incr('start')
        except TypeError:
            logger.exception("Couldn't initialize statsd client; bad config?")
            sys.exit(1)

        # Load state DB.
        try:
            logger.debug(f"Opening state database '{self.config.state_path()}'")
            self.statedb = threatingestor.state.State(self.config.state_path())
        except (OSError, IOError, threatingestor.exceptions.IngestorError):
            # Error loading state DB.
            logger.exception("Error reading state database")
            sys.exit(1)

        # Instantiate plugins.
        try:
            logger.debug("Initializing sources")
            self.sources = {name: source(**kwargs)
                            for name, source, kwargs in self.config.sources()}

            logger.debug("Initializing operators")
            self.operators = {name: operator(**kwargs)
                              for name, operator, kwargs in self.config.operators()}

        except (TypeError, ConnectionError, threatingestor.exceptions.PluginError):
            logger.warning("Twitter config format has recently changed. See https://github.com/InQuest/ThreatIngestor/releases/tag/v1.0.0b5")
            logger.exception("Error initializing plugins")
            sys.exit(1) 
Example #25
Source File: logs.py    From veros with MIT License 4 votes vote down vote up
def setup_logging(loglevel='info', stream_sink=sys.stdout):
    from . import runtime_state, runtime_settings

    handler_conf = dict(
        sink=stream_sink,
        level=loglevel.upper(),
        colorize=sys.stdout.isatty(),
    )

    logger.level('TRACE', color='<dim>')
    logger.level('DEBUG', color='<dim><cyan>')
    logger.level('INFO', color='')
    logger.level('SUCCESS', color='<dim><green>')
    logger.level('WARNING', color='<yellow>')
    logger.level('ERROR', color='<bold><red>')
    logger.level('DIAGNOSTIC', color='<bold><yellow>')
    logger.level('CRITICAL', color='<bold><red><WHITE>')

    if runtime_settings.log_all_processes:
        handler_conf.update(
            format=f'{runtime_state.proc_rank} | <level>{{message}}</level>'
        )
    else:
        handler_conf.update(
            format='<level>{message}</level>',
            filter=lambda record: runtime_state.proc_rank == 0
        )

    def diagnostic(_, message, *args, **kwargs):
        logger.opt(depth=1).log('DIAGNOSTIC', message, *args, **kwargs)

    logger.__class__.diagnostic = diagnostic

    def showwarning(message, cls, source, lineno, *args):
        logger.warning(
            '{warning}: {message} ({source}:{lineno})',
            message=message,
            warning=cls.__name__,
            source=source,
            lineno=lineno
        )

    warnings.showwarning = showwarning

    veros_logger = logger.configure(handlers=[handler_conf])
    logger.enable('veros')

    return veros_logger