def log(sink, rep=1): logger.debug("This shouldn't be printed.") i = logger.start(sink, format='{message}') for _ in range(rep): logger.debug(message) logger.stop(i) logger.debug("This shouldn't be printed neither.")
def test_size_rotation(monkeypatch_date, tmpdir, size): monkeypatch_date(2018, 1, 1, 0, 0, 0, 0) file = tmpdir.join("test_{time}.log") i = logger.start(file.realpath(), format='{message}', rotation=size, mode='w') monkeypatch_date(2018, 1, 1, 0, 0, 1, 0) logger.debug("abcde") monkeypatch_date(2018, 1, 1, 0, 0, 2, 0) logger.debug("fghij") monkeypatch_date(2018, 1, 1, 0, 0, 3, 0) logger.debug("klmno") monkeypatch_date(2018, 1, 1, 0, 0, 4, 0) logger.stop(i) assert len(tmpdir.listdir()) == 3 assert tmpdir.join( "test_2018-01-01_00-00-00_000000.log").read() == 'abcde\n' assert tmpdir.join( "test_2018-01-01_00-00-02_000000.log").read() == 'fghij\n' assert tmpdir.join( "test_2018-01-01_00-00-03_000000.log").read() == 'klmno\n'
def test_safe_writing_after_removing(capsys): barrier = Barrier(2) i = logger.add(NonSafeSink(1), format="{message}", catch=False) j = logger.add(NonSafeSink(1), format="{message}", catch=False) def write(): barrier.wait() logger.info("Writing") def remove(): barrier.wait() time.sleep(0.5) logger.remove(j) threads = [Thread(target=write), Thread(target=remove)] for thread in threads: thread.start() for thread in threads: thread.join() logger.stop() out, err = capsys.readouterr() assert out == "" assert err == ""
def test_compression_at_stop_without_rotation(tmpdir, mode): i = logger.start(tmpdir.join('file.log'), compression="gz", mode=mode) logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join('file.log.gz').check(exists=1)
def test_no_renaming(tmpdir): i = logger.start(tmpdir.join('test.log'), format="{message}", retention=10) logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join('test.log').read() == 'test\n'
def test_no_compression_at_stop_with_rotation(tmpdir, mode): i = logger.start(tmpdir.join('test.log'), compression="gz", rotation="100 MB", mode=mode) logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join("test.log").check(exists=1)
def test_lazy(writer): counter = 0 def laziness(): nonlocal counter counter += 1 return counter logger.start(writer, level=10, format="{level.no} => {message}") logger.opt(lazy=True).log(10, "1: {lazy}", lazy=laziness) logger.opt(lazy=True).log(5, "2: {0}", laziness) logger.stop() logger.opt(lazy=True).log(20, "3: {}", laziness) a = logger.start(writer, level=15, format="{level.no} => {message}") b = logger.start(writer, level=20, format="{level.no} => {message}") logger.log(17, "4: {}", counter) logger.opt(lazy=True).log(14, "5: {lazy}", lazy=lambda: counter) logger.stop(a) logger.opt(lazy=True).log(16, "6: {0}", lambda: counter) logger.opt(lazy=True).info("7: {}", laziness) logger.debug("7: {}", counter) assert writer.read() == "10 => 1: 1\n17 => 4: 1\n20 => 7: 2\n"
def test_stop_enqueue(writer): i = logger.start(writer, format="{message}", enqueue=True) logger.debug("1") time.sleep(0.1) logger.stop(i) logger.debug("2") assert writer.read() == "1\n"
def test_stop_all(tmpdir, writer, capsys): file = tmpdir.join("test.log") logger.debug("This shouldn't be printed.") logger.start(file.realpath(), format='{message}') logger.start(sys.stdout, format='{message}') logger.start(sys.stderr, format='{message}') logger.start(writer, format='{message}') message = 'some message' expected = message + '\n' logger.debug(message) logger.stop() logger.debug("This shouldn't be printed neither.") out, err = capsys.readouterr() assert file.read() == expected assert out == expected assert err == expected assert writer.read() == expected
def test_with_backtrace(writer): logger.start(writer, backtrace=True) def c(): a = 2 b = 0 a / b decorated = logger.catch()(c) decorated() result_with = writer.read() logger.stop() writer.clear() logger.start(writer, backtrace=False) decorated = logger.catch()(c) decorated() result_without = writer.read() assert len(result_with) > len(result_without) assert result_with.endswith(zero_division_error) assert result_without.endswith(zero_division_error)
def test_safe_removing_while_logging(): barrier = Barrier(2) counter = itertools.count() sink_1 = NonSafeSink(1) sink_2 = NonSafeSink(1) a = logger.add(sink_1, format="{message}", catch=False) b = logger.add(sink_2, format="{message}", catch=False) def thread_1(): barrier.wait() logger.info("aaa{}bbb", next(counter)) def thread_2(): barrier.wait() time.sleep(0.5) logger.remove(a) logger.info("ccc{}ddd", next(counter)) threads = [Thread(target=thread_1), Thread(target=thread_2)] for thread in threads: thread.start() for thread in threads: thread.join() logger.stop() assert sink_1.written == "aaa0bbb\n" assert sink_2.written == "aaa0bbb\nccc1ddd\n"
def test_delayed(tmpdir): i = logger.start(tmpdir.join('file.log'), compression='gz', delay=True) logger.debug('a') logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join("file.log.gz").check(exists=1)
def test_delayed_early_stop(tmpdir): for i in range(5): tmpdir.join("test.%d.log" % i).write("test") i = logger.start(tmpdir.join('test.log'), retention=0, delay=True) logger.stop(i) assert len(tmpdir.listdir()) == 0
def test_deprecated_start_and_stop(writer): i = logger.start(writer, format="{message}") logger.debug("Test") assert writer.read() == "Test\n" writer.clear() logger.stop(i) logger.debug("Test") assert writer.read() == ""
def test_file_handler(tmpdir): file = tmpdir.join('test.log') logger.start(FileHandler(file), format="{message}") logger.info("test") logger.stop() logger.warning("nope") assert file.read() == "test\n"
def test_null_handler(capsys): logger.start(NullHandler()) logger.error("nope") logger.stop() out, err = capsys.readouterr() assert out == "" assert err == ""
def install_logging(verbosity: int): logger.stop() logger.add( sys.stderr, format=LOGURU_FORMAT, level=VERBOSITY_LEVELS.get(verbosity, 'TRACE'), backtrace=False, )
def init_logging(verbosity: int): logger.stop() logger.add(sys.stderr, format=LOGURU_FORMAT, level=VERBOSITY_LEVELS.get(verbosity, 'TRACE'), backtrace=False) logging.basicConfig(level=logging.NOTSET, handlers=[InterceptHandler()]) logger.disable('aiosqlite.core')
def test_stream_handler(capsys): logger.start(StreamHandler(sys.stderr), format="{message}") logger.info("test") logger.stop() logger.warning("nope") out, err = capsys.readouterr() assert out == "" assert err == "test\n"
def test_no_retention_at_stop_with_rotation(tmpdir, mode): i = logger.start(tmpdir.join('file.log'), retention=0, rotation="100 MB", mode=mode) logger.debug("1") assert len(tmpdir.listdir()) == 1 logger.stop(i) assert len(tmpdir.listdir()) == 1
def test_compression_function(tmpdir): def compress(file): os.replace(file, file + '.rar') i = logger.start(tmpdir.join('file.log'), compression=compress) logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join('file.log.rar').check(exists=1)
def test_delayed_early_stop(monkeypatch_date, tmpdir): monkeypatch_date(2018, 1, 1, 0, 0, 0, 0) i = logger.start(tmpdir.join('file.log'), rotation=0, delay=True, format="{message}") logger.stop(i) assert len(tmpdir.listdir()) == 0
def test_manage_formatted_files_without_extension(tmpdir): tmpdir.join('file_8').write("") tmpdir.join('file_7').write("") tmpdir.join('file_6').write("") i = logger.start(tmpdir.join('file_{time}'), retention=0) logger.debug("1") logger.stop(i) assert len(tmpdir.listdir()) == 0
def test_deprecated_start_and_stop(writer): with pytest.warns(DeprecationWarning): i = logger.start(writer, format="{message}") logger.debug("Test") assert writer.read() == "Test\n" writer.clear() with pytest.warns(DeprecationWarning): logger.stop(i) logger.debug("Test") assert writer.read() == ""
def test_no_rotation_at_stop(tmpdir, mode): i = logger.start(tmpdir.join("test.log"), rotation="10 MB", mode=mode, format="{message}") logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join("test.log").read() == "test\n"
def test_manage_file_without_extension(tmpdir): file = tmpdir.join('file') i = logger.start(file, retention=0) logger.debug("?") assert len(tmpdir.listdir()) == 1 assert file.check(exists=1) logger.stop(i) assert len(tmpdir.listdir()) == 0 assert file.check(exists=0)
def test_retention_count(tmpdir, retention): file = tmpdir.join('test.log') for i in range(retention): tmpdir.join('test.%d.log' % i).write('test') i = logger.start(file.realpath(), retention=retention) logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == retention
def test_rename_existing_before_compression(monkeypatch_date, tmpdir): monkeypatch_date(2018, 1, 1, 0, 0, 0, 0) i = logger.start(tmpdir.join('test.log'), compression="tar.gz") logger.debug("test") logger.stop(i) j = logger.start(tmpdir.join('test.log'), compression="tar.gz") logger.debug("test") logger.stop(j) assert len(tmpdir.listdir()) == 2 assert tmpdir.join("test.log.tar.gz").check(exists=1) assert tmpdir.join("test.2018-01-01_00-00-00_000000.log.tar.gz").check(exists=1)
def test_rotation_at_stop(monkeypatch_date, tmpdir, mode): monkeypatch_date(2018, 1, 1, 0, 0, 0, 0) i = logger.start(tmpdir.join("test_{time:YYYY}.log"), rotation="10 MB", mode=mode, format="{message}") logger.debug("test") logger.stop(i) assert len(tmpdir.listdir()) == 1 assert tmpdir.join("test_2018.log").read() == "test\n"
def test_delayed(monkeypatch_date, tmpdir): monkeypatch_date(2018, 1, 1, 0, 0, 0, 0) i = logger.start(tmpdir.join('file.log'), rotation=0, delay=True, format="{message}") logger.debug('a') logger.stop(i) assert len(tmpdir.listdir()) == 2 assert tmpdir.join("file.log").read() == "a\n" assert tmpdir.join("file.2018-01-01_00-00-00_000000.log").read() == ""