@mastra/core
Version:
295 lines (294 loc) • 8.6 kB
JavaScript
import { Transform } from "stream";
// ../_internal-core/dist/logger/index.js
function isAdaptableLogger(logger) {
return typeof logger.__attachObservability === "function";
}
/**
* Export a tracked exception through the adapter sink, mirroring the
* DualLogger dual-write shape (`errorId`/`domain`/`category`/`details`/`cause`
* when present on a MastraError-like value). Never throws into the caller.
*/
function exportTrackedException(ctx, error, metadata) {
if (!ctx?.options.export) return;
try {
const mastraError = error;
ctx.getLogSink()?.error(error.message, {
...mastraError.id !== void 0 ? { errorId: mastraError.id } : {},
...mastraError.domain !== void 0 ? { domain: mastraError.domain } : {},
...mastraError.category !== void 0 ? { category: mastraError.category } : {},
...mastraError.details !== void 0 ? { details: mastraError.details } : {},
...error.cause instanceof Error ? { cause: error.cause.message } : {},
...metadata
});
} catch {}
}
/**
* Adapt IMastraLogger's variadic args into the structured `data` payload of
* an exported log record. Extracts the first plain object as data,
* serializes an Error arg, and collects remaining primitives under `args`
* so the derived record preserves all context from the native call.
*/
function buildLogRecordData(args) {
const objectData = args.find((arg) => arg !== null && typeof arg === "object" && !Array.isArray(arg) && !(arg instanceof Error));
const errorArg = args.find((arg) => arg instanceof Error);
const extraArgs = args.filter((arg) => arg !== objectData && arg !== errorArg);
if (!objectData && !errorArg && extraArgs.length === 0) return void 0;
return {
...objectData ?? {},
...errorArg ? { error: {
name: errorArg.name,
message: errorArg.message,
stack: errorArg.stack
} } : {},
...extraArgs.length > 0 ? { args: extraArgs } : {}
};
}
const noopLogger = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
cleanup: async () => {},
getTransports: () => /* @__PURE__ */ new Map(),
trackException: () => {},
listLogs: async () => ({
logs: [],
total: 0,
page: 1,
perPage: 100,
hasMore: false
}),
listLogsByRunId: async () => ({
logs: [],
total: 0,
page: 1,
perPage: 100,
hasMore: false
})
};
const RegisteredLogger = {
AGENT: "AGENT",
OBSERVABILITY: "OBSERVABILITY",
AUTH: "AUTH",
BROWSER: "BROWSER",
NETWORK: "NETWORK",
WORKFLOW: "WORKFLOW",
LLM: "LLM",
TTS: "TTS",
VOICE: "VOICE",
VECTOR: "VECTOR",
BUNDLER: "BUNDLER",
DEPLOYER: "DEPLOYER",
MEMORY: "MEMORY",
STORAGE: "STORAGE",
EMBEDDINGS: "EMBEDDINGS",
MCP_SERVER: "MCP_SERVER",
SERVER_CACHE: "SERVER_CACHE",
SERVER: "SERVER",
WORKSPACE: "WORKSPACE",
CHANNEL: "CHANNEL"
};
const LogLevel = {
DEBUG: "debug",
INFO: "info",
WARN: "warn",
ERROR: "error",
NONE: "silent"
};
var LoggerTransport = class extends Transform {
constructor(opts = {}) {
super({
...opts,
objectMode: true
});
}
async listLogsByRunId(_args) {
return {
logs: [],
total: 0,
page: _args?.page ?? 1,
perPage: _args?.perPage ?? 100,
hasMore: false
};
}
async listLogs(_args) {
return {
logs: [],
total: 0,
page: _args?.page ?? 1,
perPage: _args?.perPage ?? 100,
hasMore: false
};
}
};
const createCustomTransport = (stream, listLogs, listLogsByRunId) => {
let transport = stream;
if (listLogs) transport.listLogs = listLogs;
if (listLogsByRunId) transport.listLogsByRunId = listLogsByRunId;
return transport;
};
var MastraLogger = class {
name;
level;
transports;
constructor(options = {}) {
this.name = options.name || "Mastra";
this.level = options.level || LogLevel.ERROR;
this.transports = new Map(Object.entries(options.transports || {}));
}
getTransports() {
return this.transports;
}
trackException(_error, _metadata) {}
async listLogs(transportId, params) {
if (!transportId || !this.transports.has(transportId)) return {
logs: [],
total: 0,
page: params?.page ?? 1,
perPage: params?.perPage ?? 100,
hasMore: false
};
return this.transports.get(transportId).listLogs?.(params) ?? {
logs: [],
total: 0,
page: params?.page ?? 1,
perPage: params?.perPage ?? 100,
hasMore: false
};
}
async listLogsByRunId({ transportId, runId, fromDate, toDate, logLevel, filters, page, perPage }) {
if (!transportId || !this.transports.has(transportId) || !runId) return {
logs: [],
total: 0,
page: page ?? 1,
perPage: perPage ?? 100,
hasMore: false
};
return this.transports.get(transportId).listLogsByRunId?.({
runId,
fromDate,
toDate,
logLevel,
filters,
page,
perPage
}) ?? {
logs: [],
total: 0,
page: page ?? 1,
perPage: perPage ?? 100,
hasMore: false
};
}
};
var ConsoleLogger = class ConsoleLogger extends MastraLogger {
component;
filter;
#adapterContext;
constructor(options = {}) {
super(options);
this.component = options.component;
this.filter = options.filter;
}
/**
* Adapter hook (see `AdaptableLogger`): enables native trace correlation
* (trace_id/span_id appended to console output) and observability export
* derived from the same record. Called by Mastra during setup.
*/
__attachObservability(ctx) {
this.#adapterContext = ctx;
}
child(componentOrBindings) {
const component = typeof componentOrBindings === "string" ? componentOrBindings : componentOrBindings?.component ?? this.component;
const child = new ConsoleLogger({
name: this.name,
level: this.level,
component,
filter: this.filter
});
if (this.#adapterContext) child.__attachObservability(this.#adapterContext);
return child;
}
/**
* Native-record correlation: when a span is active, append the trace
* fields object to the console args so every destination sees them.
*/
#correlate(args) {
const ctx = this.#adapterContext;
if (!ctx?.options.correlation) return args;
try {
const fields = ctx.resolveTraceFields();
return fields ? [...args, fields] : args;
} catch {
return args;
}
}
/**
* Export the record derived from the same native call to observability.
* Mirrors DualLogger semantics: export is independent of the console
* level filter, and never throws into the caller.
*/
#export(level, message, args) {
const ctx = this.#adapterContext;
if (!ctx?.options.export) return;
try {
ctx.getLogSink()?.[level](message, buildLogRecordData(args));
} catch {}
}
shouldLog(level, message, args) {
if (!this.filter) return true;
try {
return this.filter({
component: this.component,
level,
message,
args
});
} catch (e) {
console.error(`[Logger] Filter error for component=${this.component} level=${level}:`, e);
return true;
}
}
prefix() {
return this.component ? `[${this.component}] ` : "";
}
debug(message, ...args) {
if (this.level === LogLevel.DEBUG && this.shouldLog(LogLevel.DEBUG, message, args)) console.info(`${this.prefix()}${message}`, ...this.#correlate(args));
this.#export("debug", message, args);
}
info(message, ...args) {
if ((this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.INFO, message, args)) console.info(`${this.prefix()}${message}`, ...this.#correlate(args));
this.#export("info", message, args);
}
warn(message, ...args) {
if ((this.level === LogLevel.WARN || this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.WARN, message, args)) console.warn(`${this.prefix()}${message}`, ...this.#correlate(args));
this.#export("warn", message, args);
}
error(message, ...args) {
if ((this.level === LogLevel.ERROR || this.level === LogLevel.WARN || this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.ERROR, message, args)) console.error(`${this.prefix()}${message}`, ...this.#correlate(args));
this.#export("error", message, args);
}
trackException(error, metadata) {
exportTrackedException(this.#adapterContext, error, metadata);
}
async listLogs(_transportId, _params) {
return {
logs: [],
total: 0,
page: _params?.page ?? 1,
perPage: _params?.perPage ?? 100,
hasMore: false
};
}
async listLogsByRunId(_args) {
return {
logs: [],
total: 0,
page: _args.page ?? 1,
perPage: _args.perPage ?? 100,
hasMore: false
};
}
};
export { RegisteredLogger as a, exportTrackedException as c, MastraLogger as i, isAdaptableLogger as l, LogLevel as n, buildLogRecordData as o, LoggerTransport as r, createCustomTransport as s, ConsoleLogger as t, noopLogger as u };
//# sourceMappingURL=logger-BwbmRqfc.js.map