📄 日志(Log)#
#bs.log:help
记录不同级别的日志消息并显示在聊天栏中。
启用命令方块
This module uses a command block to get the system time. For the time to be displayed as intended, be sure that both the command_blocks_work and send_command_feedback game rules are set to true.
🔧 函数#
你可以在下方找到此模块中的所有可用函数。
记录消息#
- #bs.log:error
记录错误消息。有关其工作原理的更多信息,请参见使用方法部分。默认情况下,它显示为:[ERROR] <标签> > <消息>,将鼠标悬停在日志左侧部分可显示时间戳和日志函数路径。
- 输入:
函数宏:
参数
message: 记录的日志消息。必须是有效的 SNBT 文本组件。
namespace: 函数的命名空间。
path: 日志来源路径(当前 Minecraft 函数)。
tag: 函数标识标签。显示在日志消息中,并作为分组管理粒度的依据。
示例:记录来自 bs.foo:bar 函数的错误消息:
function #bs.log:error { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"Error"' }
- #bs.log:warn
记录警告消息。有关其工作原理的更多信息,请参见使用方法部分。默认情况下,它显示为:[WARN] <标签> > <消息>,将鼠标悬停在日志左侧部分可显示时间戳和日志函数路径。
- 输入:
函数宏:
参数
message: 记录的日志消息。必须是有效的 SNBT 文本组件。
namespace: 函数的命名空间。
path: 日志来源路径(当前 Minecraft 函数)。
tag: 函数标识标签。显示在日志消息中,并作为分组管理粒度的依据。
示例:记录来自 bs.foo:bar 函数的警告消息:
function #bs.log:warn { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"Warning"' }
- #bs.log:info
记录信息消息。有关其工作原理的更多信息,请参见使用方法部分。默认情况下,它显示为:[INFO] <标签> > <消息>,将鼠标悬停在日志左侧部分可显示时间戳和日志函数路径。
- 输入:
函数宏:
参数
message: 记录的日志消息。必须是有效的 SNBT 文本组件。
namespace: 函数的命名空间。
path: 日志来源路径(当前 Minecraft 函数)。
tag: 函数标识标签。显示在日志消息中,并作为分组管理粒度的依据。
示例:记录来自 bs.foo:bar 函数的信息消息:
function #bs.log:info { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"Info"' }
- #bs.log:debug
记录调试消息。有关其工作原理的更多信息,请参见使用方法部分。默认情况下,它显示为:[DEBUG] <标签> > <消息>,将鼠标悬停在日志左侧部分可显示时间戳和日志函数路径。
- 输入:
函数宏:
参数
message: 记录的日志消息。必须是有效的 SNBT 文本组件。
namespace: 函数的命名空间。
path: 日志来源路径(当前 Minecraft 函数)。
tag: 函数标识标签。显示在日志消息中,并作为分组管理粒度的依据。
示例:记录来自 bs.foo:bar 函数的调试消息:
function #bs.log:debug { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"Debug"' }
制作人员:theogiraudet
日志历史#
- #bs.log:history {with:{}}
检索日志历史。显示所有之前记录的消息。
- 输入:
函数宏:
参数
with: 可选过滤条件。
severity: 过滤日志的严重级别(
error、warn、info或debug)。namespace: 过滤日志的命名空间。
tag: 过滤日志的标签。
示例:查看特定命名空间、标签和严重程度的日志历史:
# Once
function #bs.log:history {with: {severity: "warn", namespace: "bs.foo", tag: "bar"}}
- #bs.log:clear_history {with:{}}
清除日志历史。
- 输入:
函数宏:
参数
with: 可选过滤条件。
severity: 过滤日志的严重级别(
error、warn、info或debug)。namespace: 过滤日志的命名空间。
tag: 过滤日志的标签。
示例:清除特定命名空间、标签和严重程度的日志历史:
# Once
function #bs.log:clear_history {with: {severity: "warn", namespace: "bs.foo", tag: "bar"}}
制作人员:Aksiome
🎓 如何使用?#
不同类型的日志有不同的日志级别:
调试:详细的调试信息。
信息:兴趣/重要事件。
警告:不属于错误的异常情况。
错误:应该监控和修复的运行时错误。
管理日志粒度#
大量日志可能会迅速淹没聊天栏。为防止这种情况,Bookshelf 的日志模块可以根据三个参数配置显示特定日志:日志级别、标签和命名空间。
用户需要给自己添加遵循严格语法的标签:<命名空间>.log.<标签>.<级别>,其中级别可以是 debug、info、warn 或 error。
_ 符号作为通配符,记录所有标签或所有级别:
bs.foo.log._.<级别>:显示bs.foo的所有日志,无论标签是什么。bs.foo.log.<标签>._:显示bs.foo的所有日志,无论级别是什么。bs.foo.log._._:显示bs.foo的所有日志。_.log._._:显示所有日志。
备注
每个级别允许显示后续级别。例如,如果用户给自己添加了以 warn 结尾的标签,他们就可以看到警告和错误级别的日志。默认情况下,不向任何用户显示日志。
定义消息#
日志函数接受四个变量作为输入。当前函数的 path 告知用户日志的来源,tag、namespace 和 message。
警告
message 字符串必须是有效的 SNBT 文本组件。因此,要将纯文本字符串指定为消息,需要转义消息("\"message\"" 或 '"message"')。
示例:记录简单的警告消息。我们假设日志来自 bs.foo:bar 函数:
function #bs.log:warn { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"A warning message"' }
如果用户具有以下标签之一,将显示以下消息:bs.foo.log.bar.warn、bs.foo.log.bar.info、bs.foo.log.bar.debug、bs.foo.log._.warn、bs.foo.log._.info、bs.foo.log._.debug、bs.foo.log._._、bs.foo.log.bar._、_.log.bar.warn、_.log.bar.info、_.log.bar.debug、_.log._.warn、_.log._.info、_.log._.debug、_.log._._、_.log.bar._。

