70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
import fs from 'fs';
 | 
						|
import moment from "moment";
 | 
						|
 | 
						|
 | 
						|
// const shortenStringsInObject = (obj, maxLength) => {
 | 
						|
//     // Kiểm tra nếu obj không phải là một đối tượng hoặc maxLength không phải là một số
 | 
						|
//     if (typeof obj !== 'object' || typeof maxLength !== 'number') {
 | 
						|
//       return obj;
 | 
						|
//     }
 | 
						|
  
 | 
						|
//     // Duyệt qua các thuộc tính của đối tượng
 | 
						|
//     for (const key in obj) {
 | 
						|
//       if (obj.hasOwnProperty(key)) {
 | 
						|
//         // Kiểm tra nếu thuộc tính là một chuỗi và độ dài của chuỗi lớn hơn maxLength
 | 
						|
//         if (typeof obj[key] === 'string' && obj[key].length > maxLength) {
 | 
						|
//           // Rút ngắn chuỗi lại maxLength ký tự
 | 
						|
//           obj[key] = obj[key].substring(0, maxLength);
 | 
						|
//         }
 | 
						|
//       }
 | 
						|
//     }
 | 
						|
  
 | 
						|
 | 
						|
//     if (Array.isArray(obj) && obj.length > 10) {
 | 
						|
//         // Rút ngắn chuỗi lại maxLength ký tự
 | 
						|
//         obj = "[Array:"+obj.length+"]\n"+obj.slice(0,15)
 | 
						|
//       }
 | 
						|
 | 
						|
//     return obj;
 | 
						|
//   }
 | 
						|
 | 
						|
export function loggerAPI(req, res, location) {
 | 
						|
    let old_data = "";
 | 
						|
    let resBody = res.getBody()
 | 
						|
    // let body = shortenStringsInObject(resBody, 300)
 | 
						|
    try {
 | 
						|
      const fileName =
 | 
						|
        "./app/store/logsAPI/" +
 | 
						|
        moment(Date.now()).format("DD_MM_YYYY").toString() +
 | 
						|
        ".log";
 | 
						|
  
 | 
						|
      let logInfo =
 | 
						|
        "\n\n[" +
 | 
						|
        moment(Date.now()).format("D/M/YY-HH:mm:ss") +
 | 
						|
        "] - " +
 | 
						|
        res.getStatus() +
 | 
						|
        " - " +
 | 
						|
        location +
 | 
						|
        "\n------PAYLOAD------\n" +
 | 
						|
        JSON.stringify(req.all(), null, 2) +
 | 
						|
        "\n------PARAMS-------\n" +
 | 
						|
        JSON.stringify(req.params(), null, 2) +
 | 
						|
        "\n-----RESPONSE------\n" +
 | 
						|
        JSON.stringify(resBody, null, 2) +
 | 
						|
        "\n------HEADERS------\n" +
 | 
						|
        JSON.stringify(req.headers(), null, 2) +
 | 
						|
        "\n\n======================================================================";
 | 
						|
  
 | 
						|
      fs.exists(fileName, async (exists) => {
 | 
						|
        if (exists) {
 | 
						|
          old_data = await fs.readFileSync(fileName, "utf8");
 | 
						|
          fs.writeFileSync(fileName, old_data + logInfo);
 | 
						|
        } else {
 | 
						|
          fs.writeFileSync(fileName, old_data + logInfo);
 | 
						|
        }
 | 
						|
      });
 | 
						|
  
 | 
						|
    } catch (error) {
 | 
						|
      res.status(500).send("CAN'T WRITE LOG");
 | 
						|
    }
 | 
						|
  } |