日志
🌐 Logging
Mastra 的日志系统以结构化格式捕获函数执行、输入数据和输出响应。
🌐 Mastra's logging system captures function execution, input data, and output responses in a structured format.
部署到 Mastra 云时,日志会显示在 Logs 页面。在自托管或自定义环境中,日志可以根据配置的传输方式定向到文件或外部服务。
🌐 When deploying to Mastra Cloud, logs are shown on the Logs page. In self-hosted or custom environments, logs can be directed to files or external services depending on the configured transports.
使用 PinoLogger 配置日志Direct link to 使用 PinoLogger 配置日志
🌐 Configuring logs with PinoLogger
在使用 CLI 初始化一个新的 Mastra 项目 时,PinoLogger 会默认包含在内。
🌐 When initializing a new Mastra project using the CLI, PinoLogger is included by default.
import { Mastra } from "@mastra/core/mastra";
import { PinoLogger } from "@mastra/loggers";
export const mastra = new Mastra({
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
});
访问 PinoLogger 了解所有可用的配置选项。
🌐 Visit PinoLogger for all available configuration options.
自定义日志Direct link to 自定义日志
🌐 Customizing logs
Mastra 通过 mastra.getLogger() 方法提供对日志记录器实例的访问,该方法可在工作流步骤和工具中使用。日志记录器支持标准的严重性级别:debug、info、warn 和 error。
🌐 Mastra provides access to a logger instance via the mastra.getLogger() method, available inside both workflow steps and tools. The logger supports standard severity levels: debug, info, warn, and error.
从工作流步骤记录日志Direct link to 从工作流步骤记录日志
🌐 Logging from workflow steps
在工作流步骤中,可以通过 execute 函数内的 mastra 参数访问日志记录器。这使你能够记录与步骤执行相关的消息。
🌐 Within a workflow step, access the logger via the mastra parameter inside the execute function. This allows you to log messages relevant to the step’s execution.
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({
execute: async ({ mastra }) => {
const logger = mastra.getLogger();
logger.info("workflow info log");
return {
output: ""
};
}
});
export const testWorkflow = createWorkflow({...})
.then(step1)
.commit();
来自工具的日志Direct link to 来自工具的日志
🌐 Logging from tools
同样,工具可以通过 mastra 参数访问日志记录实例。在执行过程中使用它记录特定工具的活动。
🌐 Similarly, tools have access to the logger instance via the mastra parameter. Use this to log tool specific activity during execution.
import { createTool } from "@mastra/core/tools";
import { z } from "zod";
export const testTool = createTool({
execute: async (inputData, context) => {
const logger = context?.mastra.getLogger();
logger?.info("tool info log");
return {
output: "",
};
},
});
记录附加数据Direct link to 记录附加数据
🌐 Logging with additional data
Logger 方法接受一个可选的第二个参数,用于传递附加数据。这个参数可以是任何值,例如对象、字符串或数字。
🌐 Logger methods accept an optional second argument for additional data. This can be any value, such as an object, string, or number.
在此示例中,日志消息包含一个对象,该对象的键为 agent,值为 testAgent 实例。
🌐 In this example, the log message includes an object with a key of agent and a value of the testAgent instance.
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({
execute: async ({ mastra }) => {
const testAgent = mastra.getAgent("testAgent");
const logger = mastra.getLogger();
logger.info("workflow info log", { agent: testAgent });
return {
output: ""
};
}
});
export const testWorkflow = createWorkflow({...})
.then(step1)
.commit();