示例:记录复杂的信息消息。我们假设日志来自 bs.foo:baz 函数:
function #bs.log:info { namespace: "bs.foo", path: "bs.foo:baz", tag: "baz", message: [{text:"Score: ",color:"light_purple"},{score:{name:"-1",objective:"bs.const"}},{text:", "},{text:"@p: ",color:"light_purple"},{selector:"@p"},{text:", "},{text:"[hover me]",color:"light_purple",hover_event:{action:"show_text",value:"That tickles!"}}] }
如果用户具有以下标签之一,将显示以下消息:bs.foo.log.baz.info、bs.foo.log.baz.debug、bs.foo.log._.info、bs.foo.log._.debug、bs.foo.log.baz._、_.log.baz.info、_.log.baz.debug、_.log._.info、_.log._.debug、_.log.baz._。

自定义格式#
Bookshelf 建议根据命名空间定义不同的日志消息格式。要添加新的日志消息格式,你必须直接写入命令存储数组 bs:const log.messages:
{
namespaces: ["<namespace>"],
format: {
debug: "<SNBT compound>",
info: "<SNBT compound>",
warn: "<SNBT compound>",
error: "<SNBT compound>",
}
}
namespaces 数组存储所有共享相同日志消息格式的命名空间。四种格式(error、warn、info 和 debug)描述了相应严重程度级别的日志格式。每个值必须是完整的 SNBT 文本组件。
Bookshelf 提供了几个可以直接在日志消息格式中使用的值:
命令存储 |
路径 |
描述 |
|
|---|---|---|---|
bs:in |
log.message |
日志的消息(必须是有效的 SNBT 文本组件) |
|
bs:in |
log.namespace |
当前日志消息的命名空间 |
|
bs:in |
log.path |
记录当前消息的函数路径 |
|
bs:in |
log.tag |
日志消息的标签 |
|
bs:in |
log.hours |
日志消息时间戳的小时 |
|
bs:in |
log.minutes |
日志消息时间戳的分钟 |
|
bs:in |
log.seconds |
日志消息时间戳的秒 |
|
bs:in |
log.ticks |
日志消息时间戳的刻 |
|
bs:in |
log.gametime |
记录消息时的游戏时间 |
|
bs:const |
log.time_hms |
以 hh:mm:ss 格式显示时间的 SNBT 文本组件 |
|
bs:const |
log.time_hmst |
以 hh:mm:ss:tt 格式显示时间的 SNBT 文本组件 |
示例:为命名空间 bs.foo 定义自定义日志消息格式:
data modify storage bs:const log.messages append value { \
namespaces: ["bs.foo"], \
format: { \
debug: ["", {nbt: "log.full_time", storage: "bs:const", interpret: true, color: "red"}, " [DEBUG] - ", {nbt: "log.message", storage: "bs:in", interpret: true}], \
info: ["", {nbt: "log.full_time", storage: "bs:const", interpret: true, color: "red"}, " [INFO] - ", {nbt: "log.message", storage: "bs:in", interpret: true}], \
warn: ["", {nbt: "log.full_time", storage: "bs:const", interpret: true, color: "red"}, " [WARN] - ", {nbt: "log.message", storage: "bs:in", interpret: true}], \
error: ["", {nbt: "log.full_time", storage: "bs:const", interpret: true, color: "red"}, " [ERROR] - ", {nbt: "log.message", storage: "bs:in", interpret: true}] \
} \
}
试试新格式:
tag @s add bs.foo.log.bar.warn
function #bs.log:warn { namespace: "bs.foo", path: "bs.foo:bar", tag: "bar", message: '"A warning message"' }
💬 这对你有帮助吗?
欢迎在下方留下你的问题和反馈!