diff --git a/server/model/monitor.js b/server/model/monitor.js index e106f7ab3..f2d16524b 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 { log, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); -const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog, mqttAsync } = require("../util-server"); +const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mqttAsync } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); const { Notification } = require("../notification"); @@ -15,6 +15,7 @@ const { Proxy } = require("../proxy"); const { demoMode } = require("../config"); const version = require("../../package.json").version; const apicache = require("../modules/apicache"); +const { UptimeKumaServer } = require("../uptime-kuma-server"); /** * status: @@ -521,7 +522,7 @@ class Monitor extends BeanModel { await beat(); } catch (e) { console.trace(e); - errorLog(e, false); + UptimeKumaServer.errorLog(e, false); log.error("monitor", "Please report to https://github.com/louislam/uptime-kuma/issues"); if (! this.isStop) { diff --git a/server/server.js b/server/server.js index 11c59edbb..79cb21026 100644 --- a/server/server.js +++ b/server/server.js @@ -60,7 +60,7 @@ log.info("server", "Importing this project modules"); log.debug("server", "Importing Monitor"); const Monitor = require("./model/monitor"); log.debug("server", "Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, errorLog, doubleCheckPassword } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, doubleCheckPassword } = require("./util-server"); log.debug("server", "Importing Notification"); const { Notification } = require("./notification"); @@ -1694,6 +1694,6 @@ gracefulShutdown(server.httpServer, { // Catch unexpected errors here process.addListener("unhandledRejection", (error, promise) => { console.trace(error); - errorLog(error, false); + UptimeKumaServer.errorLog(error, false); console.error("If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues"); }); diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 1cc740646..d0c968e73 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -5,13 +5,14 @@ const http = require("http"); const { Server } = require("socket.io"); const { R } = require("redbean-node"); const { log } = require("../src/util"); +const Database = require("./database"); +const util = require("util"); /** * `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue. * @type {UptimeKumaServer} */ class UptimeKumaServer { - /** * * @type {UptimeKumaServer} @@ -83,6 +84,32 @@ class UptimeKumaServer { return result; } + + /** + * Write error to log file + * @param {any} error The error to write + * @param {boolean} outputToConsole Should the error also be output to console? + */ + static errorLog(error, outputToConsole = true) { + const errorLogStream = fs.createWriteStream(Database.dataDir + "/error.log", { + flags: "a" + }); + + errorLogStream.on("error", () => { + log.info("", "Cannot write to error.log"); + }); + + if (errorLogStream) { + const dateTime = R.isoDateTime(); + errorLogStream.write(`[${dateTime}] ` + util.format(error) + "\n"); + + if (outputToConsole) { + console.error(error); + } + } + + errorLogStream.end(); + } } module.exports = { diff --git a/server/util-server.js b/server/util-server.js index 39a2d9040..985143d51 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -7,8 +7,6 @@ const { Resolver } = require("dns"); const childProcess = require("child_process"); const iconv = require("iconv-lite"); const chardet = require("chardet"); -const fs = require("fs"); -const nodeJsUtil = require("util"); const mqtt = require("mqtt"); // From ping-lite @@ -206,7 +204,7 @@ exports.dnsResolve = function (hostname, resolverServer, rrtype) { /** * Retrieve value of setting based on key * @param {string} key Key of setting to retrieve - * @returns {Promise} Object representation of setting + * @returns {Promise} Value */ exports.setting = async function (key) { let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ @@ -524,29 +522,3 @@ exports.convertToUTF8 = (body) => { const str = iconv.decode(body, guessEncoding); return str.toString(); }; - -let logFile; - -try { - logFile = fs.createWriteStream("./data/error.log", { - flags: "a" - }); -} catch (_) { } - -/** - * Write error to log file - * @param {any} error The error to write - * @param {boolean} outputToConsole Should the error also be output to console? - */ -exports.errorLog = (error, outputToConsole = true) => { - try { - if (logFile) { - const dateTime = R.isoDateTime(); - logFile.write(`[${dateTime}] ` + nodeJsUtil.format(error) + "\n"); - - if (outputToConsole) { - console.error(error); - } - } - } catch (_) { } -};