@nm-logger/logger 1.1.7 → 1.1.8
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/package.json +3 -2
- package/src/DailyWatcher.js +36 -10
- package/src/S3Uploader.js +24 -10
- package/src/utils.js +9 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nm-logger/logger",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "Express JSON logger with S3 upload, correlation IDs, and separate success/error/external daily logs.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@aws-sdk/client-s3": "^3.600.0",
|
|
25
|
-
"fs-extra": "^11.1.1"
|
|
25
|
+
"fs-extra": "^11.1.1",
|
|
26
|
+
"@nm-logger/logger": "^1.1.8"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"express": ">=4.0.0"
|
package/src/DailyWatcher.js
CHANGED
|
@@ -40,16 +40,42 @@ class DailyWatcher {
|
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
this.queue.add(async () => {
|
|
44
|
+
try {
|
|
45
|
+
const now = new Date();
|
|
46
|
+
const key = `${Y}/${M}/${D}/${fileName}`;
|
|
47
|
+
|
|
48
|
+
console.log(`📤 Uploading [${category}] logs to S3: ${key}`);
|
|
49
|
+
|
|
50
|
+
// Step 1 — Read local file
|
|
51
|
+
const localContent = JSON.parse(await fs.readFile(file, "utf8"));
|
|
52
|
+
|
|
53
|
+
// Step 2 — Try loading existing file from S3
|
|
54
|
+
let existing = { logs: [] };
|
|
55
|
+
try {
|
|
56
|
+
const s3Content = await this.s3Uploader.getObject(key);
|
|
57
|
+
if (s3Content) {
|
|
58
|
+
existing = JSON.parse(s3Content);
|
|
59
|
+
}
|
|
60
|
+
} catch (err) {
|
|
61
|
+
// File may not exist on first upload — ignore
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Step 3 — Append logs
|
|
65
|
+
const merged = {
|
|
66
|
+
logs: [...existing.logs, ...localContent.logs]
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Step 4 — Upload merged logs
|
|
70
|
+
await this.s3Uploader.putObject(key, JSON.stringify(merged, null, 2));
|
|
71
|
+
|
|
72
|
+
console.log("✅ Logs appended to S3 successfully");
|
|
73
|
+
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error(`❌ Error uploading logs to S3:`, err);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
53
79
|
});
|
|
54
80
|
}, this.intervalMs);
|
|
55
81
|
}
|
package/src/S3Uploader.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const {
|
|
3
3
|
S3Client,
|
|
4
|
-
PutObjectCommand
|
|
4
|
+
PutObjectCommand,
|
|
5
|
+
GetObjectCommand
|
|
5
6
|
} = require("@aws-sdk/client-s3");
|
|
7
|
+
const { streamToString } = require("./utils");
|
|
6
8
|
|
|
7
9
|
class S3Uploader {
|
|
8
10
|
constructor(config) {
|
|
@@ -17,16 +19,28 @@ class S3Uploader {
|
|
|
17
19
|
this.bucket = config.bucket;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
async getObject(key) {
|
|
23
|
+
try {
|
|
24
|
+
const result = await this.client.send(
|
|
25
|
+
new GetObjectCommand({
|
|
26
|
+
Bucket: this.bucket,
|
|
27
|
+
Key: key
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
return await streamToString(result.Body);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
return null; // file not found
|
|
33
|
+
}
|
|
34
|
+
}
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
async putObject(key, body) {
|
|
37
|
+
await this.client.send(
|
|
38
|
+
new PutObjectCommand({
|
|
39
|
+
Bucket: this.bucket,
|
|
40
|
+
Key: key,
|
|
41
|
+
Body: body
|
|
42
|
+
})
|
|
43
|
+
);
|
|
30
44
|
}
|
|
31
45
|
}
|
|
32
46
|
|
package/src/utils.js
CHANGED
|
@@ -89,3 +89,12 @@ exports.generateCorrelationId = () => {
|
|
|
89
89
|
const ts = Date.now().toString(16);
|
|
90
90
|
return `cid-${rand}-${ts}`;
|
|
91
91
|
};
|
|
92
|
+
|
|
93
|
+
exports.streamToString = async (stream) => {
|
|
94
|
+
return await new Promise((resolve, reject) => {
|
|
95
|
+
const chunks = [];
|
|
96
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
97
|
+
stream.on("error", reject);
|
|
98
|
+
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
99
|
+
});
|
|
100
|
+
};
|