@anhocva214/logzen 1.0.3 → 1.0.4
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/dist/index.js +12 -2
- package/package.json +1 -1
- package/src/index.ts +13 -3
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,22 +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
|
+
// Improved check: if the response body is HTML (starts with DOCTYPE or <html), set it to "<HTML>"
|
58
59
|
if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
|
59
60
|
responseBody = "<HTML>";
|
60
61
|
}
|
61
62
|
const duration = Date.now() - startTime;
|
63
|
+
const endTime = Date.now();
|
62
64
|
const logEntry = {
|
63
|
-
time: new Date().toISOString(),
|
65
|
+
time: new Date(startTime).toISOString(),
|
66
|
+
startTime: new Date(startTime).toISOString(),
|
67
|
+
endTime: new Date(endTime).toISOString(),
|
64
68
|
duration,
|
65
69
|
request: {
|
66
70
|
method,
|
67
71
|
url: originalUrl,
|
72
|
+
httpVersion,
|
68
73
|
headers,
|
74
|
+
ip,
|
75
|
+
query,
|
76
|
+
params,
|
69
77
|
// Note: req.body will have a value if body-parser middleware has been applied.
|
70
78
|
body: req.body || null,
|
79
|
+
userAgent: headers["user-agent"] || "",
|
71
80
|
},
|
72
81
|
response: {
|
73
82
|
statusCode: res.statusCode,
|
83
|
+
headers: res.getHeaders ? res.getHeaders() : {},
|
74
84
|
body: responseBody,
|
75
85
|
},
|
76
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,24 +46,34 @@ export function logzen(options: LogZenOptions = {}) {
|
|
46
46
|
}
|
47
47
|
let responseBody = Buffer.concat(chunks).toString("utf8");
|
48
48
|
|
49
|
+
// Improved check: if the response body is HTML (starts with DOCTYPE or <html), set it to "<HTML>"
|
49
50
|
if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
|
50
51
|
responseBody = "<HTML>";
|
51
52
|
}
|
52
53
|
|
53
54
|
const duration = Date.now() - startTime;
|
55
|
+
const endTime = Date.now();
|
54
56
|
|
55
57
|
const logEntry = {
|
56
|
-
time: new Date().toISOString(),
|
58
|
+
time: new Date(startTime).toISOString(),
|
59
|
+
startTime: new Date(startTime).toISOString(),
|
60
|
+
endTime: new Date(endTime).toISOString(),
|
57
61
|
duration, // Request processing time (ms)
|
58
62
|
request: {
|
59
63
|
method,
|
60
64
|
url: originalUrl,
|
65
|
+
httpVersion,
|
61
66
|
headers,
|
67
|
+
ip,
|
68
|
+
query,
|
69
|
+
params,
|
62
70
|
// Note: req.body will have a value if body-parser middleware has been applied.
|
63
71
|
body: req.body || null,
|
72
|
+
userAgent: headers["user-agent"] || "",
|
64
73
|
},
|
65
74
|
response: {
|
66
75
|
statusCode: res.statusCode,
|
76
|
+
headers: res.getHeaders ? res.getHeaders() : {},
|
67
77
|
body: responseBody,
|
68
78
|
},
|
69
79
|
};
|
@@ -78,4 +88,4 @@ export function logzen(options: LogZenOptions = {}) {
|
|
78
88
|
};
|
79
89
|
}
|
80
90
|
|
81
|
-
export default logzen;
|
91
|
+
export default logzen;
|