From d7f7dba13ff326d0b236ce539418b2de469c52ff Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 29 Oct 2021 18:24:47 +0800 Subject: [PATCH] write some error logs to ./data/error.log --- server/model/monitor.js | 3 ++- server/server.js | 3 ++- server/util-server.js | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index a1aa1656..fc329231 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -7,7 +7,7 @@ dayjs.extend(timezone); const axios = require("axios"); const { Prometheus } = require("../prometheus"); const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); -const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting } = require("../util-server"); +const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); const { Notification } = require("../notification"); @@ -404,6 +404,7 @@ class Monitor extends BeanModel { await beat(); } catch (e) { console.trace(e); + errorLog(e, false); console.error("Please report to https://github.com/louislam/uptime-kuma/issues"); if (! this.isStop) { diff --git a/server/server.js b/server/server.js index 324ae677..fe0d2e66 100644 --- a/server/server.js +++ b/server/server.js @@ -41,7 +41,7 @@ console.log("Importing this project modules"); debug("Importing Monitor"); const Monitor = require("./model/monitor"); debug("Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, errorLog } = require("./util-server"); debug("Importing Notification"); const { Notification } = require("./notification"); @@ -1529,5 +1529,6 @@ gracefulShutdown(server, { // Catch unexpected errors here process.addListener("unhandledRejection", (error, promise) => { console.trace(error); + errorLog(error, false); console.error("If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues"); }); diff --git a/server/util-server.js b/server/util-server.js index 7be922dd..2ed6889b 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -8,6 +8,8 @@ const { Resolver } = require("dns"); const child_process = require("child_process"); const iconv = require("iconv-lite"); const chardet = require("chardet"); +const fs = require("fs"); +const nodeJsUtil = require("util"); // From ping-lite exports.WIN = /^win/.test(process.platform); @@ -332,3 +334,18 @@ exports.convertToUTF8 = (body) => { const str = iconv.decode(body, guessEncoding); return str.toString(); }; + +let logFile = fs.createWriteStream("./data/error.log", { + flags: "a" +}); + +exports.errorLog = (error, outputToConsole = true) => { + try { + const dateTime = R.isoDateTime(); + logFile.write(`[${dateTime}] ` + nodeJsUtil.format(error) + "\n"); + + if (outputToConsole) { + console.error(error); + } + } catch (_) { } +};