@anhocva214/logzen 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +4 -4
- package/dist/index.js +15 -11
- package/package.json +1 -1
- package/src/index.ts +21 -15
package/dist/index.d.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
2
2
|
export interface LogZenOptions {
|
3
3
|
/**
|
4
|
-
*
|
5
|
-
*
|
4
|
+
* Path to the log file where logs will be written.
|
5
|
+
* Default is 'logzen.log' in the current working directory.
|
6
6
|
*/
|
7
7
|
filePath?: string;
|
8
8
|
}
|
9
9
|
/**
|
10
10
|
* LogZen middleware
|
11
|
-
* @param options
|
12
|
-
* @returns Express middleware function
|
11
|
+
* @param options Middleware configuration options.
|
12
|
+
* @returns An Express middleware function.
|
13
13
|
*/
|
14
14
|
export declare function logzen(options?: LogZenOptions): (req: Request, res: Response, next: NextFunction) => void;
|
15
15
|
export default logzen;
|
package/dist/index.js
CHANGED
@@ -28,33 +28,37 @@ const fs = __importStar(require("fs"));
|
|
28
28
|
const path = __importStar(require("path"));
|
29
29
|
/**
|
30
30
|
* LogZen middleware
|
31
|
-
* @param options
|
32
|
-
* @returns Express middleware function
|
31
|
+
* @param options Middleware configuration options.
|
32
|
+
* @returns An Express middleware function.
|
33
33
|
*/
|
34
34
|
function logzen(options = {}) {
|
35
35
|
const filePath = options.filePath || path.join(process.cwd(), "logzen.log");
|
36
|
-
//
|
36
|
+
// Create a write stream in append mode.
|
37
37
|
const logStream = fs.createWriteStream(filePath, { flags: "a" });
|
38
38
|
return (req, res, next) => {
|
39
39
|
const startTime = Date.now();
|
40
40
|
const { method, originalUrl, headers } = req;
|
41
|
-
//
|
41
|
+
// Array to hold chunks of response data.
|
42
42
|
const chunks = [];
|
43
|
-
//
|
43
|
+
// Save references to the original res.write and res.end functions.
|
44
44
|
const originalWrite = res.write.bind(res);
|
45
45
|
const originalEnd = res.end.bind(res);
|
46
|
-
//
|
46
|
+
// Override res.write to capture response chunks.
|
47
47
|
res.write = ((chunk, ...args) => {
|
48
|
-
// chunk
|
48
|
+
// chunk can be a string or Buffer.
|
49
49
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
50
50
|
return originalWrite(chunk, ...args);
|
51
51
|
});
|
52
|
-
//
|
52
|
+
// Override res.end to finalize logging when response is finished.
|
53
53
|
res.end = ((chunk, ...args) => {
|
54
54
|
if (chunk) {
|
55
55
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
56
56
|
}
|
57
|
-
|
57
|
+
let responseBody = Buffer.concat(chunks).toString("utf8");
|
58
|
+
// If the response body appears to be HTML, set it to "<HTML>"
|
59
|
+
if (/^\s*<\s*html/i.test(responseBody)) {
|
60
|
+
responseBody = "<HTML>";
|
61
|
+
}
|
58
62
|
const duration = Date.now() - startTime;
|
59
63
|
const logEntry = {
|
60
64
|
time: new Date().toISOString(),
|
@@ -63,7 +67,7 @@ function logzen(options = {}) {
|
|
63
67
|
method,
|
64
68
|
url: originalUrl,
|
65
69
|
headers,
|
66
|
-
//
|
70
|
+
// Note: req.body will have a value if body-parser middleware has been applied.
|
67
71
|
body: req.body || null,
|
68
72
|
},
|
69
73
|
response: {
|
@@ -71,7 +75,7 @@ function logzen(options = {}) {
|
|
71
75
|
body: responseBody,
|
72
76
|
},
|
73
77
|
};
|
74
|
-
//
|
78
|
+
// Write the log entry as JSON.
|
75
79
|
logStream.write(JSON.stringify(logEntry) + "\n");
|
76
80
|
return originalEnd(chunk, ...args);
|
77
81
|
});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@anhocva214/logzen",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.2",
|
4
4
|
"description": "A lightweight logging middleware for Node.js/Express written in TypeScript that logs request and response details in JSON format.",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/index.ts
CHANGED
@@ -4,57 +4,63 @@ import * as path from "path";
|
|
4
4
|
|
5
5
|
export interface LogZenOptions {
|
6
6
|
/**
|
7
|
-
*
|
8
|
-
*
|
7
|
+
* Path to the log file where logs will be written.
|
8
|
+
* Default is 'logzen.log' in the current working directory.
|
9
9
|
*/
|
10
10
|
filePath?: string;
|
11
11
|
}
|
12
12
|
|
13
13
|
/**
|
14
14
|
* LogZen middleware
|
15
|
-
* @param options
|
16
|
-
* @returns Express middleware function
|
15
|
+
* @param options Middleware configuration options.
|
16
|
+
* @returns An Express middleware function.
|
17
17
|
*/
|
18
18
|
export function logzen(options: LogZenOptions = {}) {
|
19
19
|
const filePath = options.filePath || path.join(process.cwd(), "logzen.log");
|
20
20
|
|
21
|
-
//
|
21
|
+
// Create a write stream in append mode.
|
22
22
|
const logStream = fs.createWriteStream(filePath, { flags: "a" });
|
23
23
|
|
24
24
|
return (req: Request, res: Response, next: NextFunction): void => {
|
25
25
|
const startTime = Date.now();
|
26
26
|
const { method, originalUrl, headers } = req;
|
27
27
|
|
28
|
-
//
|
28
|
+
// Array to hold chunks of response data.
|
29
29
|
const chunks: Buffer[] = [];
|
30
30
|
|
31
|
-
//
|
31
|
+
// Save references to the original res.write and res.end functions.
|
32
32
|
const originalWrite = res.write.bind(res);
|
33
33
|
const originalEnd = res.end.bind(res);
|
34
34
|
|
35
|
-
//
|
35
|
+
// Override res.write to capture response chunks.
|
36
36
|
res.write = ((chunk: any, ...args: any[]): boolean => {
|
37
|
-
// chunk
|
37
|
+
// chunk can be a string or Buffer.
|
38
38
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
39
39
|
return originalWrite(chunk, ...args);
|
40
40
|
}) as typeof res.write;
|
41
41
|
|
42
|
-
//
|
42
|
+
// Override res.end to finalize logging when response is finished.
|
43
43
|
res.end = ((chunk?: any, ...args: any[]): Response => {
|
44
44
|
if (chunk) {
|
45
45
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
46
46
|
}
|
47
|
-
|
47
|
+
let responseBody = Buffer.concat(chunks).toString("utf8");
|
48
|
+
|
49
|
+
// If the response body appears to be HTML, set it to "<HTML>"
|
50
|
+
if (/^\s*<\s*html/i.test(responseBody)) {
|
51
|
+
responseBody = "<HTML>";
|
52
|
+
}
|
53
|
+
|
48
54
|
const duration = Date.now() - startTime;
|
49
55
|
|
50
56
|
const logEntry = {
|
51
57
|
time: new Date().toISOString(),
|
52
|
-
duration, //
|
58
|
+
duration, // Request processing time (ms)
|
53
59
|
request: {
|
54
60
|
method,
|
55
61
|
url: originalUrl,
|
56
62
|
headers,
|
57
|
-
//
|
63
|
+
// Note: req.body will have a value if body-parser middleware has been applied.
|
58
64
|
body: req.body || null,
|
59
65
|
},
|
60
66
|
response: {
|
@@ -63,7 +69,7 @@ export function logzen(options: LogZenOptions = {}) {
|
|
63
69
|
},
|
64
70
|
};
|
65
71
|
|
66
|
-
//
|
72
|
+
// Write the log entry as JSON.
|
67
73
|
logStream.write(JSON.stringify(logEntry) + "\n");
|
68
74
|
|
69
75
|
return originalEnd(chunk, ...args);
|
@@ -73,4 +79,4 @@ export function logzen(options: LogZenOptions = {}) {
|
|
73
79
|
};
|
74
80
|
}
|
75
81
|
|
76
|
-
export default logzen;
|
82
|
+
export default logzen;
|