diff --git a/server/logger.js b/server/logger.js deleted file mode 100644 index e69de29b..00000000 diff --git a/server/util-server.js b/server/util-server.js index 53b67985..074867f7 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -90,6 +90,9 @@ exports.pingAsync = function (hostname, ipv6 = false) { }; exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { + + log.debug("mqtt", `Topic: ${topic}`); + return new Promise((resolve, reject) => { const { port, username, password, interval = 20 } = options; @@ -114,7 +117,12 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { client.on("connect", () => { log.debug("mqtt", "MQTT subscribe topic"); - client.subscribe(topic); + + try { + client.subscribe(topic); + } catch (e) { + reject(new Error("Cannot subscribe topic")); + } }); client.on("error", (error) => { @@ -256,7 +264,7 @@ const parseCertificateInfo = function (info) { const existingList = {}; while (link) { - log.debug("util", `[${i}] ${link.fingerprint}`); + log.debug("cert", `[${i}] ${link.fingerprint}`); if (!link.valid_from || !link.valid_to) { break; @@ -271,7 +279,7 @@ const parseCertificateInfo = function (info) { if (link.issuerCertificate == null) { break; } else if (link.issuerCertificate.fingerprint in existingList) { - log.debug("util", `[Last] ${link.issuerCertificate.fingerprint}`); + log.debug("cert", `[Last] ${link.issuerCertificate.fingerprint}`); link.issuerCertificate = null; break; } else { @@ -292,7 +300,7 @@ exports.checkCertificate = function (res) { const info = res.request.res.socket.getPeerCertificate(true); const valid = res.request.res.socket.authorized || false; - log.debug("util", "Parsing Certificate Info"); + log.debug("cert", "Parsing Certificate Info"); const parsedInfo = parseCertificateInfo(info); return { diff --git a/src/util.js b/src/util.js index 059e13c2..b1f55201 100644 --- a/src/util.js +++ b/src/util.js @@ -54,7 +54,39 @@ function debug(msg) { } exports.debug = debug; class Logger { + constructor() { + /** + * UPTIME_KUMA_HIDE_LOG=debug_monitor,info_monitor + * + * Example: + * [ + * "debug_monitor", // Hide all logs that level is debug and the module is monitor + * "info_monitor", + * ] + */ + this.hideLog = { + info: [], + warn: [], + error: [], + debug: [], + }; + if (typeof process !== "undefined" && process.env.UPTIME_KUMA_HIDE_LOG) { + let list = process.env.UPTIME_KUMA_HIDE_LOG.split(",").map(v => v.toLowerCase()); + for (let pair of list) { + // split first "_" only + let values = pair.split(/_(.*)/s); + if (values.length >= 2) { + this.hideLog[values[0]].push(values[1]); + } + } + this.debug("server", "UPTIME_KUMA_HIDE_LOG is set"); + this.debug("server", this.hideLog); + } + } log(module, msg, level) { + if (this.hideLog[level] && this.hideLog[level].includes(module)) { + return; + } module = module.toUpperCase(); level = level.toUpperCase(); const now = new Date().toISOString(); diff --git a/src/util.ts b/src/util.ts index f584f604..75abd662 100644 --- a/src/util.ts +++ b/src/util.ts @@ -59,7 +59,46 @@ export function debug(msg: any) { } class Logger { + + /** + * UPTIME_KUMA_HIDE_LOG=debug_monitor,info_monitor + * + * Example: + * [ + * "debug_monitor", // Hide all logs that level is debug and the module is monitor + * "info_monitor", + * ] + */ + hideLog : any = { + info: [], + warn: [], + error: [], + debug: [], + }; + + constructor() { + if (typeof process !== "undefined" && process.env.UPTIME_KUMA_HIDE_LOG) { + let list = process.env.UPTIME_KUMA_HIDE_LOG.split(",").map(v => v.toLowerCase()); + + for (let pair of list) { + // split first "_" only + let values = pair.split(/_(.*)/s); + + if (values.length >= 2) { + this.hideLog[values[0]].push(values[1]); + } + } + + this.debug("server", "UPTIME_KUMA_HIDE_LOG is set"); + this.debug("server", this.hideLog); + } + } + log(module: string, msg: any, level: string) { + if (this.hideLog[level] && this.hideLog[level].includes(module)) { + return; + } + module = module.toUpperCase(); level = level.toUpperCase();