Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_contextualize(writer):
logger.add(writer, format="{message} {extra[foo]} {extra[baz]}")
with logger.contextualize(foo="bar", baz=123):
logger.info("Contextualized")
assert writer.read() == "Contextualized bar 123\n"
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")
import sys
from loguru import logger
logger.remove()
logger.add(sys.stderr, format="", colorize=False, backtrace=True, diagnose=False)
@logger.catch
def a(n):
if n:
a(n - 1)
n / 0
def b(n):
with logger.catch():
if n:
b(n - 1)
n / 0
def test_format(message, format, expected, writer):
logger.add(writer, format=format)
logger.debug(message)
assert writer.read() == expected
import sys
from loguru import logger
logger.remove()
logger.add(sys.stderr, format="", colorize=False, backtrace=True, diagnose=False)
def a(x):
@logger.catch
def nested(i):
1 / i
nested(x)
a(0)
def b(x):
def nested(i):
1 / i
def test_not_picklable_sinks_inheritance(capsys, tmpdir):
filepath = tmpdir.join("test.log")
stream = sys.stderr
output = []
logger.add(str(filepath), format="{message}", enqueue=True, catch=False)
logger.add(stream, format="{message}", enqueue=True, catch=False)
logger.add(lambda m: output.append(m), format="{message}", enqueue=True, catch=False)
process = multiprocessing.Process(target=subworker_inheritance)
process.start()
process.join()
assert process.exitcode == 0
logger.info("Main")
logger.remove()
out, err = capsys.readouterr()
assert filepath.read() == "Child\nMain\n"
assert out == ""
def test_exception_not_raising(writer, exception):
logger.add(writer)
@logger.catch(exception)
def a():
1 / 0
a()
assert writer.read().endswith("ZeroDivisionError: division by zero\n")
def test_handler_removed():
i = logger.add(sys.__stdout__)
logger.add(sys.__stderr__)
logger.remove(i)
assert repr(logger) == ")]>"
save_tests (bool): save loaded/parsed tests to JSON file.
log_level (str): logging level.
log_file (str): log file path.
"""
self.exception_stage = "initialize HttpRunner()"
kwargs = {
"failfast": failfast,
"resultclass": report.HtmlTestResult
}
logger.remove()
log_level = log_level.upper()
logger.add(sys.stdout, level=log_level)
if log_file:
logger.add(log_file, level=log_level)
self.unittest_runner = unittest.TextTestRunner(**kwargs)
self.test_loader = unittest.TestLoader()
self.save_tests = save_tests
self._summary = None
self.test_path = None
def init_logger(args):
logger.remove()
# Forward logging to standard output
if args.verbose:
logger.add(sys.__stdout__, level="DEBUG")
else:
logger.add(sys.__stdout__, level="INFO")
stream = StreamToLogger()
info_logger_id = logger.add(constants.INFO_LOGGING_FILE_NAME, level="INFO")
info_debug_id = logger.add(constants.DEBUG_LOGGING_FILE_NAME, level="DEBUG")
return stream, info_logger_id, info_debug_id