BotConsole
控制台日志输出工具,包根直接导出,可在插件、入口文件或其他模块中使用。也可通过 hotcat-bot-qq/botConsole 子路径导入。
ts
import { BotConsole } from 'hotcat-bot-qq'
new BotConsole('system', '启动完成').log()
new BotConsole('error', '连接失败').log()构造函数
ts
new BotConsole(type: BotConsoleType, content: ...)| 类型 | content | 输出示例 |
|---|---|---|
system | string | [hotCatBotSystem]: 启动完成 |
error | string | [error] 连接失败 |
plugin | { name: string; msg: string } | [plugin:myPlugin]: 已加载 |
groupMessage、privateMessage、notice 等类型供框架内部格式化事件使用,需要传入对应事件对象,外部一般使用 system / error / plugin。
完整 BotConsoleType:system、groupMessage、privateMessage、error、sendGroupMessage、sendPrivateMessage、groupMessageSelf、privateMessageSelf、requestFriend、requestGroupAdd、requestGroupInvite、notice、plugin、debug、info、warn。
全局配置
ts
BotConsole.configure({
level: 'info',
timestamps: true,
file: './logs/bot.log',
maxSize: 5 * 1024 * 1024,
maxFiles: 5,
json: false,
onLog: (entry) => {
// 接入 Sentry 等
},
})| 选项 | 说明 |
|---|---|
level | 最低级别:debug / info / warn / error / silent |
timestamps | 是否输出 ISO 时间戳 |
file | 日志文件路径,自动创建目录 |
maxSize | 单文件最大字节数,超过后轮转 |
maxFiles | 保留的历史文件数量 |
json | 使用 JSON Lines 输出 |
onLog | 每条日志回调,可用于错误上报 |
BotConsole.reset() 恢复默认配置,BotConsole.getOptions() 读取当前配置。
log()
ts
new BotConsole('plugin', { name: 'myPlugin', msg: '已加载' }).log()输出到 stdout,返回 void。日志会清理控制字符,JSON、图片、文件等内容不做长度截断。
辅助导出
ts
import { BotConsole, Message, colors, parseCQ, formatMessage, formatError } from 'hotcat-bot-qq'
colors.green // ANSI 颜色码
parseCQ('[CQ:at,qq=123]') // '[@123]'
formatMessage(Message.text('hi')) // 带颜色的消息段字符串
formatError(new Error('boom')) // 'boom',循环引用对象也不会抛错formatMessage 接受 Message 实例;formatError 可安全格式化未知异常。类型 BotConsoleType、PrivateMessage、NoticeEvent 等也可从包根导入。