跳转到内容

错误日志

服务器上出现失败时,OpenBot 会写一行级别为 Error 的日志。 OpenBot 不会把这些日志发送到其他服务。你可以用 Effect 日志器自己添加这个功能。


OpenBot 记录什么

来源 何时
http 请求以 500 或更高的状态结束时。
turn Turn 以 Failed 状态结束时。
startup OpenBot 启动失败时,比如因为配置有误。

普通的请求错误,比如 400、401、404 或 409,不会作为错误记录。人有意中断的工作也不会记录。

每条错误日志的消息都是 OpenBot operation failed,并带有下面这些注解。注解(annotation)是附加在日志行上的一个具名值。

注解 含义
source、operation OpenBot 的哪个部分失败了。
errorType 错误的简短名称,OpenBot 知道时才有。
requestId、method、route、status 用于 HTTP 失败。route 是路由模式,不是真实的路径。
sessionId、turnId 用于 Turn 失败。

日志中还有失败的原因。原因中可能包含错误消息和其他私密数据。


把错误发送到你自己的服务

当你从自己的代码中运行 OpenBot 时,用 Logger.layer 添加一个日志器。使用 mergeWithExisting: true 来保留其他日志器。 在服务器外部提供它,这样它会一直保持打开,直到服务器关闭。

import { NodeRuntime } from "@effect/platform-node"
import { EnvironmentConfiguration, Server } from "@openbot-build/openbot"
import { Effect, Layer, Logger, References } from "effect"
import { FetchHttpClient } from "effect/http"
const alerts = Logger.layer(
[
Logger.make((log) => {
if (log.logLevel !== "Error" && log.logLevel !== "Fatal") return
const annotations = log.fiber.getRef(References.CurrentLogAnnotations)
// 只发送你需要的值,比如发给一个聊天机器人。
console.log("alert", annotations["source"], annotations["operation"])
}),
],
{ mergeWithExisting: true },
)
NodeRuntime.runMain(
Effect.gen(function* () {
const settings = yield* EnvironmentConfiguration.load
return yield* Layer.launch(
Server.layer(settings).pipe(Layer.provide(FetchHttpClient.layer)),
)
}).pipe(Effect.provide(alerts)),
)

日志器不能阻塞。要调用一个慢的服务,请把每个事件放进队列,再从后台 fiber 发送。fiber 是 Effect 运行的一种轻量线程。