File tree Expand file tree Collapse file tree
saltify-docs/content/guide Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # 日志配置
2+
3+ Saltify 使用 KtorSimpleLogger 输出运行时日志。
4+
5+ ## JVM 平台
6+
7+ 在 JVM 平台上,使用 SLF4J 作为日志框架,这里以 Logback 为例:
8+
9+ ``` kotlin
10+ dependencies {
11+ implementation(" ch.qos.logback:logback-classic:$logbackVersion " )
12+ }
13+ ```
14+
15+ ### 配置日志
16+
17+ 在 ` src/main/resources ` 目录下创建 ` logback.xml ` 配置文件,以下是示例配置:
18+
19+ ``` xml
20+ <configuration >
21+ <appender name =" STDOUT" class =" ch.qos.logback.core.ConsoleAppender" >
22+ <encoder >
23+ <pattern >%d{HH:mm:ss} %highlight([%-5level]) %cyan(%logger{36}) - %msg%n</pattern >
24+ </encoder >
25+ </appender >
26+ <root level =" INFO" >
27+ <appender-ref ref =" STDOUT" />
28+ </root >
29+ </configuration >
30+ ```
31+
32+ 关于其他自定义配置,可以参照相关日志框架的文档。
33+
34+ ## 其他平台
35+
36+ 对于其他非 JVM 平台,Saltify 会调用 println 等相关基础 API 直接输出到控制台。
37+
38+ ## 不使用日志
39+
40+ 如果不需要日志功能,可以选择不添加任何日志实现依赖。在这种情况下:
41+
42+ - ** JVM 平台** :日志调用会被忽略,不会有任何输出
43+ - ** 其他平台** :仍会输出到控制台
44+
45+ ## 自行调用 Logger
46+
47+ 在插件上下文、指令上下文中可以直接使用 ` logger ` :
48+
49+ ``` kotlin
50+ val myPlugin = SaltifyPlugin (" my-plugin" , ::Config ) { config ->
51+ onStart {
52+ logger.info(" 插件已启动" )
53+ }
54+
55+ on<Event .MessageReceive > {
56+ logger.debug(" 收到消息: ${it.data.segments.plainText} " )
57+ }
58+
59+ command(" test" ) {
60+ onExecute {
61+ logger.info(" 123" )
62+ // ...
63+ }
64+ }
65+ }
66+ ```
67+
68+ 此时 logger 的 name 分别为: ` Saltify/plugin:$pluginName ` 与 ` Saltify/cmd:$commandName ` 。
69+
70+ > [ !tip]
71+ > 对于匿名插件,Saltify 会生成 ` Saltify/plugin:anonymous-XXXX ` 作为 logger name。
You can’t perform that action at this time.
0 commit comments