diff --git a/server/model/monitor.js b/server/model/monitor.js index a1aa1656d..fc3292317 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 324ae6779..fe0d2e66e 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 7be922dd5..2ed6889b4 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 (_) { } +};