@mastra/loggers 1.3.6-alpha.2 → 1.3.6-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/file/index.cjs +16 -16
- package/dist/file/index.cjs.map +1 -1
- package/dist/file/index.d.ts.map +1 -1
- package/dist/file/index.js +16 -16
- package/dist/file/index.js.map +1 -1
- package/package.json +2 -2
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-loggers
|
|
|
3
3
|
description: Documentation for @mastra/loggers. Use when working with @mastra/loggers APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/loggers"
|
|
6
|
-
version: "1.3.6-alpha.
|
|
6
|
+
version: "1.3.6-alpha.3"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
package/dist/file/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _mastra_core_logger = require("@mastra/core/logger");
|
|
3
3
|
let fs = require("fs");
|
|
4
4
|
let readline = require("readline");
|
|
5
|
+
let _mastra_core_error = require("@mastra/core/error");
|
|
5
6
|
//#region src/file/index.ts
|
|
6
7
|
var FileTransport = class extends _mastra_core_logger.LoggerTransport {
|
|
7
8
|
path;
|
|
@@ -49,14 +50,12 @@ var FileTransport = class extends _mastra_core_logger.LoggerTransport {
|
|
|
49
50
|
try {
|
|
50
51
|
return await this.#queryLogs(params || {});
|
|
51
52
|
} catch (error) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
hasMore: false
|
|
59
|
-
};
|
|
53
|
+
throw new _mastra_core_error.MastraError({
|
|
54
|
+
id: "FILE_TRANSPORT_LIST_LOGS_FAILED",
|
|
55
|
+
domain: _mastra_core_error.ErrorDomain.MASTRA_OBSERVABILITY,
|
|
56
|
+
category: _mastra_core_error.ErrorCategory.SYSTEM,
|
|
57
|
+
details: { path: this.path }
|
|
58
|
+
}, error);
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
async listLogsByRunId({ runId, fromDate, toDate, logLevel, filters, page, perPage }) {
|
|
@@ -71,14 +70,15 @@ var FileTransport = class extends _mastra_core_logger.LoggerTransport {
|
|
|
71
70
|
perPage
|
|
72
71
|
});
|
|
73
72
|
} catch (error) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
throw new _mastra_core_error.MastraError({
|
|
74
|
+
id: "FILE_TRANSPORT_LIST_LOGS_BY_RUN_ID_FAILED",
|
|
75
|
+
domain: _mastra_core_error.ErrorDomain.MASTRA_OBSERVABILITY,
|
|
76
|
+
category: _mastra_core_error.ErrorCategory.SYSTEM,
|
|
77
|
+
details: {
|
|
78
|
+
path: this.path,
|
|
79
|
+
runId
|
|
80
|
+
}
|
|
81
|
+
}, error);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
/**
|
package/dist/file/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["LoggerTransport","#queryLogs"],"sources":["../../src/file/index.ts"],"sourcesContent":["import type { WriteStream } from 'node:fs';\nimport { createReadStream, createWriteStream, existsSync, statSync } from 'node:fs';\nimport { createInterface } from 'node:readline';\nimport { LoggerTransport } from '@mastra/core/logger';\nimport type { BaseLogMessage, LogLevel } from '@mastra/core/logger';\n\nexport class FileTransport extends LoggerTransport {\n path: string;\n fileStream: WriteStream;\n constructor({ path }: { path: string }) {\n super({ objectMode: true });\n this.path = path;\n\n if (!existsSync(this.path)) {\n throw new Error('File path does not exist');\n }\n\n if (!statSync(this.path).isFile()) {\n throw new Error(`File path must point to a file: ${this.path}`);\n }\n\n this.fileStream = createWriteStream(this.path, { flags: 'a' });\n // Without a listener, the file stream's 'error' event would crash the process.\n // Write failures already reach the transport through the write callback, so only\n // forward errors that have not destroyed the transport yet (e.g. open failures).\n this.fileStream.on('error', error => {\n if (!this.destroyed) this.destroy(error);\n });\n }\n\n _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk?: any) => void) {\n try {\n this.fileStream.write(chunk, error => {\n if (error) callback(error);\n else callback(null, chunk);\n });\n } catch (error) {\n callback(error as Error);\n }\n }\n\n _flush(callback: Function) {\n // End the file stream when transform stream ends\n this.fileStream.end(() => {\n callback();\n });\n }\n\n _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean {\n if (typeof callback === 'function') {\n this._transform(chunk, encoding || 'utf8', callback);\n return true;\n }\n\n this._transform(chunk, encoding || 'utf8', (error: Error | null) => {\n if (error) console.error('Transform error in write:', error);\n });\n return true;\n }\n\n // Clean up resources\n _destroy(error: Error, callback: Function) {\n if (this.fileStream) {\n this.fileStream.destroy(error);\n }\n callback(error);\n }\n\n async listLogs(params?: {\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean; // default true\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs(params || {});\n } catch (error) {\n
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["LoggerTransport","#queryLogs","MastraError","ErrorDomain","ErrorCategory"],"sources":["../../src/file/index.ts"],"sourcesContent":["import type { WriteStream } from 'node:fs';\nimport { createReadStream, createWriteStream, existsSync, statSync } from 'node:fs';\nimport { createInterface } from 'node:readline';\nimport { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport { LoggerTransport } from '@mastra/core/logger';\nimport type { BaseLogMessage, LogLevel } from '@mastra/core/logger';\n\nexport class FileTransport extends LoggerTransport {\n path: string;\n fileStream: WriteStream;\n constructor({ path }: { path: string }) {\n super({ objectMode: true });\n this.path = path;\n\n if (!existsSync(this.path)) {\n throw new Error('File path does not exist');\n }\n\n if (!statSync(this.path).isFile()) {\n throw new Error(`File path must point to a file: ${this.path}`);\n }\n\n this.fileStream = createWriteStream(this.path, { flags: 'a' });\n // Without a listener, the file stream's 'error' event would crash the process.\n // Write failures already reach the transport through the write callback, so only\n // forward errors that have not destroyed the transport yet (e.g. open failures).\n this.fileStream.on('error', error => {\n if (!this.destroyed) this.destroy(error);\n });\n }\n\n _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk?: any) => void) {\n try {\n this.fileStream.write(chunk, error => {\n if (error) callback(error);\n else callback(null, chunk);\n });\n } catch (error) {\n callback(error as Error);\n }\n }\n\n _flush(callback: Function) {\n // End the file stream when transform stream ends\n this.fileStream.end(() => {\n callback();\n });\n }\n\n _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean {\n if (typeof callback === 'function') {\n this._transform(chunk, encoding || 'utf8', callback);\n return true;\n }\n\n this._transform(chunk, encoding || 'utf8', (error: Error | null) => {\n if (error) console.error('Transform error in write:', error);\n });\n return true;\n }\n\n // Clean up resources\n _destroy(error: Error, callback: Function) {\n if (this.fileStream) {\n this.fileStream.destroy(error);\n }\n callback(error);\n }\n\n async listLogs(params?: {\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean; // default true\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs(params || {});\n } catch (error) {\n throw new MastraError(\n {\n id: 'FILE_TRANSPORT_LIST_LOGS_FAILED',\n domain: ErrorDomain.MASTRA_OBSERVABILITY,\n category: ErrorCategory.SYSTEM,\n details: { path: this.path },\n },\n error,\n );\n }\n }\n\n async listLogsByRunId({\n runId,\n fromDate,\n toDate,\n logLevel,\n filters,\n page,\n perPage,\n }: {\n runId: string;\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs({ runId, fromDate, toDate, logLevel, filters, page, perPage });\n } catch (error) {\n throw new MastraError(\n {\n id: 'FILE_TRANSPORT_LIST_LOGS_BY_RUN_ID_FAILED',\n domain: ErrorDomain.MASTRA_OBSERVABILITY,\n category: ErrorCategory.SYSTEM,\n details: { path: this.path, runId },\n },\n error,\n );\n }\n }\n\n /**\n * Streams the log file line by line, counting every matching record but\n * retaining only the requested page (or all matches when pagination is off).\n */\n async #queryLogs({\n runId,\n fromDate,\n toDate,\n logLevel,\n filters,\n returnPaginationResults = true,\n page: pageInput,\n perPage: perPageInput,\n }: {\n runId?: string;\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean;\n page?: number;\n perPage?: number;\n }) {\n const page = pageInput === 0 ? 1 : (pageInput ?? 1);\n const perPage = (perPageInput ?? 100) || 100;\n const start = (page - 1) * perPage;\n const end = start + perPage;\n const filterEntries = Object.entries(filters ?? {});\n\n const logs: BaseLogMessage[] = [];\n let total = 0;\n\n const lines = createInterface({ input: createReadStream(this.path, 'utf8'), crlfDelay: Infinity });\n for await (const line of lines) {\n if (!line.trim()) continue;\n\n let log: any;\n try {\n log = JSON.parse(line);\n } catch {\n continue;\n }\n\n if (log === null || typeof log !== 'object') continue;\n if (runId !== undefined && log.runId !== runId) continue;\n if (!filterEntries.every(([key, value]) => log[key] === value)) continue;\n if (logLevel && log.level !== logLevel) continue;\n if (fromDate && !(new Date(log.time).getTime() >= fromDate.getTime())) continue;\n if (toDate && !(new Date(log.time).getTime() <= toDate.getTime())) continue;\n\n if (!returnPaginationResults || (total >= start && total < end)) {\n logs.push(log);\n }\n total++;\n }\n\n if (!returnPaginationResults) {\n return { logs, total, page, perPage: total, hasMore: false };\n }\n\n return { logs, total, page, perPage, hasMore: end < total };\n }\n}\n"],"mappings":";;;;;;AAOA,IAAa,gBAAb,cAAmCA,oBAAAA,gBAAgB;CACjD;CACA;CACA,YAAY,EAAE,QAA0B;EACtC,MAAM,EAAE,YAAY,KAAK,CAAC;EAC1B,KAAK,OAAO;EAEZ,IAAI,EAAA,GAAA,GAAA,WAAA,CAAY,KAAK,IAAI,GACvB,MAAM,IAAI,MAAM,0BAA0B;EAG5C,IAAI,EAAA,GAAA,GAAA,SAAA,CAAU,KAAK,IAAI,CAAC,CAAC,OAAO,GAC9B,MAAM,IAAI,MAAM,mCAAmC,KAAK,MAAM;EAGhE,KAAK,cAAA,GAAA,GAAA,kBAAA,CAA+B,KAAK,MAAM,EAAE,OAAO,IAAI,CAAC;EAI7D,KAAK,WAAW,GAAG,UAAS,UAAS;GACnC,IAAI,CAAC,KAAK,WAAW,KAAK,QAAQ,KAAK;EACzC,CAAC;CACH;CAEA,WAAW,OAAY,WAAmB,UAAsD;EAC9F,IAAI;GACF,KAAK,WAAW,MAAM,QAAO,UAAS;IACpC,IAAI,OAAO,SAAS,KAAK;SACpB,SAAS,MAAM,KAAK;GAC3B,CAAC;EACH,SAAS,OAAO;GACd,SAAS,KAAc;EACzB;CACF;CAEA,OAAO,UAAoB;EAEzB,KAAK,WAAW,UAAU;GACxB,SAAS;EACX,CAAC;CACH;CAEA,OAAO,OAAY,UAAmB,UAAoD;EACxF,IAAI,OAAO,aAAa,YAAY;GAClC,KAAK,WAAW,OAAO,YAAY,QAAQ,QAAQ;GACnD,OAAO;EACT;EAEA,KAAK,WAAW,OAAO,YAAY,SAAS,UAAwB;GAClE,IAAI,OAAO,QAAQ,MAAM,6BAA6B,KAAK;EAC7D,CAAC;EACD,OAAO;CACT;CAGA,SAAS,OAAc,UAAoB;EACzC,IAAI,KAAK,YACP,KAAK,WAAW,QAAQ,KAAK;EAE/B,SAAS,KAAK;CAChB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AAQ,UAAU;GAC7C,IAAI,UAAU,KAAA,KAAa,IAAI,UAAU,OAAO;GAChD,IAAI,CAAC,cAAc,OAAO,CAAC,KAAK,WAAW,IAAI,SAAS,KAAK,GAAG;GAChE,IAAI,YAAY,IAAI,UAAU,UAAU;GACxC,IAAI,YAAY,EAAE,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,QAAQ,IAAI;GACvE,IAAI,UAAU,EAAE,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,QAAQ,KAAK,OAAO,QAAQ,IAAI;GAEnE,IAAI,CAAC,2BAA4B,SAAS,SAAS,QAAQ,KACzD,KAAK,KAAK,GAAG;GAEf;EACF;EAEA,IAAI,CAAC,yBACH,OAAO;GAAE;GAAM;GAAO;GAAM,SAAS;GAAO,SAAS;EAAM;EAG7D,OAAO;GAAE;GAAM;GAAO;GAAM;GAAS,SAAS,MAAM;EAAM;CAC5D;AACF"}
|
package/dist/file/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAI3C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAEpE,qBAAa,aAAc,SAAQ,eAAe;;IAChD,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,WAAW,CAAC;IACxB,YAAY,EAAE,IAAI,EAAE,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,EAmBrC;IAED,UAAU,CAAC,KAAK,EAAE,GAAG,E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AC,CAAC;QAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,GAAG,OAAO,CAAC;QACV,IAAI,EAAE,cAAc,EAAE,CAAC;QACvB,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,OAAO,CAAC;KAClB,CAAC,CAcD;CAgEF"}
|
package/dist/file/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LoggerTransport } from "@mastra/core/logger";
|
|
2
2
|
import { createReadStream, createWriteStream, existsSync, statSync } from "fs";
|
|
3
3
|
import { createInterface } from "readline";
|
|
4
|
+
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
4
5
|
//#region src/file/index.ts
|
|
5
6
|
var FileTransport = class extends LoggerTransport {
|
|
6
7
|
path;
|
|
@@ -48,14 +49,12 @@ var FileTransport = class extends LoggerTransport {
|
|
|
48
49
|
try {
|
|
49
50
|
return await this.#queryLogs(params || {});
|
|
50
51
|
} catch (error) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
hasMore: false
|
|
58
|
-
};
|
|
52
|
+
throw new MastraError({
|
|
53
|
+
id: "FILE_TRANSPORT_LIST_LOGS_FAILED",
|
|
54
|
+
domain: ErrorDomain.MASTRA_OBSERVABILITY,
|
|
55
|
+
category: ErrorCategory.SYSTEM,
|
|
56
|
+
details: { path: this.path }
|
|
57
|
+
}, error);
|
|
59
58
|
}
|
|
60
59
|
}
|
|
61
60
|
async listLogsByRunId({ runId, fromDate, toDate, logLevel, filters, page, perPage }) {
|
|
@@ -70,14 +69,15 @@ var FileTransport = class extends LoggerTransport {
|
|
|
70
69
|
perPage
|
|
71
70
|
});
|
|
72
71
|
} catch (error) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
throw new MastraError({
|
|
73
|
+
id: "FILE_TRANSPORT_LIST_LOGS_BY_RUN_ID_FAILED",
|
|
74
|
+
domain: ErrorDomain.MASTRA_OBSERVABILITY,
|
|
75
|
+
category: ErrorCategory.SYSTEM,
|
|
76
|
+
details: {
|
|
77
|
+
path: this.path,
|
|
78
|
+
runId
|
|
79
|
+
}
|
|
80
|
+
}, error);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
/**
|
package/dist/file/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["#queryLogs"],"sources":["../../src/file/index.ts"],"sourcesContent":["import type { WriteStream } from 'node:fs';\nimport { createReadStream, createWriteStream, existsSync, statSync } from 'node:fs';\nimport { createInterface } from 'node:readline';\nimport { LoggerTransport } from '@mastra/core/logger';\nimport type { BaseLogMessage, LogLevel } from '@mastra/core/logger';\n\nexport class FileTransport extends LoggerTransport {\n path: string;\n fileStream: WriteStream;\n constructor({ path }: { path: string }) {\n super({ objectMode: true });\n this.path = path;\n\n if (!existsSync(this.path)) {\n throw new Error('File path does not exist');\n }\n\n if (!statSync(this.path).isFile()) {\n throw new Error(`File path must point to a file: ${this.path}`);\n }\n\n this.fileStream = createWriteStream(this.path, { flags: 'a' });\n // Without a listener, the file stream's 'error' event would crash the process.\n // Write failures already reach the transport through the write callback, so only\n // forward errors that have not destroyed the transport yet (e.g. open failures).\n this.fileStream.on('error', error => {\n if (!this.destroyed) this.destroy(error);\n });\n }\n\n _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk?: any) => void) {\n try {\n this.fileStream.write(chunk, error => {\n if (error) callback(error);\n else callback(null, chunk);\n });\n } catch (error) {\n callback(error as Error);\n }\n }\n\n _flush(callback: Function) {\n // End the file stream when transform stream ends\n this.fileStream.end(() => {\n callback();\n });\n }\n\n _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean {\n if (typeof callback === 'function') {\n this._transform(chunk, encoding || 'utf8', callback);\n return true;\n }\n\n this._transform(chunk, encoding || 'utf8', (error: Error | null) => {\n if (error) console.error('Transform error in write:', error);\n });\n return true;\n }\n\n // Clean up resources\n _destroy(error: Error, callback: Function) {\n if (this.fileStream) {\n this.fileStream.destroy(error);\n }\n callback(error);\n }\n\n async listLogs(params?: {\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean; // default true\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs(params || {});\n } catch (error) {\n
|
|
1
|
+
{"version":3,"file":"index.js","names":["#queryLogs"],"sources":["../../src/file/index.ts"],"sourcesContent":["import type { WriteStream } from 'node:fs';\nimport { createReadStream, createWriteStream, existsSync, statSync } from 'node:fs';\nimport { createInterface } from 'node:readline';\nimport { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport { LoggerTransport } from '@mastra/core/logger';\nimport type { BaseLogMessage, LogLevel } from '@mastra/core/logger';\n\nexport class FileTransport extends LoggerTransport {\n path: string;\n fileStream: WriteStream;\n constructor({ path }: { path: string }) {\n super({ objectMode: true });\n this.path = path;\n\n if (!existsSync(this.path)) {\n throw new Error('File path does not exist');\n }\n\n if (!statSync(this.path).isFile()) {\n throw new Error(`File path must point to a file: ${this.path}`);\n }\n\n this.fileStream = createWriteStream(this.path, { flags: 'a' });\n // Without a listener, the file stream's 'error' event would crash the process.\n // Write failures already reach the transport through the write callback, so only\n // forward errors that have not destroyed the transport yet (e.g. open failures).\n this.fileStream.on('error', error => {\n if (!this.destroyed) this.destroy(error);\n });\n }\n\n _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk?: any) => void) {\n try {\n this.fileStream.write(chunk, error => {\n if (error) callback(error);\n else callback(null, chunk);\n });\n } catch (error) {\n callback(error as Error);\n }\n }\n\n _flush(callback: Function) {\n // End the file stream when transform stream ends\n this.fileStream.end(() => {\n callback();\n });\n }\n\n _write(chunk: any, encoding?: string, callback?: (error?: Error | null) => void): boolean {\n if (typeof callback === 'function') {\n this._transform(chunk, encoding || 'utf8', callback);\n return true;\n }\n\n this._transform(chunk, encoding || 'utf8', (error: Error | null) => {\n if (error) console.error('Transform error in write:', error);\n });\n return true;\n }\n\n // Clean up resources\n _destroy(error: Error, callback: Function) {\n if (this.fileStream) {\n this.fileStream.destroy(error);\n }\n callback(error);\n }\n\n async listLogs(params?: {\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean; // default true\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs(params || {});\n } catch (error) {\n throw new MastraError(\n {\n id: 'FILE_TRANSPORT_LIST_LOGS_FAILED',\n domain: ErrorDomain.MASTRA_OBSERVABILITY,\n category: ErrorCategory.SYSTEM,\n details: { path: this.path },\n },\n error,\n );\n }\n }\n\n async listLogsByRunId({\n runId,\n fromDate,\n toDate,\n logLevel,\n filters,\n page,\n perPage,\n }: {\n runId: string;\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n page?: number;\n perPage?: number;\n }): Promise<{\n logs: BaseLogMessage[];\n total: number;\n page: number;\n perPage: number;\n hasMore: boolean;\n }> {\n try {\n return await this.#queryLogs({ runId, fromDate, toDate, logLevel, filters, page, perPage });\n } catch (error) {\n throw new MastraError(\n {\n id: 'FILE_TRANSPORT_LIST_LOGS_BY_RUN_ID_FAILED',\n domain: ErrorDomain.MASTRA_OBSERVABILITY,\n category: ErrorCategory.SYSTEM,\n details: { path: this.path, runId },\n },\n error,\n );\n }\n }\n\n /**\n * Streams the log file line by line, counting every matching record but\n * retaining only the requested page (or all matches when pagination is off).\n */\n async #queryLogs({\n runId,\n fromDate,\n toDate,\n logLevel,\n filters,\n returnPaginationResults = true,\n page: pageInput,\n perPage: perPageInput,\n }: {\n runId?: string;\n fromDate?: Date;\n toDate?: Date;\n logLevel?: LogLevel;\n filters?: Record<string, any>;\n returnPaginationResults?: boolean;\n page?: number;\n perPage?: number;\n }) {\n const page = pageInput === 0 ? 1 : (pageInput ?? 1);\n const perPage = (perPageInput ?? 100) || 100;\n const start = (page - 1) * perPage;\n const end = start + perPage;\n const filterEntries = Object.entries(filters ?? {});\n\n const logs: BaseLogMessage[] = [];\n let total = 0;\n\n const lines = createInterface({ input: createReadStream(this.path, 'utf8'), crlfDelay: Infinity });\n for await (const line of lines) {\n if (!line.trim()) continue;\n\n let log: any;\n try {\n log = JSON.parse(line);\n } catch {\n continue;\n }\n\n if (log === null || typeof log !== 'object') continue;\n if (runId !== undefined && log.runId !== runId) continue;\n if (!filterEntries.every(([key, value]) => log[key] === value)) continue;\n if (logLevel && log.level !== logLevel) continue;\n if (fromDate && !(new Date(log.time).getTime() >= fromDate.getTime())) continue;\n if (toDate && !(new Date(log.time).getTime() <= toDate.getTime())) continue;\n\n if (!returnPaginationResults || (total >= start && total < end)) {\n logs.push(log);\n }\n total++;\n }\n\n if (!returnPaginationResults) {\n return { logs, total, page, perPage: total, hasMore: false };\n }\n\n return { logs, total, page, perPage, hasMore: end < total };\n }\n}\n"],"mappings":";;;;;AAOA,IAAa,gBAAb,cAAmC,gBAAgB;CACjD;CACA;CACA,YAAY,EAAE,QAA0B;EACtC,MAAM,EAAE,YAAY,KAAK,CAAC;EAC1B,KAAK,OAAO;EAEZ,IAAI,CAAC,WAAW,KAAK,IAAI,GACvB,MAAM,IAAI,MAAM,0BAA0B;EAG5C,IAAI,CAAC,SAAS,KAAK,IAAI,CAAC,CAAC,OAAO,GAC9B,MAAM,IAAI,MAAM,mCAAmC,KAAK,MAAM;EAGhE,KAAK,aAAa,kBAAkB,KAAK,MAAM,EAAE,OAAO,IAAI,CAAC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-1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/loggers",
|
|
3
|
-
"version": "1.3.6-alpha.
|
|
3
|
+
"version": "1.3.6-alpha.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"vitest": "4.1.11",
|
|
79
79
|
"@internal/lint": "0.0.141",
|
|
80
80
|
"@internal/types-builder": "0.0.116",
|
|
81
|
-
"@mastra/core": "1.76.0-alpha.
|
|
81
|
+
"@mastra/core": "1.76.0-alpha.6"
|
|
82
82
|
},
|
|
83
83
|
"peerDependencies": {
|
|
84
84
|
"@mastra/core": ">=1.0.0-0 <2.0.0-0"
|