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

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