@nm-logger/logger 1.1.5 → 1.1.7
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/README.md +233 -6
- package/package.json +2 -2
- package/src/Logger.js +24 -21
- package/src/S3Uploader.js +18 -12
package/README.md
CHANGED
|
@@ -1,14 +1,241 @@
|
|
|
1
1
|
# @nm-logger/logger
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Daily JSON logger for Node.js / Express APIs with:
|
|
5
4
|
- Per-request logging (success, error, external API)
|
|
6
5
|
- Separate daily files:
|
|
7
6
|
- `daily_logs_success.json`
|
|
8
7
|
- `daily_logs_error.json`
|
|
9
8
|
- `daily_logs_external.json`
|
|
10
|
-
-
|
|
11
|
-
- Correlation IDs (`X-Correlation-ID`)
|
|
12
|
-
-
|
|
9
|
+
- S3 upload + queue + daily rotation
|
|
10
|
+
- Correlation IDs (with `X-Correlation-ID` header)
|
|
11
|
+
- External API logging (Axios)
|
|
12
|
+
- Sensitive field masking (password, token, otp, etc.)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
After you publish the package to npm:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @nm-logger/logger
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Log Format
|
|
27
|
+
|
|
28
|
+
Each log line in `daily_logs.json` is a JSON object:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"url": "/api/v1/attendance/get",
|
|
33
|
+
"body": "{\"month\":\"2025-12\"}",
|
|
34
|
+
"params": "{\"params\":{},\"query\":{}}",
|
|
35
|
+
"type": "get",
|
|
36
|
+
"error": "",
|
|
37
|
+
"date": "2025-12-05 12:24:00",
|
|
38
|
+
"employee_id": "TAKK122",
|
|
39
|
+
"correlation_id": "cid-abcd1234-17f5d3c9a"
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Fields:
|
|
44
|
+
|
|
45
|
+
- `url` – `req.originalUrl`
|
|
46
|
+
- `body` – stringified (and masked) `req.body`
|
|
47
|
+
- `params` – stringified (and masked) object `{ params: req.params, query: req.query }`
|
|
48
|
+
- `type` – last segment of the URL (e.g. `/api/v1/attendance/get` → `"get"`)
|
|
49
|
+
- `error` – error message if any
|
|
50
|
+
- `date` – `YYYY-MM-DD HH:mm:ss`
|
|
51
|
+
- `employee_id` – from argument or `req.user.employee_id / emp_code / id`
|
|
52
|
+
- `correlation_id` – unique per request chain (also added as `X-Correlation-ID` header)
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
|
|
58
|
+
### 1. Create the logger
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const Logger = require("@nm-logger/logger");
|
|
62
|
+
|
|
63
|
+
const logger = new Logger(
|
|
64
|
+
{
|
|
65
|
+
accessKeyId: process.env.AWS_KEY,
|
|
66
|
+
secretAccessKey: process.env.AWS_SECRET,
|
|
67
|
+
region: "ap-south-1",
|
|
68
|
+
bucket: "your-log-bucket-name"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
baseDir: "logs", // optional, default "logs"
|
|
72
|
+
watchIntervalMs: 60000, // optional, default 60s
|
|
73
|
+
maskFields: ["aadhaar", "panNumber"] // extra fields to mask
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 2. Log every request + set correlation ID header
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
app.use(logger.requestLoggerMiddleware());
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 3. Log errors via Express error middleware
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
// your routes above...
|
|
88
|
+
|
|
89
|
+
app.use(logger.expressMiddleware()); // or logger.expressErrorMiddleware()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 4. Manual logging in routes
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
app.post("/api/v1/attendance/get", async (req, res) => {
|
|
96
|
+
try {
|
|
97
|
+
// ... your logic, external APIs etc ...
|
|
98
|
+
|
|
99
|
+
await logger.logRequest(req, req.user?.employee_id);
|
|
100
|
+
res.json({ success: true });
|
|
101
|
+
} catch (err) {
|
|
102
|
+
await logger.logError(err, req, req.user?.employee_id);
|
|
103
|
+
res.status(500).json({ error: err.message });
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## External API logging with Axios
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
const axios = require("axios");
|
|
114
|
+
|
|
115
|
+
// Attach once at startup
|
|
116
|
+
logger.attachAxiosLogger(axios);
|
|
117
|
+
|
|
118
|
+
app.get("/api/v1/some-data", async (req, res) => {
|
|
119
|
+
try {
|
|
120
|
+
const response = await axios.get("https://api.example.com/data", {
|
|
121
|
+
headers: {
|
|
122
|
+
"X-Correlation-ID": req.correlationId, // propagated
|
|
123
|
+
"X-Employee-ID": req.user?.employee_id || "" // optional
|
|
124
|
+
},
|
|
125
|
+
params: {
|
|
126
|
+
id: 123
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
res.json(response.data);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
await logger.logError(err, req, req.user?.employee_id);
|
|
133
|
+
res.status(500).json({ error: err.message });
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
This will produce external log lines like:
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"url": "https://api.example.com/data",
|
|
143
|
+
"body": "{}",
|
|
144
|
+
"params": "{\"id\":123}",
|
|
145
|
+
"type": "external_api",
|
|
146
|
+
"error": "",
|
|
147
|
+
"date": "2025-12-05 12:24:00",
|
|
148
|
+
"employee_id": "TAKK122",
|
|
149
|
+
"correlation_id": "cid-abcd1234-17f5d3c9a"
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Sensitive Data Masking
|
|
156
|
+
|
|
157
|
+
Built-in masked keys (case-insensitive, partial match):
|
|
158
|
+
|
|
159
|
+
- password, pass
|
|
160
|
+
- token, secret
|
|
161
|
+
- otp
|
|
162
|
+
- auth, authorization
|
|
163
|
+
- apiKey, api_key
|
|
164
|
+
- session
|
|
165
|
+
- ssn
|
|
166
|
+
|
|
167
|
+
Plus anything you pass in `maskFields` option.
|
|
168
|
+
|
|
169
|
+
Any object like:
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{
|
|
173
|
+
"password": "MyPass123",
|
|
174
|
+
"otp": "111222",
|
|
175
|
+
"aadhaar": "9999-8888-7777",
|
|
176
|
+
"email": "user@example.com"
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
will be logged as:
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"password": "*****",
|
|
185
|
+
"otp": "*****",
|
|
186
|
+
"aadhaar": "*****",
|
|
187
|
+
"email": "user@example.com"
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## S3 Upload Behavior
|
|
194
|
+
|
|
195
|
+
- Logs are stored locally under:
|
|
196
|
+
- `logs/YYYY/MM/DD/daily_logs.json`
|
|
197
|
+
- A watcher runs every `watchIntervalMs` (default 60 seconds)
|
|
198
|
+
- When the date changes (e.g., from `2025-12-05` to `2025-12-06`),
|
|
199
|
+
- The logger uploads the **previous day's** log file to S3:
|
|
200
|
+
|
|
201
|
+
Example S3 key:
|
|
202
|
+
|
|
203
|
+
```txt
|
|
204
|
+
2025/12/05/daily_logs.json
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
So final S3 path:
|
|
208
|
+
|
|
209
|
+
```txt
|
|
210
|
+
s3://<bucket>/<year>/<month>/<day>/daily_logs.json
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## TypeScript Usage
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import Logger, { S3Config, LoggerOptions } from "@ve/logger";
|
|
219
|
+
|
|
220
|
+
const s3config: S3Config = {
|
|
221
|
+
accessKeyId: process.env.AWS_KEY!,
|
|
222
|
+
secretAccessKey: process.env.AWS_SECRET!,
|
|
223
|
+
region: "ap-south-1",
|
|
224
|
+
bucket: "your-log-bucket"
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const options: LoggerOptions = {
|
|
228
|
+
baseDir: "logs",
|
|
229
|
+
watchIntervalMs: 60000,
|
|
230
|
+
maskFields: ["aadhaar", "pan"]
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const logger = new Logger(s3config, options);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|
|
13
241
|
|
|
14
|
-
See source comments for usage.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nm-logger/logger",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
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",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"author": "nm-logger",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"aws-sdk": "^
|
|
24
|
+
"@aws-sdk/client-s3": "^3.600.0",
|
|
25
25
|
"fs-extra": "^11.1.1"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
package/src/Logger.js
CHANGED
|
@@ -140,31 +140,34 @@ class Logger {
|
|
|
140
140
|
return this.expressMiddleware();
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
143
|
+
requestLoggerMiddleware() {
|
|
144
|
+
return (req, res, next) => {
|
|
145
|
+
try {
|
|
146
|
+
// Prevent duplicate logging
|
|
147
|
+
if (req.__nm_logger_logged) {
|
|
148
|
+
return next();
|
|
149
|
+
}
|
|
150
|
+
req.__nm_logger_logged = true;
|
|
150
151
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
let correlationId =
|
|
153
|
+
req.headers["x-correlation-id"] ||
|
|
154
|
+
req.headers["X-Correlation-ID"] ||
|
|
155
|
+
req.correlationId ||
|
|
156
|
+
generateCorrelationId();
|
|
154
157
|
|
|
155
|
-
|
|
156
|
-
|
|
158
|
+
req.correlationId = correlationId;
|
|
159
|
+
res.setHeader("X-Correlation-ID", correlationId);
|
|
157
160
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
this.logRequest(req).catch((e) =>
|
|
162
|
+
console.error("Logger requestLoggerMiddleware error:", e)
|
|
163
|
+
);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
console.error("Logger requestLoggerMiddleware outer error:", e);
|
|
166
|
+
}
|
|
164
167
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
+
next();
|
|
169
|
+
};
|
|
170
|
+
}
|
|
168
171
|
|
|
169
172
|
attachAxiosLogger(axiosInstance) {
|
|
170
173
|
if (!axiosInstance || !axiosInstance.interceptors) {
|
package/src/S3Uploader.js
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
|
-
const
|
|
2
|
+
const {
|
|
3
|
+
S3Client,
|
|
4
|
+
PutObjectCommand
|
|
5
|
+
} = require("@aws-sdk/client-s3");
|
|
3
6
|
|
|
4
7
|
class S3Uploader {
|
|
5
8
|
constructor(config) {
|
|
6
|
-
this.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
this.client = new S3Client({
|
|
10
|
+
region: config.region,
|
|
11
|
+
credentials: {
|
|
12
|
+
accessKeyId: config.accessKeyId,
|
|
13
|
+
secretAccessKey: config.secretAccessKey
|
|
14
|
+
}
|
|
10
15
|
});
|
|
16
|
+
|
|
11
17
|
this.bucket = config.bucket;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
async upload(filePath, s3Key) {
|
|
15
21
|
const fileData = fs.readFileSync(filePath);
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const command = new PutObjectCommand({
|
|
24
|
+
Bucket: this.bucket,
|
|
25
|
+
Key: s3Key,
|
|
26
|
+
Body: fileData
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
await this.client.send(command);
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
32
|
|