简体   繁体   English

如何使用 Loguru 默认值 + 和额外信息?

[英]How to use Loguru defaults + and extra information?

I'm still reaseaching about Loguru, but I can't find an easy way to do this.我仍在研究 Loguru,但我找不到一个简单的方法来做到这一点。 I want to use the default options from Loguru, I believe they are great, but I want to add information to it, I want to add the IP of a request that will be logged.我想使用 Loguru 的默认选项,我相信它们很棒,但我想向它添加信息,我想添加将被记录的请求的 IP。

If I try this:如果我试试这个:

import sys
from loguru import logger
logger.info("This is log info!")
# This is directle from Loguru page
logger.add(sys.stderr, format="{extra[ip]} {extra[user]} {message}")
context_logger = logger.bind(ip="192.168.0.1", user="someone")
context_logger.info("Contextualize your logger easily")
context_logger.bind(user="someone_else").info("Inline binding of extra attribute")
context_logger.info("Use kwargs to add context during formatting: {user}", user="anybody")

That logs this:这记录了这个: 在此处输入图像描述

I know that with logger.remove(0) I will remove the default logs, but I want to use it to obtain something like this: 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody我知道使用logger.remove(0)我将删除默认日志,但我想用它来获得类似这样的东西: 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody , with XXX.XXX.XX.X being the IP. 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody ,其中 XXX.XXX.XX.X 是 IP。 Using the default config (for color and the rest of thing) and adding a little thing to the format.使用默认配置(用于颜色和事物的 rest)并在格式中添加一些东西。

I'm trying to access the default configs, but I haven't been able to import them and use them with logger.add .我正在尝试访问默认配置,但我无法导入它们并将它们与logger.add一起使用。 I think I will have to configure everything from scratch.我想我将不得不从头开始配置所有内容。

Hope someone can help me, thanks.希望有人可以帮助我,谢谢。

I make the same question in the Github Repository and this was the answered by Delgan (Loguru maintainer):我在Github 存储库中提出了同样的问题,这是 Delgan(Loguru 维护者)的回答:

I think you simply need to add() your handler using a custom format containing the extra information.我认为您只需要使用包含额外信息的自定义格式add()您的处理程序。 Here is an example:这是一个例子:

logger_format = (
    "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
    "<level>{level: <8}</level> | "
    "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
    "{extra[ip]} {extra[user]} - <level>{message}</level>"
)
logger.configure(extra={"ip": "", "user": ""})  # Default values
logger.remove()
logger.add(sys.stderr, format=logger_format)

Extra: if you want to use TRACE level use this when adding the configurations:额外:如果您想使用 TRACE 级别,请在添加配置时使用:

logger.add(sys.stderr, format=logger_format, level="TRACE")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM