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_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"
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("")
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("")
def rotation_reopening(tmpdir, monkeypatch_date, delay):
monkeypatch_date(2018, 10, 27, 5, 0, 0, 0)
filepath = tmpdir.join("test.log")
i = logger.add(str(filepath), format="{message}", delay=delay, rotation="2 h")
logger.info("1")
monkeypatch_date(2018, 10, 27, 6, 30, 0, 0)
logger.info("2")
logger.remove(i)
i = logger.add(str(filepath), format="{message}", delay=delay, rotation="2 h")
logger.info("3")
assert len(tmpdir.listdir()) == 1
assert filepath.read() == "1\n2\n3\n"
monkeypatch_date(2018, 10, 27, 7, 30, 0, 0)
logger.info("4")
assert len(tmpdir.listdir()) == 2
assert filepath.read() == "4\n"
logger.remove(i)
monkeypatch_date(2018, 10, 27, 8, 30, 0, 0)
i = logger.add(str(filepath), format="{message}", delay=delay, rotation="2 h")
def test_compression(tmpdir):
i = logger.add(str(tmpdir.join("file.log")), compression="gz", delay=True)
logger.debug("a")
logger.remove(i)
assert len(tmpdir.listdir()) == 1
assert tmpdir.join("file.log.gz").check(exists=1)
def test_renaming_compression_dest_exists(monkeypatch, monkeypatch_date, tmpdir):
date = (2019, 1, 2, 3, 4, 5, 6)
timestamp = datetime.datetime(*date).timestamp()
monkeypatch_date(*date)
monkeypatch.setattr(loguru._file_sink, "get_ctime", lambda _: timestamp)
for i in range(4):
logger.add(str(tmpdir.join("rotate.log")), compression=".tar.gz", format="{message}")
logger.info(str(i))
logger.remove()
assert len(tmpdir.listdir()) == 4
assert tmpdir.join("rotate.log.tar.gz").check(exists=1)
assert tmpdir.join("rotate.2019-01-02_03-04-05_000006.log.tar.gz").check(exists=1)
assert tmpdir.join("rotate.2019-01-02_03-04-05_000006.2.log.tar.gz").check(exists=1)
assert tmpdir.join("rotate.2019-01-02_03-04-05_000006.3.log.tar.gz").check(exists=1)
def subworker_remove_inheritance():
logger.info("Child")
logger.remove()
logger.info("Nope")
def check_tb_sink(message):
exception = message.record["exception"]
if exception is None:
return
assert exception.traceback is not None
logger.add(check_tb_sink, enqueue=False, catch=False)
logger.add(writer, enqueue=True, catch=False, format="{message}")
logger.add(check_tb_sink, enqueue=False, catch=False)
try:
1 / 0
except ZeroDivisionError:
logger.exception("Error")
logger.remove()
lines = writer.read().strip().splitlines()
assert lines[0] == "Error"
assert lines[-1] == "ZeroDivisionError: division by zero"
import sys
from loguru import logger
logger.remove()
logger.add(sys.stderr, format="", colorize=True, backtrace=False, diagnose=True)
code = """
if True:
a = 5
print("foobar") #intentional faulty indentation here.
b = 7
"""
try:
exec(code)
except IndentationError:
logger.exception("")
def init_logger():
logger.remove() # Remove the pre-configured handler
logger.start(sys.stderr, format="{level} {time:MM-DD HH:mm:ss} {file}:{line} - {message}")