@mastra/loggers 0.0.1-alpha.17 → 0.0.1-alpha.2
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 +0 -115
- package/package.json +14 -20
- package/src/file/index.test.ts +2 -2
- package/src/file/index.ts +1 -1
- package/src/index.ts +2 -0
- package/src/upstash/index.test.ts +2 -2
- package/src/upstash/index.ts +1 -1
- package/README.md +0 -146
- package/dist/file/index.d.ts +0 -19
- package/dist/file/index.js +0 -49
- package/dist/upstash/index.d.ts +0 -31
- package/dist/upstash/index.js +0 -111
package/CHANGELOG.md
CHANGED
|
@@ -1,120 +1,5 @@
|
|
|
1
1
|
# @mastra/loggers
|
|
2
2
|
|
|
3
|
-
## 0.0.1-alpha.17
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- Updated dependencies [2f17a5f]
|
|
8
|
-
- Updated dependencies [cb290ee]
|
|
9
|
-
- Updated dependencies [b4d7416]
|
|
10
|
-
- Updated dependencies [38b7f66]
|
|
11
|
-
- @mastra/core@0.2.0-alpha.84
|
|
12
|
-
|
|
13
|
-
## 0.0.1-alpha.16
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- 9625602: Use mastra core splitted bundles in other packages
|
|
18
|
-
- Updated dependencies [30322ce]
|
|
19
|
-
- Updated dependencies [78eec7c]
|
|
20
|
-
- Updated dependencies [9625602]
|
|
21
|
-
- Updated dependencies [8769a62]
|
|
22
|
-
- @mastra/core@0.2.0-alpha.83
|
|
23
|
-
|
|
24
|
-
## 0.0.1-alpha.15
|
|
25
|
-
|
|
26
|
-
### Patch Changes
|
|
27
|
-
|
|
28
|
-
- Updated dependencies [73d112c]
|
|
29
|
-
- @mastra/core@0.1.27-alpha.82
|
|
30
|
-
|
|
31
|
-
## 0.0.1-alpha.14
|
|
32
|
-
|
|
33
|
-
### Patch Changes
|
|
34
|
-
|
|
35
|
-
- Updated dependencies [9fb3039]
|
|
36
|
-
- @mastra/core@0.1.27-alpha.81
|
|
37
|
-
|
|
38
|
-
## 0.0.1-alpha.13
|
|
39
|
-
|
|
40
|
-
### Patch Changes
|
|
41
|
-
|
|
42
|
-
- Updated dependencies [327ece7]
|
|
43
|
-
- @mastra/core@0.1.27-alpha.80
|
|
44
|
-
|
|
45
|
-
## 0.0.1-alpha.12
|
|
46
|
-
|
|
47
|
-
### Patch Changes
|
|
48
|
-
|
|
49
|
-
- Updated dependencies [21fe536]
|
|
50
|
-
- @mastra/core@0.1.27-alpha.79
|
|
51
|
-
|
|
52
|
-
## 0.0.1-alpha.11
|
|
53
|
-
|
|
54
|
-
### Patch Changes
|
|
55
|
-
|
|
56
|
-
- Updated dependencies [685108a]
|
|
57
|
-
- Updated dependencies [685108a]
|
|
58
|
-
- @mastra/core@0.1.27-alpha.78
|
|
59
|
-
|
|
60
|
-
## 0.0.1-alpha.10
|
|
61
|
-
|
|
62
|
-
### Patch Changes
|
|
63
|
-
|
|
64
|
-
- cbaecb0: @mastra/loggers tsup bundling
|
|
65
|
-
- Updated dependencies [8105fae]
|
|
66
|
-
- @mastra/core@0.1.27-alpha.77
|
|
67
|
-
|
|
68
|
-
## 0.0.1-alpha.9
|
|
69
|
-
|
|
70
|
-
### Patch Changes
|
|
71
|
-
|
|
72
|
-
- Updated dependencies [ae7bf94]
|
|
73
|
-
- Updated dependencies [ae7bf94]
|
|
74
|
-
- @mastra/core@0.1.27-alpha.76
|
|
75
|
-
|
|
76
|
-
## 0.0.1-alpha.8
|
|
77
|
-
|
|
78
|
-
### Patch Changes
|
|
79
|
-
|
|
80
|
-
- Updated dependencies [23dcb23]
|
|
81
|
-
- @mastra/core@0.1.27-alpha.75
|
|
82
|
-
|
|
83
|
-
## 0.0.1-alpha.7
|
|
84
|
-
|
|
85
|
-
### Patch Changes
|
|
86
|
-
|
|
87
|
-
- Updated dependencies [7b87567]
|
|
88
|
-
- @mastra/core@0.1.27-alpha.74
|
|
89
|
-
|
|
90
|
-
## 0.0.1-alpha.6
|
|
91
|
-
|
|
92
|
-
### Patch Changes
|
|
93
|
-
|
|
94
|
-
- Updated dependencies [3427b95]
|
|
95
|
-
- @mastra/core@0.1.27-alpha.73
|
|
96
|
-
|
|
97
|
-
## 0.0.1-alpha.5
|
|
98
|
-
|
|
99
|
-
### Patch Changes
|
|
100
|
-
|
|
101
|
-
- Updated dependencies [e4d4ede]
|
|
102
|
-
- Updated dependencies [06b2c0a]
|
|
103
|
-
- @mastra/core@0.1.27-alpha.72
|
|
104
|
-
|
|
105
|
-
## 0.0.1-alpha.4
|
|
106
|
-
|
|
107
|
-
### Patch Changes
|
|
108
|
-
|
|
109
|
-
- Updated dependencies [d9c8dd0]
|
|
110
|
-
- @mastra/core@0.1.27-alpha.71
|
|
111
|
-
|
|
112
|
-
## 0.0.1-alpha.3
|
|
113
|
-
|
|
114
|
-
### Patch Changes
|
|
115
|
-
|
|
116
|
-
- bdaf834: publish packages
|
|
117
|
-
|
|
118
3
|
## 0.0.1-alpha.2
|
|
119
4
|
|
|
120
5
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,48 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/loggers",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/loggers.esm.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
11
|
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/loggers.esm.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
11
16
|
"types": "./dist/index.d.ts",
|
|
12
17
|
"default": "./dist/index.js"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
|
-
"./file": {
|
|
16
|
-
"import": {
|
|
17
|
-
"types": "./dist/file/index.d.ts",
|
|
18
|
-
"default": "./dist/file/index.js"
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
"./upstash": {
|
|
22
|
-
"import": {
|
|
23
|
-
"types": "./dist/upstash/index.d.ts",
|
|
24
|
-
"default": "./dist/upstash/index.js"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
20
|
"./package.json": "./package.json"
|
|
28
21
|
},
|
|
29
22
|
"keywords": [],
|
|
30
23
|
"author": "",
|
|
31
24
|
"license": "ISC",
|
|
32
25
|
"dependencies": {
|
|
33
|
-
"@mastra/core": "0.
|
|
26
|
+
"@mastra/core": "0.1.27-alpha.70"
|
|
34
27
|
},
|
|
35
28
|
"devDependencies": {
|
|
36
29
|
"@babel/preset-env": "^7.26.0",
|
|
37
30
|
"@babel/preset-typescript": "^7.26.0",
|
|
38
31
|
"@tsconfig/recommended": "^1.0.7",
|
|
32
|
+
"@types/jsdom": "^21.1.7",
|
|
39
33
|
"@types/node": "^22.9.0",
|
|
40
|
-
"
|
|
41
|
-
"vitest": "^
|
|
34
|
+
"dts-cli": "^2.0.5",
|
|
35
|
+
"vitest": "^2.1.8"
|
|
42
36
|
},
|
|
43
37
|
"scripts": {
|
|
44
|
-
"build": "
|
|
45
|
-
"dev": "
|
|
38
|
+
"build": "dts build",
|
|
39
|
+
"build:dev": "dts watch",
|
|
46
40
|
"test": "vitest run"
|
|
47
41
|
}
|
|
48
42
|
}
|
package/src/file/index.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createLogger, LogLevel } from '@mastra/core
|
|
1
|
+
import { createLogger, LogLevel } from '@mastra/core';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { describe, it, expect, beforeEach, afterEach, vi, afterAll } from 'vitest';
|
|
@@ -72,7 +72,7 @@ describe('FileTransport', () => {
|
|
|
72
72
|
it('should properly clean up resources on destroy', () => {
|
|
73
73
|
const destroySpy = vi.spyOn(fileLogger.fileStream, 'destroy');
|
|
74
74
|
|
|
75
|
-
fileLogger._destroy(
|
|
75
|
+
fileLogger._destroy(null, () => {
|
|
76
76
|
expect(destroySpy).toHaveBeenCalled();
|
|
77
77
|
});
|
|
78
78
|
});
|
package/src/file/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LoggerTransport, BaseLogMessage } from '@mastra/core
|
|
1
|
+
import { LoggerTransport, BaseLogMessage } from '@mastra/core';
|
|
2
2
|
import { createWriteStream, existsSync, readFileSync, WriteStream } from 'fs';
|
|
3
3
|
|
|
4
4
|
export class FileTransport extends LoggerTransport {
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createLogger, LogLevel } from '@mastra/core
|
|
1
|
+
import { createLogger, LogLevel } from '@mastra/core';
|
|
2
2
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
3
3
|
|
|
4
4
|
import { UpstashTransport } from './index.js';
|
|
@@ -95,7 +95,7 @@ describe('UpstashTransport', () => {
|
|
|
95
95
|
const clearIntervalSpy = vi.spyOn(global, 'clearInterval');
|
|
96
96
|
const flushSpy = vi.spyOn(transport, '_flush').mockImplementation(() => Promise.resolve());
|
|
97
97
|
|
|
98
|
-
transport._destroy(
|
|
98
|
+
transport._destroy(null, () => {
|
|
99
99
|
expect(clearIntervalSpy).toHaveBeenCalled();
|
|
100
100
|
if (transport.logBuffer.length > 0) {
|
|
101
101
|
expect(flushSpy).toHaveBeenCalled();
|
package/src/upstash/index.ts
CHANGED
package/README.md
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
# @mastra/loggers
|
|
2
|
-
|
|
3
|
-
A collection of logging transport implementations for Mastra, extending the `LoggerTransport` class from `@mastra/core`.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @mastra/loggers
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Available Transports
|
|
12
|
-
|
|
13
|
-
### File Transport
|
|
14
|
-
|
|
15
|
-
A transport that writes logs to a local file system.
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
import { FileTransport } from '@mastra/loggers';
|
|
19
|
-
|
|
20
|
-
const fileLogger = new FileTransport({
|
|
21
|
-
path: '/path/to/logs/app.log',
|
|
22
|
-
});
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
#### Configuration
|
|
26
|
-
|
|
27
|
-
- `path`: Absolute path to the log file (must exist)
|
|
28
|
-
|
|
29
|
-
#### Features
|
|
30
|
-
|
|
31
|
-
- Append-mode logging
|
|
32
|
-
- Automatic stream cleanup
|
|
33
|
-
- JSON log parsing
|
|
34
|
-
- Query logs by run ID
|
|
35
|
-
- Stream-based implementation
|
|
36
|
-
|
|
37
|
-
### Upstash Transport
|
|
38
|
-
|
|
39
|
-
A transport that sends logs to Upstash Redis with batching and auto-trimming capabilities.
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
import { UpstashTransport } from '@mastra/loggers';
|
|
43
|
-
|
|
44
|
-
const upstashLogger = new UpstashTransport({
|
|
45
|
-
upstashUrl: 'https://your-instance.upstash.io',
|
|
46
|
-
upstashToken: 'your-token',
|
|
47
|
-
listName: 'application-logs', // optional
|
|
48
|
-
maxListLength: 10000, // optional
|
|
49
|
-
batchSize: 100, // optional
|
|
50
|
-
flushInterval: 10000, // optional
|
|
51
|
-
});
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
#### Configuration
|
|
55
|
-
|
|
56
|
-
Required:
|
|
57
|
-
|
|
58
|
-
- `upstashUrl`: Your Upstash Redis instance URL
|
|
59
|
-
- `upstashToken`: Your Upstash authentication token
|
|
60
|
-
|
|
61
|
-
Optional:
|
|
62
|
-
|
|
63
|
-
- `listName`: Redis list name for logs (default: 'application-logs')
|
|
64
|
-
- `maxListLength`: Maximum number of logs to keep (default: 10000)
|
|
65
|
-
- `batchSize`: Number of logs to send in one batch (default: 100)
|
|
66
|
-
- `flushInterval`: Milliseconds between flush attempts (default: 10000)
|
|
67
|
-
|
|
68
|
-
#### Features
|
|
69
|
-
|
|
70
|
-
- Batched log writing
|
|
71
|
-
- Automatic log rotation (LTRIM)
|
|
72
|
-
- Configurable buffer size
|
|
73
|
-
- Automatic retry on failure
|
|
74
|
-
- Query logs by run ID
|
|
75
|
-
- JSON log formatting
|
|
76
|
-
- Timestamp auto-injection
|
|
77
|
-
- Graceful shutdown with final flush
|
|
78
|
-
|
|
79
|
-
## Usage with Mastra Core
|
|
80
|
-
|
|
81
|
-
Both transports implement the `LoggerTransport` interface from `@mastra/core`:
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
import { Logger } from '@mastra/core/logger';
|
|
85
|
-
import { FileTransport, UpstashTransport } from '@mastra/loggers';
|
|
86
|
-
|
|
87
|
-
// Create transports
|
|
88
|
-
const fileTransport = new FileTransport({
|
|
89
|
-
path: '/var/log/app.log',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const upstashTransport = new UpstashTransport({
|
|
93
|
-
upstashUrl: process.env.UPSTASH_URL!,
|
|
94
|
-
upstashToken: process.env.UPSTASH_TOKEN!,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// Create logger with multiple transports
|
|
98
|
-
const logger = new Logger({
|
|
99
|
-
transports: [fileTransport, upstashTransport],
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// Log messages
|
|
103
|
-
logger.info('Hello world', { metadata: 'value' });
|
|
104
|
-
|
|
105
|
-
// Query logs
|
|
106
|
-
const allLogs = await fileTransport.getLogs();
|
|
107
|
-
const runLogs = await upstashTransport.getLogsByRunId({ runId: 'abc-123' });
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
## Log Message Format
|
|
111
|
-
|
|
112
|
-
Both transports handle log messages in JSON format with the following structure:
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
interface BaseLogMessage {
|
|
116
|
-
time?: number; // Timestamp (auto-injected if not present)
|
|
117
|
-
level?: string; // Log level
|
|
118
|
-
msg?: {
|
|
119
|
-
// Message content
|
|
120
|
-
runId?: string; // Optional run ID for grouping logs
|
|
121
|
-
[key: string]: any;
|
|
122
|
-
};
|
|
123
|
-
[key: string]: any;
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## Error Handling
|
|
128
|
-
|
|
129
|
-
Both transports include robust error handling:
|
|
130
|
-
|
|
131
|
-
- File Transport:
|
|
132
|
-
|
|
133
|
-
- Validates file path existence
|
|
134
|
-
- Handles stream errors
|
|
135
|
-
- Graceful cleanup on destroy
|
|
136
|
-
|
|
137
|
-
- Upstash Transport:
|
|
138
|
-
- Validates required configuration
|
|
139
|
-
- Retries failed batches
|
|
140
|
-
- Buffers logs during outages
|
|
141
|
-
- Graceful shutdown with final flush
|
|
142
|
-
|
|
143
|
-
## Related Links
|
|
144
|
-
|
|
145
|
-
- [Upstash Redis Documentation](https://docs.upstash.com/redis)
|
|
146
|
-
- [Node.js Stream Documentation](https://nodejs.org/api/stream.html)
|
package/dist/file/index.d.ts
DELETED
|
@@ -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 };
|
package/dist/file/index.js
DELETED
|
@@ -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 };
|
package/dist/upstash/index.d.ts
DELETED
|
@@ -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 };
|
package/dist/upstash/index.js
DELETED
|
@@ -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 };
|