WIP 记录器!受到 charmbracelet 的 log 包和 Python structlog 包的启发。
开源地址 : https://github.com/thatstoasty/stump/
我正在解决编译时终端颜色配置文件查询的一些问题。目前,默认样式采用TRUE_COLOR
启用的颜色配置文件。因此,如果您的终端仅支持ANSI
或ANSI256
,请尝试像示例中那样设置自定义样式,或者将fromcustom.mojo
中的默认配置文件更新为或。stump/style.mojo``TRUE_COLOR``ANSI``ANSI256
请参阅示例目录,了解有关设置自定义处理器、样式、仅消息/json/logfmt 日志记录以及关闭样式的日志记录的示例!

最小默认记录器示例:
from stump import get_logger
alias logger = get_logger()
fn main():
logger.info("Information is good.")
logger.warn("Warnings can be good too.")
logger.error("An error!")
logger.debug("Debugging...")
logger.fatal("uh oh...")
支持将任意参数对和 kwargs 合并到日志语句中!
from stump import get_logger
alias logger = get_logger()
fn main():
logger.info("Information is good.", "key", "value")
logger.warn("Warnings can be good too.", "no_value")
logger.error("An error!", erroring=True)
logger.fatal("uh oh...", "number", 4, "mojo", "🔥")
logger.debug("Debugging...")
输出(不包括颜色)
2024-04-03 14:53:56 INFO Information is good. key=value
2024-04-03 14:53:56 WARN Warnings can be good too. no_value=
2024-04-03 14:53:56 ERROR An error! erroring=True
2024-04-03 14:53:56 FATAL uh oh... number=4 mojo=🔥
最小 JSON 记录器示例:
from stump import (
DEBUG,
JSON_FORMAT,
BoundLogger,
PrintLogger
)
# The loggers are compiled at build time, so we can reuse it.
alias LOG_LEVEL = DEBUG
alias logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=JSON_FORMAT)
fn main():
logger.info("Information is good.")
logger.warn("Warnings can be good too.")
logger.error("An error!")
logger.debug("Debugging...")
logger.fatal("uh oh...")
定制样式和处理器记录器示例:
from stump import (
DEBUG,
DEFAULT_FORMAT,
Processor,
Context,
Styles,
Sections,
BoundLogger,
PrintLogger,
add_log_level,
add_timestamp,
add_timestamp_with_format,
)
from external.mist import TerminalStyle, Profile, TRUE_COLOR
# Define a custom processor to add a name to the log output.
fn add_my_name(context: Context) -> Context:
var new_context = Context(context)
new_context["name"] = "Mikhail"
return new_context
# Define custom processors to add extra information to the log output.
fn my_processors() -> List[Processor]:
return List[Processor](
add_log_level, add_timestamp_with_format["YYYY"](), add_my_name
)
# Define custom styles to format and colorize the log output.
fn my_styles() -> Styles:
# Log level styles, by default just set colors
var levels = Sections()
levels["FATAL"] = TerminalStyle.new().background("#d4317d")
levels["ERROR"] = TerminalStyle.new().background("#d48244")
levels["INFO"] = TerminalStyle.new().background("#13ed84")
levels["WARN"] = TerminalStyle.new().background("#decf2f")
levels["DEBUG"] = TerminalStyle.new().background("#bd37db")
var keys = Sections()
keys["name"] = (
TerminalStyle.new().foreground("#c9a0dc").underline()
)
var values = Sections()
values["name"] = TerminalStyle.new().foreground("#d48244").bold()
return Styles(
levels=levels,
key=TerminalStyle.new().faint(),
separator=TerminalStyle.new().faint(),
keys=keys,
values=values,
)
# The loggers are compiled at build time, so we can reuse it.
alias LOG_LEVEL = DEBUG
# Build a bound logger with custom processors and styling
alias logger = BoundLogger(
PrintLogger(LOG_LEVEL), formatter=DEFAULT_FORMAT, processors=my_processors, styles=my_styles
)
fn main():
logger.info("Information is good.")
logger.warn("Warnings can be good too.")
logger.error("An error!")
logger.debug("Debugging...")
logger.fatal("uh oh...")
将记录器导入其他文件即可!
from examples.default import logger
fn main():
logger.info("Hello!")
去做
特征
- 添加更多处理器功能。
- 添加对通过
Logger
使用实现io.Writer
.
- 一旦我们有了文件范围支持,就添加全局记录器支持。
- 使格式化程序灵活且可组合。目前只有几种预定义的格式。
- 因致命日志调用而退出。
- logf 函数指定该日志消息的特定格式。