From 7534287f7dc3824ff46476da2a0c7b9289ce0393 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期一, 23 十二月 2024 13:32:02 +0800
Subject: [PATCH] 加入更新日志

---
 app.js           |   13 +++++++++----
 package.json     |    2 ++
 thirdPartyApi.js |    6 ++++++
 logger.js        |   20 ++++++++++++++++++++
 4 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/app.js b/app.js
index 8bcb91f..c4ed47f 100644
--- a/app.js
+++ b/app.js
@@ -4,7 +4,7 @@
 const fs = require('fs').promises;
 const fs2=require('fs')
 const path = require('path');
-const moment = require('moment')
+const logger = require('./logger'); // 根据实际情况调整路径
 const axios = require('axios');
 // 转成能识别的url
 const ViapiUtil = require('@alicloud/viapi-utils');
@@ -77,6 +77,7 @@
   });
   app.get('/test', async (req, res) => {
     const token=await thirdPartyApi.getAccessToken()
+    logger.info('返回token=='+JSON.stringify(token))
     res.json(token);
   });
 // 定义 /webuser 路由处理器  用患者code 换信息
@@ -87,12 +88,14 @@
     const deviceID = req.body.deviceID;
     const type = req.body.type;
     const xid = req.body.xid;
-    console.log(`action=${action},deviceID=${deviceID},type=${type},xid=${xid}`)
+    console.log()
+    logger.info(`req body action=${action},deviceID=${deviceID},type=${type},xid=${xid}`)
     if (action === 'doWebUser' && deviceID && type !== undefined) {
         try {
             const pathParams={code:xid}
             const data = await thirdPartyApi.fetchThirdPartyData('/patient/info/detail2', 'POST', {}, null,  pathParams,{});
-            console.log('--------------获取到了患者信息',data.data)
+            logger.info(`get patInfo data=${JSON.stringify(data)}`)
+            console.log('get patInfo ----',data.data)
             // console.log(data)
             if(data?.code===200&&data?.message==='SUCCESS'&&data?.data){
                 const user = data.data;
@@ -108,7 +111,8 @@
                     },
                     remsg: "success"
                 };
-                console.log('返回患者信息成功')
+                logger.info('Successfully returned patient information')
+                console.log('Successfully returned patient information')
             }else{
                 response = {
                     retcode: 1001,
@@ -119,6 +123,7 @@
 
         } catch (err) {
             console.error(err);
+            logger.error(err)
             res.status(500).json({ retCode: "-1", msg: err });
         }
     } else {
diff --git a/logger.js b/logger.js
new file mode 100644
index 0000000..1a29655
--- /dev/null
+++ b/logger.js
@@ -0,0 +1,20 @@
+const pino = require('pino');
+const pretty = require('pino-pretty');
+
+// 创建日志记录器
+const logger = pino({
+  level: 'info',
+  transport: {
+    target: 'pino-pretty', // 可选:美化输出
+    options: {
+      translateTime: 'SYS:dd-mm-yyyy HH:MM:ss',
+      ignore: 'pid,hostname'
+    }
+  }
+});
+
+// 或者简单地创建一个基础日志记录器
+// const logger = pino();
+
+// 将 logger 导出以便其他模块使用
+module.exports = logger;
\ No newline at end of file
diff --git a/package.json b/package.json
index f80bd86..f83758e 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,8 @@
     "express": "^4.21.1",
     "moment": "^2.30.1",
     "mysql2": "^3.11.3",
+    "pino": "^9.5.0",
+    "pino-pretty": "^13.0.0",
     "pkg": "^5.8.1"
   },
   "devDependencies": {
diff --git a/thirdPartyApi.js b/thirdPartyApi.js
index 7aa289f..b508184 100644
--- a/thirdPartyApi.js
+++ b/thirdPartyApi.js
@@ -1,5 +1,7 @@
 // thirdPartyApi.js
 const axios = require('axios');
+
+const logger = require('./logger'); // 根据实际情况调整路径
 // BASIC_API = 'https://hemobs.icoldchain.cn/'
 const BASIC_API = 'http://testbs.ihemodialysis.com/'
 let cachedToken = null;
@@ -28,11 +30,13 @@
 
 // 获取缓存或新的 Token
 async function getCachedOrNewToken() {
+  logger.log('Check if the token exists')
   if (cachedToken && tokenExpiresAt > Date.now()) {
     return cachedToken;
   }
 
   // 如果 token 已经过期或不存在,则重新获取
+  logger.log('If the token has expired or does not exist, retrieve it again')
   const { data, expires_in } = await getAccessToken();
   cachedToken = data;
   tokenExpiresAt = Date.now() + (expires_in - 60) * 1000; // 提前 60 秒刷新 token
@@ -73,6 +77,7 @@
 async function fetchThirdPartyData(url, method = 'GET', headers = {}, data = null, queryParams = {}, pathParams = {}) {
   try {
     const token = await getCachedOrNewToken();
+    logger.info(`get token====${token}`)
     console.log('得到了token------------------------',token)
     const fullUrl = buildUrl(url, pathParams);
 
@@ -89,6 +94,7 @@
 
     return response.data;
   } catch (error) {
+    logger.error('Error fetching third-party data:', error.message)
     console.error('Error fetching third-party data:', error.message);
     throw error;
   }

--
Gitblit v1.8.0