Log4MC
Log4MC 是用于数据包(datapack)的日志记录工具。
它旨在与其他编程语言的标准日志 API 保持一致。
它基于 MulverineX 的模块,但增加了实用功能并简化了语法。
⚠ 注意
不建议将 Log4MC 作为模块包含在你的数据包中,也不建议分发带有 Log4MC 的数据包,因为这不是为生产版本设计的,而仅适用于在创建你的数据包时使用。
此外,最终用户应有权选择是否使用以及使用何种日志记录器。事实上,即使你的代码中仍然保留着日志记录函数,你的数据包在没有 Log4MC 的情况下仍能正常工作。在没有日志记录器响应的情况下调用这些函数是预期行为。
入门指南
使用 Log4MC 进行日志记录非常简单。
在 tick 文件或 load 文件的开头,调用 #tick_logger 或 #load_logger 并指定一个命名空间。
然后你可以使用 6 个日志记录函数,分别对应六种严重性级别:#trace、#debug、#info、#warn、#error、#fatal。
将这些函数置于默认命名空间下的原因详见此处。
#load.mcfunction
function #load_logger {namespace:"mypack"}
function #trace {m:"this is a trace"}
function #debug {m:"this is a debug"}
function #info {m:"this is an info"}
function #warn {m:"this is a warn"}
function #error {m:"this is an error"}
function #fatal {m:"this is a fatal error"}
#tick.mcfunction
function #tick_logger {namespace:"mypack"}
function #trace {m:"tick"}
其背后的逻辑是:如果你在 tick 文件或 load 文件的开头注册了你的命名空间,就可以将日志链接到你的命名空间和记录时刻。
此外,严重性级别可用于过滤并仅显示相关的日志。在默认配置中,控制台的严重性级别为 INFO。
在这种情况下,使用前面的示例,控制台将显示以下内容:
... @@@ [L:279822][mypack/INFO]: this is an info
... @@@ [L:279822][mypack/WARN]: this is a warn
... @@@ [L:279822][mypack/ERROR]: this is an error
... @@@ [L:279822][mypack/FATAL]: this is a fatal error
但如果控制台的严重性级别设置为 TRACE,则会显示以下内容:
... @@@ [L:279822][mypack/TRACE]: this is a trace
... @@@ [L:279822][mypack/DEBUG]: this is a debug
... @@@ [L:279822][mypack/INFO]: this is an info
... @@@ [L:279822][mypack/WARN]: this is a warn
... @@@ [L:279822][mypack/ERROR]: this is an error
... @@@ [L:279822][mypack/FATAL]: this is a fatal error
... @@@ [T:279822][mypack/TRACE]: tick
... @@@ [T:279823][mypack/TRACE]: tick
... @@@ [T:279824][mypack/TRACE]: tick
... @@@ [T:279825][mypack/TRACE]: tick
... ...
@@@ 表示控制台显示的是一条来自数据包的日志消息。
第一个块对应于时间点:
- 首字母
L 或 T 指示消息是在加载时还是在 tick 时发送的
- 随后的数字是游戏时间(以 tick 为单位)
第二个块对应于消息来源:
mypack 是生成日志的数据包的命名空间
TRACE、DEBUG、INFO、WARN、ERROR、FATAL 是严重性级别
你可以按以下方式更改控制台的严重性级别:
scoreboard players operation console_level log4mc.main = #off log4mc.enum
scoreboard players operation console_level log4mc.main = #trace log4mc.enum
...
scoreboard players operation console_level log4mc.main = #fatal log4mc.enum
scoreboard players operation console_level log4mc.main = #all log4mc.enum
功能
Log4MC
Log4MC is logger for datapacks.
It is an attempt to match the standard logging APIs of other programming languages.
It is based on MulverineX's module, but adds practical features and a simplified syntax.
⚠ Caution
It is not recommended to include Log4MC as a module in your datapack, or to distribute your datapack with Log4MC, as this is not done for a production version, but only when creating your datapack.
Furthermore, the end-user should have the choice of using the logger he wants, and if he wants to use one. Indeed, your datapack will still work without Log4MC, even if the logging functions are still present in your code. Calling these functions without any logger responding is EXPECTED behavior.
Getting Started
Logging with Log4MC is pretty simple.
At the start of your tick or load file, call #tick_logger or #load_logger and specify a namespace.
You can then use the 6 logging functions, corresponding to the six severity levels : #trace, #debug, #info, #warn, #error, #fatal.
The choice of placing these functions under the default namespace is explained here.
#load.mcfunction
function #load_logger {namespace:"mypack"}
function #trace {m:"this is a trace"}
function #debug {m:"this is a debug"}
function #info {m:"this is an info"}
function #warn {m:"this is a warn"}
function #error {m:"this is an error"}
function #fatal {m:"this is a fatal error"}
#tick.mcfunction
function #tick_logger {namespace:"mypack"}
function #trace {m:"tick"}
The logic behind this is that if you register your namespace at the beginning of your tick file or your load file, it's possible to link your logs to your namespace and to the moment of logging.
In addition, the severity level can be used to filter and display only relevant logs. In the default configuration, the console severity level is INFO.
In this case, with the previous example, the console will display this :
... @@@ [L:279822][mypack/INFO]: this is an info
... @@@ [L:279822][mypack/WARN]: this is a warn
... @@@ [L:279822][mypack/ERROR]: this is an error
... @@@ [L:279822][mypack/FATAL]: this is a fatal error
But if the console's severity level was set to TRACE, this would be displayed :
... @@@ [L:279822][mypack/TRACE]: this is a trace
... @@@ [L:279822][mypack/DEBUG]: this is a debug
... @@@ [L:279822][mypack/INFO]: this is an info
... @@@ [L:279822][mypack/WARN]: this is a warn
... @@@ [L:279822][mypack/ERROR]: this is an error
... @@@ [L:279822][mypack/FATAL]: this is a fatal error
... @@@ [T:279822][mypack/TRACE]: tick
... @@@ [T:279823][mypack/TRACE]: tick
... @@@ [T:279824][mypack/TRACE]: tick
... @@@ [T:279825][mypack/TRACE]: tick
... ...
@@@ indicates that the message displayed in the console is a message from a datapack.
The first block corresponds to the moment :
- The first letter
L or T indicates whether the message was sent at load or at tick
- The number that follows is the gametime (in tick)
The second block corresponds to the source of the message :
mypack is the namespace of the datapack that produced the log
TRACE, DEBUG, INFO, WARN, ERROR, FATAL is the severity
You can change the console's severity level as follows :
scoreboard players operation console_level log4mc.main = #off log4mc.enum
scoreboard players operation console_level log4mc.main = #trace log4mc.enum
...
scoreboard players operation console_level log4mc.main = #fatal log4mc.enum
scoreboard players operation console_level log4mc.main = #all log4mc.enum
Features
- Severity logging
- Logs formatting
- Logs dumping
评论 0
登录后参与评分
还没有评论,来抢沙发吧