@anhocva214/logzen 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
- * Đường dẫn đến file log nơi các log sẽ được ghi.
5
- * Mặc định 'logzen.log' tại thư mục hiện hành.
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 Cấu hình cho middleware
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,36 @@ const fs = __importStar(require("fs"));
28
28
  const path = __importStar(require("path"));
29
29
  /**
30
30
  * LogZen middleware
31
- * @param options Cấu hình cho middleware
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
- // Tạo stream ghi log chế độ append
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
- // Khai báo mảng chứa các phần của response
41
+ // Array to hold chunks of response data.
42
42
  const chunks = [];
43
- // Lưu lại tham chiếu của các hàm gốc
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
- // Ghi đè res.write để lưu lại các chunk dữ liệu của response
46
+ // Override res.write to capture response chunks.
47
47
  res.write = ((chunk, ...args) => {
48
- // chunk thể string hoặc Buffer
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
- // Ghi đè res.end để hoàn thiện quá trình logging khi response kết thúc
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
- const responseBody = Buffer.concat(chunks).toString("utf8");
57
+ let responseBody = Buffer.concat(chunks).toString("utf8");
58
+ if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
59
+ responseBody = "<HTML>";
60
+ }
58
61
  const duration = Date.now() - startTime;
59
62
  const logEntry = {
60
63
  time: new Date().toISOString(),
@@ -63,7 +66,7 @@ function logzen(options = {}) {
63
66
  method,
64
67
  url: originalUrl,
65
68
  headers,
66
- // Lưu ý: req.body sẽ giá trị nếu middleware body-parser đã được gọi trước
69
+ // Note: req.body will have a value if body-parser middleware has been applied.
67
70
  body: req.body || null,
68
71
  },
69
72
  response: {
@@ -71,7 +74,7 @@ function logzen(options = {}) {
71
74
  body: responseBody,
72
75
  },
73
76
  };
74
- // Ghi log dưới dạng JSON
77
+ // Write the log entry as JSON.
75
78
  logStream.write(JSON.stringify(logEntry) + "\n");
76
79
  return originalEnd(chunk, ...args);
77
80
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anhocva214/logzen",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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,62 @@ import * as path from "path";
4
4
 
5
5
  export interface LogZenOptions {
6
6
  /**
7
- * Đường dẫn đến file log nơi các log sẽ được ghi.
8
- * Mặc định 'logzen.log' tại thư mục hiện hành.
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 Cấu hình cho middleware
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
- // Tạo stream ghi log chế độ append
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
- // Khai báo mảng chứa các phần của response
28
+ // Array to hold chunks of response data.
29
29
  const chunks: Buffer[] = [];
30
30
 
31
- // Lưu lại tham chiếu của các hàm gốc
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
- // Ghi đè res.write để lưu lại các chunk dữ liệu của response
35
+ // Override res.write to capture response chunks.
36
36
  res.write = ((chunk: any, ...args: any[]): boolean => {
37
- // chunk thể string hoặc Buffer
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
- // Ghi đè res.end để hoàn thiện quá trình logging khi response kết thúc
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
- const responseBody = Buffer.concat(chunks).toString("utf8");
47
+ let responseBody = Buffer.concat(chunks).toString("utf8");
48
+
49
+ if (/^\s*(<!doctype\s+html|<html)/i.test(responseBody)) {
50
+ responseBody = "<HTML>";
51
+ }
52
+
48
53
  const duration = Date.now() - startTime;
49
54
 
50
55
  const logEntry = {
51
56
  time: new Date().toISOString(),
52
- duration, // Thời gian xử lý request (ms)
57
+ duration, // Request processing time (ms)
53
58
  request: {
54
59
  method,
55
60
  url: originalUrl,
56
61
  headers,
57
- // Lưu ý: req.body sẽ giá trị nếu middleware body-parser đã được gọi trước
62
+ // Note: req.body will have a value if body-parser middleware has been applied.
58
63
  body: req.body || null,
59
64
  },
60
65
  response: {
@@ -63,7 +68,7 @@ export function logzen(options: LogZenOptions = {}) {
63
68
  },
64
69
  };
65
70
 
66
- // Ghi log dưới dạng JSON
71
+ // Write the log entry as JSON.
67
72
  logStream.write(JSON.stringify(logEntry) + "\n");
68
73
 
69
74
  return originalEnd(chunk, ...args);
@@ -73,4 +78,4 @@ export function logzen(options: LogZenOptions = {}) {
73
78
  };
74
79
  }
75
80
 
76
- export default logzen;
81
+ export default logzen;