@anhocva214/logzen 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +13 -4
- package/package.json +1 -1
- package/src/index.ts +14 -5
package/dist/index.js
CHANGED
@@ -37,7 +37,7 @@ function logzen(options = {}) {
|
|
37
37
|
const logStream = fs.createWriteStream(filePath, { flags: "a" });
|
38
38
|
return (req, res, next) => {
|
39
39
|
const startTime = Date.now();
|
40
|
-
const { method, originalUrl, headers } = req;
|
40
|
+
const { method, originalUrl, headers, ip, httpVersion, query, params } = 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.
|
@@ -55,23 +55,32 @@ function logzen(options = {}) {
|
|
55
55
|
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
56
56
|
}
|
57
57
|
let responseBody = Buffer.concat(chunks).toString("utf8");
|
58
|
-
//
|
59
|
-
if (/^\s
|
58
|
+
// Improved check: if the response body is HTML (starts with DOCTYPE or <html), set it to "<HTML>"
|
59
|
+
if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
|
60
60
|
responseBody = "<HTML>";
|
61
61
|
}
|
62
62
|
const duration = Date.now() - startTime;
|
63
|
+
const endTime = Date.now();
|
63
64
|
const logEntry = {
|
64
|
-
time: new Date().toISOString(),
|
65
|
+
time: new Date(startTime).toISOString(),
|
66
|
+
startTime: new Date(startTime).toISOString(),
|
67
|
+
endTime: new Date(endTime).toISOString(),
|
65
68
|
duration,
|
66
69
|
request: {
|
67
70
|
method,
|
68
71
|
url: originalUrl,
|
72
|
+
httpVersion,
|
69
73
|
headers,
|
74
|
+
ip,
|
75
|
+
query,
|
76
|
+
params,
|
70
77
|
// Note: req.body will have a value if body-parser middleware has been applied.
|
71
78
|
body: req.body || null,
|
79
|
+
userAgent: headers["user-agent"] || "",
|
72
80
|
},
|
73
81
|
response: {
|
74
82
|
statusCode: res.statusCode,
|
83
|
+
headers: res.getHeaders ? res.getHeaders() : {},
|
75
84
|
body: responseBody,
|
76
85
|
},
|
77
86
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@anhocva214/logzen",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.4",
|
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
@@ -23,7 +23,7 @@ export function logzen(options: LogZenOptions = {}) {
|
|
23
23
|
|
24
24
|
return (req: Request, res: Response, next: NextFunction): void => {
|
25
25
|
const startTime = Date.now();
|
26
|
-
const { method, originalUrl, headers } = req;
|
26
|
+
const { method, originalUrl, headers, ip, httpVersion, query, params } = req;
|
27
27
|
|
28
28
|
// Array to hold chunks of response data.
|
29
29
|
const chunks: Buffer[] = [];
|
@@ -46,25 +46,34 @@ export function logzen(options: LogZenOptions = {}) {
|
|
46
46
|
}
|
47
47
|
let responseBody = Buffer.concat(chunks).toString("utf8");
|
48
48
|
|
49
|
-
//
|
50
|
-
if (/^\s
|
49
|
+
// Improved check: if the response body is HTML (starts with DOCTYPE or <html), set it to "<HTML>"
|
50
|
+
if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
|
51
51
|
responseBody = "<HTML>";
|
52
52
|
}
|
53
53
|
|
54
54
|
const duration = Date.now() - startTime;
|
55
|
+
const endTime = Date.now();
|
55
56
|
|
56
57
|
const logEntry = {
|
57
|
-
time: new Date().toISOString(),
|
58
|
+
time: new Date(startTime).toISOString(),
|
59
|
+
startTime: new Date(startTime).toISOString(),
|
60
|
+
endTime: new Date(endTime).toISOString(),
|
58
61
|
duration, // Request processing time (ms)
|
59
62
|
request: {
|
60
63
|
method,
|
61
64
|
url: originalUrl,
|
65
|
+
httpVersion,
|
62
66
|
headers,
|
67
|
+
ip,
|
68
|
+
query,
|
69
|
+
params,
|
63
70
|
// Note: req.body will have a value if body-parser middleware has been applied.
|
64
71
|
body: req.body || null,
|
72
|
+
userAgent: headers["user-agent"] || "",
|
65
73
|
},
|
66
74
|
response: {
|
67
75
|
statusCode: res.statusCode,
|
76
|
+
headers: res.getHeaders ? res.getHeaders() : {},
|
68
77
|
body: responseBody,
|
69
78
|
},
|
70
79
|
};
|
@@ -79,4 +88,4 @@ export function logzen(options: LogZenOptions = {}) {
|
|
79
88
|
};
|
80
89
|
}
|
81
90
|
|
82
|
-
export default logzen;
|
91
|
+
export default logzen;
|