本文整理汇总了Python中loguru.logger.add方法的典型用法代码示例。如果您正苦于以下问题:Python logger.add方法的具体用法?Python logger.add怎么用?Python logger.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类loguru.logger
的用法示例。
在下文中一共展示了logger.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_logger
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def initialize_logger(name: str, log_level: str) -> None:
logger.remove()
if log_level == QUIET_LOG_LEVEL:
logger.disable(name)
return
if log_level == "DEBUG":
log_format = (
"<level>{level: <8}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)
else:
log_format = "<level>[{level}]</level> {message}"
logger.add(sys.stdout, colorize=True, format=log_format, level=log_level)
logger.enable(name)
ptr.set_logger(True)
sqlite.set_logger(True)
appconfigpy.set_logger(True)
示例2: setup_logger
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def setup_logger() -> None:
"""Set up stderr logging format.
The logging format and colors can be overridden by setting up the
environment variables such as ``LOGURU_FORMAT``.
See `Loguru documentation`_ for details.
.. _Loguru documentation: https://loguru.readthedocs.io/en/stable/api/logger.html#env
"""
logger.remove() # Remove the default setting
# Set up the preferred logging colors and format unless overridden by its environment variable
logger.level("INFO", color=environ.get("LOGURU_INFO_COLOR") or "<white>")
logger.level("DEBUG", color=environ.get("LOGURU_DEBUG_COLOR") or "<d><white>")
log_format = environ.get("LOGURU_FORMAT") or (
# "<green>{time:YYYY-MM-DD HH:mm:ss}</green> "
"<b><level>{level: <8}</level></b> "
"| <level>{message}</level>"
)
logger.add(sys.stderr, format=log_format)
# By default all the logging messages are disabled
logger.enable("charger")
示例3: set_log_level
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def set_log_level(log_level):
if log_level == "DEBUG":
log_format = (
"<level>{level: <8}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)
else:
log_format = "<level>[{level}]</level> {message}"
logger.remove()
logger.add(sys.stderr, colorize=True, format=log_format, level=log_level)
if log_level == "QUIET":
set_logger(is_enable=False)
else:
set_logger(is_enable=True)
示例4: main
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def main() -> None:
parser = get_argument_parser()
arguments = parser.parse_args()
logger.stop(0)
logger.add(sys.stderr, level=arguments.logging_level)
# noinspection PyBroadException
try:
cli_main(arguments)
except KeyboardInterrupt:
logger.info("Aborted")
except ConfigError as error:
logger.error("Invalid configuration: {}", error)
except MissingDependencyError as error:
logger.error("Missing dependency: {}", error)
except Exception:
logger.exception("Unexpected error occurred:")
示例5: test_safe_logging
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_safe_logging():
barrier = Barrier(2)
counter = itertools.count()
sink = NonSafeSink(1)
logger.add(sink, format="{message}", catch=False)
def threaded():
barrier.wait()
logger.info("___{}___", next(counter))
threads = [Thread(target=threaded) for _ in range(2)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
logger.remove()
assert sink.written in ("___0___\n___1___\n", "___1___\n___0___\n")
示例6: test_heavily_threaded_logging
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_heavily_threaded_logging(capsys):
logger.remove()
def function():
i = logger.add(NonSafeSink(0.1), format="{message}", catch=False)
logger.debug("AAA")
logger.info("BBB")
logger.success("CCC")
logger.remove(i)
threads = [Thread(target=function) for _ in range(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
logger.remove()
out, err = capsys.readouterr()
assert out == ""
assert err == ""
示例7: test_encoding_error
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_encoding_error(capsys):
def sink(m):
raise UnicodeEncodeError("utf8", "", 10, 11, "too bad")
logger.add(sink, catch=True)
logger.debug("test")
out, err = capsys.readouterr()
lines = err.strip().splitlines()
assert out == ""
assert lines[0] == "--- Logging error in Loguru Handler #0 ---"
assert lines[1].startswith("Record was: {")
assert lines[1].endswith("}")
assert lines[-2].startswith("UnicodeEncodeError:")
assert lines[-1] == "--- End of logging error ---"
示例8: test_broken_sink_caught_keep_working
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_broken_sink_caught_keep_working(enqueue):
output = ""
def half_broken_sink(m):
nonlocal output
if m.startswith("NOK"):
raise ValueError("Broken!")
else:
output += m
logger.add(half_broken_sink, format="{message}", enqueue=enqueue, catch=True)
logger.info("A")
logger.info("NOK")
logger.info("B")
time.sleep(0.1)
assert output == "A\nB\n"
示例9: test_custom_sink_invalid_complete
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_custom_sink_invalid_complete(capsys, complete):
class Sink:
def __init__(self):
self.complete = complete
def write(self, message):
print(message, end="")
async def worker():
logger.info("Test")
await logger.complete()
logger.add(Sink(), format="{message}")
asyncio.run(worker())
out, err = capsys.readouterr()
assert out == "Test\n"
assert err == ""
示例10: test_concurrent_execution
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_concurrent_execution(capsys):
async def task(i):
logger.debug("=> {}", i)
async def main():
tasks = [task(i) for i in range(10)]
await asyncio.gather(*tasks)
await logger.complete()
logger.add(async_writer, format="{message}")
asyncio.run(main())
out, err = capsys.readouterr()
assert err == ""
assert sorted(out.splitlines()) == sorted("=> %d" % i for i in range(10))
示例11: test_run_multiple_same_loop_set_global
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_run_multiple_same_loop_set_global(capsys):
async def worker(i):
logger.debug("Message {}", i)
await logger.complete()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.add(async_writer, format="{message}", loop=loop)
loop.run_until_complete(worker(1))
loop.run_until_complete(worker(2))
out, err = capsys.readouterr()
assert err == ""
assert out == "Message 1\nMessage 2\n"
示例12: test_complete_in_another_run
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_complete_in_another_run(capsys):
async def worker_1():
logger.debug("A")
async def worker_2():
logger.debug("B")
await logger.complete()
loop = asyncio.new_event_loop()
logger.add(async_writer, format="{message}", loop=loop)
loop.run_until_complete(worker_1())
loop.run_until_complete(worker_2())
out, err = capsys.readouterr()
assert out == "A\nB\n"
assert err == ""
示例13: test_complete_in_another_run_set_global
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_complete_in_another_run_set_global(capsys):
async def worker_1():
logger.debug("A")
async def worker_2():
logger.debug("B")
await logger.complete()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.add(async_writer, format="{message}", loop=loop)
loop.run_until_complete(worker_1())
loop.run_until_complete(worker_2())
out, err = capsys.readouterr()
assert out == "A\nB\n"
assert err == ""
示例14: test_complete_stream_noop
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_complete_stream_noop(capsys):
logger.add(sys.stderr, format="{message}", catch=False)
logger.info("A")
async def worker():
logger.info("B")
await logger.complete()
logger.info("C")
asyncio.run(worker())
logger.info("D")
out, err = capsys.readouterr()
assert out == ""
assert err == "A\nB\nC\nD\n"
示例15: test_complete_file_noop
# 需要导入模块: from loguru import logger [as 别名]
# 或者: from loguru.logger import add [as 别名]
def test_complete_file_noop(tmpdir):
filepath = tmpdir.join("test.log")
logger.add(str(filepath), format="{message}", catch=False)
logger.info("A")
async def worker():
logger.info("B")
await logger.complete()
logger.info("C")
asyncio.run(worker())
logger.info("D")
assert filepath.read() == "A\nB\nC\nD\n"