日志记录
GenAIScript使用debug库进行日志记录。 这是一个非常灵活且功能强大的日志记录库,允许您启用或禁用特定命名空间的日志记录。
脚本记录器
env.dbg
是一个以script
为命名空间的调试记录器。
调试记录器消息不会发送到markdown跟踪中。
// put this at the top of your script// so you can use `dbg` throughout the fileconst { dbg } = env
dgb("This is a debug message!")
查看日志
默认情况下,调试日志记录是禁用的。您需要使用命名空间模式来启用它。
脚本消息可以通过运行DEBUG=
来查看。
genascript run poem --dbg script
或者使用 DEBUG
环境变量。
DEBUG=script genaiscript run ...
您可以通过用逗号分隔来指定多个类别。
genascript run poem --dbg script file config modelalias
或
DEBUG=script,genaiscript:* genaiscript run ...
*
字符可用作通配符。例如,假设您的库中有名为connect:bodyParser
、connect:compress
、connect:session
的调试器,您无需逐个列出所有三个调试器如DEBUG=connect:bodyParser,connect:compress,connect:session
,只需使用DEBUG=connect:*
即可,或者要运行使用此模块的所有内容,只需使用DEBUG=*
。
您还可以通过在特定调试器前添加-
字符来排除它们。
例如,DEBUG=*,-connect:*
将包含除以connect:
开头的所有调试器。
Visual Studio Code
打开GenAIScript脚本设置并启用"诊断"功能(等同于将命名空间设置为'*') 或者明确将DEBUG设置为您想要启用的特定命名空间。
DEBUG=script
默认值为 script
。
命令行
要通过cli启用日志记录,您需要将DEBUG
环境变量设置为要启用的命名空间。例如,要为sample
命名空间启用日志记录,可以像这样运行脚本:
DEBUG=script genaiscript run poem
你将看到以下输出:
sample This is a debug message +0ms sample This is a debug message with a variable: variable +0ms sample This is a debug message with an object: { key: 'value' } +0msTo see log messages, run the script with DEBUG=genai:sampleDEBUG=sample genaiscript run debug
自定义日志记录器
您可以使用host.logger
创建一个带有特定命名空间的自定义日志记录器。
const d = host.logger("sample")
d("This is a debug message")d("This is a debug message with a variable: %s", "variable")d("This is a debug message with an object: %o", { key: "value" })
console.log("To see log messages, run the script with DEBUG=genai:sample")console.log("DEBUG=sample genaiscript run debug")
并将DEBUG
环境变量的值更新为您想要启用的命名空间。
DEBUG=sample genaiscript run debug
GenAIScript 内置日志功能
- GenAIScript中的所有内部日志都以
genaiscript:
作为前缀。
DEBUG=genaiscript:* genaiscript run ...
- 代理日志以
agent:name
为前缀。
DEBUG=genaiscript:* genaiscript run ...