@mastra/loggers 0.1.0-alpha.34 → 0.1.0-alpha.36

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @mastra/loggers
2
2
 
3
+ ## 0.1.0-alpha.36
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [4534e77]
8
+ - @mastra/core@0.2.0-alpha.103
9
+
10
+ ## 0.1.0-alpha.35
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [a9345f9]
15
+ - @mastra/core@0.2.0-alpha.102
16
+
3
17
  ## 0.1.0-alpha.34
4
18
 
5
19
  ### Patch Changes
@@ -0,0 +1,48 @@
1
+ import { BaseLogMessage } from '@mastra/core/logger';
2
+ import { LoggerTransport } from '@mastra/core/logger';
3
+ import { WriteStream } from 'fs';
4
+
5
+ export declare class FileTransport extends LoggerTransport {
6
+ path: string;
7
+ fileStream: WriteStream;
8
+ constructor({ path }: {
9
+ path: string;
10
+ });
11
+ _transform(chunk: any, _encoding: string, callback: (error: Error | null, chunk: any) => void): void;
12
+ _flush(callback: Function): void;
13
+ _destroy(error: Error, callback: Function): void;
14
+ getLogs(): Promise<BaseLogMessage[]>;
15
+ getLogsByRunId({ runId }: {
16
+ runId: string;
17
+ }): Promise<BaseLogMessage[]>;
18
+ }
19
+
20
+ export declare class UpstashTransport extends LoggerTransport {
21
+ upstashUrl: string;
22
+ upstashToken: string;
23
+ listName: string;
24
+ maxListLength: number;
25
+ batchSize: number;
26
+ flushInterval: number;
27
+ logBuffer: any[];
28
+ lastFlush: number;
29
+ flushIntervalId: NodeJS.Timeout;
30
+ constructor(opts: {
31
+ listName?: string;
32
+ maxListLength?: number;
33
+ batchSize?: number;
34
+ upstashUrl: string;
35
+ flushInterval?: number;
36
+ upstashToken: string;
37
+ });
38
+ private executeUpstashCommand;
39
+ _flush(): Promise<void>;
40
+ _transform(chunk: string, _enc: string, cb: Function): void;
41
+ _destroy(err: Error, cb: Function): void;
42
+ getLogs(): Promise<BaseLogMessage[]>;
43
+ getLogsByRunId({ runId }: {
44
+ runId: string;
45
+ }): Promise<BaseLogMessage[]>;
46
+ }
47
+
48
+ export { }
@@ -0,0 +1 @@
1
+ export { FileTransport } from '../_tsup-dts-rollup.js';
@@ -0,0 +1,51 @@
1
+ import { LoggerTransport } from '@mastra/core/logger';
2
+ import { existsSync, createWriteStream, readFileSync } from 'fs';
3
+
4
+ // src/file/index.ts
5
+ var FileTransport = class extends LoggerTransport {
6
+ path;
7
+ fileStream;
8
+ constructor({ path }) {
9
+ super({ objectMode: true });
10
+ this.path = path;
11
+ if (!existsSync(this.path)) {
12
+ console.log(this.path);
13
+ throw new Error("File path does not exist");
14
+ }
15
+ this.fileStream = createWriteStream(this.path, { flags: "a" });
16
+ }
17
+ _transform(chunk, _encoding, callback) {
18
+ try {
19
+ this.fileStream.write(chunk);
20
+ } catch (error) {
21
+ console.error("Error parsing log entry:", error);
22
+ }
23
+ callback(null, chunk);
24
+ }
25
+ _flush(callback) {
26
+ this.fileStream.end(() => {
27
+ callback();
28
+ });
29
+ }
30
+ // Clean up resources
31
+ _destroy(error, callback) {
32
+ if (this.fileStream) {
33
+ this.fileStream.destroy(error);
34
+ }
35
+ callback(error);
36
+ }
37
+ async getLogs() {
38
+ return readFileSync(this.path, "utf8").split("\n").filter(Boolean).map((log) => JSON.parse(log));
39
+ }
40
+ async getLogsByRunId({ runId }) {
41
+ try {
42
+ const allLogs = await this.getLogs();
43
+ return allLogs.filter((log) => log?.runId === runId) || [];
44
+ } catch (error) {
45
+ console.error("Error getting logs by runId from Upstash:", error);
46
+ return [];
47
+ }
48
+ }
49
+ };
50
+
51
+ export { FileTransport };
@@ -0,0 +1 @@
1
+ export { UpstashTransport } from '../_tsup-dts-rollup.js';
@@ -0,0 +1,120 @@
1
+ import { LoggerTransport } from '@mastra/core/logger';
2
+
3
+ // src/upstash/index.ts
4
+ var UpstashTransport = class extends LoggerTransport {
5
+ upstashUrl;
6
+ upstashToken;
7
+ listName;
8
+ maxListLength;
9
+ batchSize;
10
+ flushInterval;
11
+ logBuffer;
12
+ lastFlush;
13
+ flushIntervalId;
14
+ constructor(opts) {
15
+ super({ objectMode: true });
16
+ if (!opts.upstashUrl || !opts.upstashToken) {
17
+ throw new Error("Upstash URL and token are required");
18
+ }
19
+ this.upstashUrl = opts.upstashUrl;
20
+ this.upstashToken = opts.upstashToken;
21
+ this.listName = opts.listName || "application-logs";
22
+ this.maxListLength = opts.maxListLength || 1e4;
23
+ this.batchSize = opts.batchSize || 100;
24
+ this.flushInterval = opts.flushInterval || 1e4;
25
+ this.logBuffer = [];
26
+ this.lastFlush = Date.now();
27
+ this.flushIntervalId = setInterval(() => {
28
+ this._flush().catch((err) => {
29
+ console.error("Error flushing logs to Upstash:", err);
30
+ });
31
+ }, this.flushInterval);
32
+ }
33
+ async executeUpstashCommand(command) {
34
+ const response = await fetch(`${this.upstashUrl}/pipeline`, {
35
+ method: "POST",
36
+ headers: {
37
+ Authorization: `Bearer ${this.upstashToken}`,
38
+ "Content-Type": "application/json"
39
+ },
40
+ body: JSON.stringify([command])
41
+ });
42
+ if (!response.ok) {
43
+ throw new Error(`Failed to execute Upstash command: ${response.statusText}`);
44
+ }
45
+ return response.json();
46
+ }
47
+ async _flush() {
48
+ if (this.logBuffer.length === 0) {
49
+ return;
50
+ }
51
+ const now = Date.now();
52
+ const logs = this.logBuffer.splice(0, this.batchSize);
53
+ try {
54
+ const command = ["LPUSH", this.listName, ...logs.map((log) => JSON.stringify(log))];
55
+ if (this.maxListLength > 0) {
56
+ command.push("LTRIM", this.listName, 0, this.maxListLength - 1);
57
+ }
58
+ await this.executeUpstashCommand(command);
59
+ this.lastFlush = now;
60
+ } catch (error) {
61
+ this.logBuffer.unshift(...logs);
62
+ throw error;
63
+ }
64
+ }
65
+ _transform(chunk, _enc, cb) {
66
+ try {
67
+ const log = typeof chunk === "string" ? JSON.parse(chunk) : chunk;
68
+ if (!log.time) {
69
+ log.time = Date.now();
70
+ }
71
+ this.logBuffer.push(log);
72
+ if (this.logBuffer.length >= this.batchSize) {
73
+ this._flush().catch((err) => {
74
+ console.error("Error flushing logs to Upstash:", err);
75
+ });
76
+ }
77
+ cb(null, chunk);
78
+ } catch (error) {
79
+ cb(error);
80
+ }
81
+ }
82
+ _destroy(err, cb) {
83
+ clearInterval(this.flushIntervalId);
84
+ if (this.logBuffer.length > 0) {
85
+ this._flush().then(() => cb(err)).catch((flushErr) => {
86
+ console.error("Error in final flush:", flushErr);
87
+ cb(err || flushErr);
88
+ });
89
+ } else {
90
+ cb(err);
91
+ }
92
+ }
93
+ async getLogs() {
94
+ try {
95
+ const command = ["LRANGE", this.listName, 0, -1];
96
+ const response = await this.executeUpstashCommand(command);
97
+ return response?.[0]?.result.map((log) => {
98
+ try {
99
+ return JSON.parse(log);
100
+ } catch (e) {
101
+ return "";
102
+ }
103
+ });
104
+ } catch (error) {
105
+ console.error("Error getting logs from Upstash:", error);
106
+ return [];
107
+ }
108
+ }
109
+ async getLogsByRunId({ runId }) {
110
+ try {
111
+ const allLogs = await this.getLogs();
112
+ return allLogs.filter((log) => log.msg?.runId === runId) || [];
113
+ } catch (error) {
114
+ console.error("Error getting logs by runId from Upstash:", error);
115
+ return [];
116
+ }
117
+ }
118
+ };
119
+
120
+ export { UpstashTransport };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/loggers",
3
- "version": "0.1.0-alpha.34",
3
+ "version": "0.1.0-alpha.36",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "author": "",
31
31
  "license": "ISC",
32
32
  "dependencies": {
33
- "@mastra/core": "^0.2.0-alpha.101"
33
+ "@mastra/core": "^0.2.0-alpha.103"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@microsoft/api-extractor": "^7.49.2",