How to use loguru - 10 common examples

To help you get started, we’ve selected a few loguru examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / CCF / tests / infra / clients.py View on Github external
cmd += [
                f"https://{self.host}:{self.port}/{request.method}",
                "-H",
                "Content-Type: application/json",
                "--data-binary",
                f"@{nf.name}",
                "-w \\n%{http_code}",
            ]

            if self.ca:
                cmd.extend(["--cacert", self.ca])
            if self.key:
                cmd.extend(["--key", self.key])
            if self.cert:
                cmd.extend(["--cert", self.cert])
            LOG.debug(f"Running: {' '.join(cmd)}")
            rc = subprocess.run(cmd, capture_output=True)

            if rc.returncode != 0:
                if rc.returncode == 60:
                    raise CurlClientSSLException
                LOG.error(rc.stderr)
                raise RuntimeError(f"Curl failed with return code {rc.returncode}")

            # The response status code is displayed on the last line of
            # the output (via -w option)
            rep, status_code = rc.stdout.decode().rsplit("\n", 1)
            if int(status_code) != 200:
                LOG.error(rep)
                raise RuntimeError(f"Curl failed with status code {status_code}")

            self.stream.update(rep.encode())
github Delgan / loguru / tests / exceptions / source / ownership / indirect.py View on Github external
def test(*, backtrace, colorize, diagnose):
    logger.remove()
    logger.add(sys.stderr, format="", colorize=colorize, backtrace=backtrace, diagnose=diagnose)

    try:
        divide_indirect(10, 0)
    except ZeroDivisionError:
        logger.exception("")
github Delgan / loguru / tests / test_multiprocessing.py View on Github external
def test_remove_in_main_process_inheritance():
    writer = Writer()
    barrier = multiprocessing.Barrier(2)

    logger.add(writer, format="{message}", enqueue=True, catch=False)

    process = multiprocessing.Process(target=subworker_barrier_inheritance, args=(barrier,))
    process.start()
    barrier.wait()
    logger.info("Main")
    logger.remove()
    process.join()

    assert process.exitcode == 0

    assert writer.read() == "Child\nMain\n"
github Delgan / loguru / tests / exceptions / source / others / zerodivisionerror_without_traceback.py View on Github external
def test(diagnose, backtrace):
    logger.remove()
    logger.add(sys.stderr, format="", diagnose=diagnose, backtrace=backtrace, colorize=True)

    try:
        1 / 0
    except ZeroDivisionError:
        type_, value, _ = sys.exc_info()
        logger.opt(exception=(type_, value, None)).error("")
github Delgan / loguru / tests / exceptions / source / others / assertionerror_without_traceback.py View on Github external
def test(diagnose, backtrace):
    logger.remove()
    logger.add(sys.stderr, format="", diagnose=diagnose, backtrace=backtrace, colorize=True)

    try:
        assert False
    except AssertionError:
        type_, value, _ = sys.exc_info()
        logger.opt(exception=(type_, value, None)).error("")
github microsoft / CCF / tests / recovery.py View on Github external
check = infra.checker.Checker()

                rs = log_msgs(primary, txs)
                check_responses(rs, True, check, check_commit)
                network.wait_for_node_commit_sync()
                check_nodes_have_msgs(backups, txs)

            recovered_network = test(network, args)

            network.stop_all_nodes()
            network = recovered_network

            old_txs = Txs(args.msgs_per_recovery, recovery_idx, since_beginning=True)

            check_nodes_have_msgs(recovered_network.nodes, old_txs)
            LOG.success("Recovery complete on all nodes")
github Delgan / loguru / tests / test_contextualize.py View on Github external
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"
github Delgan / loguru / tests / test_standard_handler.py View on Github external
def test_stream_handler(capsys):
    logger.add(StreamHandler(sys.stderr), format="{level} {message}")
    logger.info("test")
    logger.remove()
    logger.warning("nope")

    out, err = capsys.readouterr()
    assert out == ""
    assert err == "INFO test\n"
github Delgan / loguru / tests / test_contextualize.py View on Github external
def test_nested_contextualize(writer):
    logger.add(writer, format="{message} {extra[foobar]}")

    with logger.contextualize(foobar="a"):
        with logger.contextualize(foobar="b"):
            logger.info("B")

        logger.info("A")

        with logger.contextualize(foobar="c"):
            logger.info("C")

    assert writer.read() == "B b\nA a\nC c\n"
github Delgan / loguru / tests / exceptions / source / backtrace / nested_decorator_catch_up.py View on Github external
import sys
from loguru import logger

logger.remove()
logger.add(sys.stderr, format="", colorize=False, backtrace=False, diagnose=False)
logger.add(sys.stderr, format="", colorize=False, backtrace=True, diagnose=False)


@logger.catch(ZeroDivisionError)
def foo():
    bar()


@logger.catch(NotImplementedError)
def bar():
    1 / 0


foo()