write some error logs to ./data/error.log

This commit is contained in:
Louis 2021-10-29 18:24:47 +08:00
parent 3e5ae00d25
commit d7f7dba13f
3 changed files with 21 additions and 2 deletions

View file

@ -7,7 +7,7 @@ dayjs.extend(timezone);
const axios = require("axios"); const axios = require("axios");
const { Prometheus } = require("../prometheus"); const { Prometheus } = require("../prometheus");
const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); 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 { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model"); const { BeanModel } = require("redbean-node/dist/bean-model");
const { Notification } = require("../notification"); const { Notification } = require("../notification");
@ -404,6 +404,7 @@ class Monitor extends BeanModel {
await beat(); await beat();
} catch (e) { } catch (e) {
console.trace(e); console.trace(e);
errorLog(e, false);
console.error("Please report to https://github.com/louislam/uptime-kuma/issues"); console.error("Please report to https://github.com/louislam/uptime-kuma/issues");
if (! this.isStop) { if (! this.isStop) {

View file

@ -41,7 +41,7 @@ console.log("Importing this project modules");
debug("Importing Monitor"); debug("Importing Monitor");
const Monitor = require("./model/monitor"); const Monitor = require("./model/monitor");
debug("Importing Settings"); 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"); debug("Importing Notification");
const { Notification } = require("./notification"); const { Notification } = require("./notification");
@ -1529,5 +1529,6 @@ gracefulShutdown(server, {
// Catch unexpected errors here // Catch unexpected errors here
process.addListener("unhandledRejection", (error, promise) => { process.addListener("unhandledRejection", (error, promise) => {
console.trace(error); console.trace(error);
errorLog(error, false);
console.error("If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues"); console.error("If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues");
}); });

View file

@ -8,6 +8,8 @@ const { Resolver } = require("dns");
const child_process = require("child_process"); const child_process = require("child_process");
const iconv = require("iconv-lite"); const iconv = require("iconv-lite");
const chardet = require("chardet"); const chardet = require("chardet");
const fs = require("fs");
const nodeJsUtil = require("util");
// From ping-lite // From ping-lite
exports.WIN = /^win/.test(process.platform); exports.WIN = /^win/.test(process.platform);
@ -332,3 +334,18 @@ exports.convertToUTF8 = (body) => {
const str = iconv.decode(body, guessEncoding); const str = iconv.decode(body, guessEncoding);
return str.toString(); 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 (_) { }
};