Add HIDE_LOG and catch error if cannot subscribe topic

This commit is contained in:
Louis Lam 2022-04-16 14:50:48 +08:00
parent 083e8355b7
commit 30e113755e
4 changed files with 83 additions and 4 deletions

View file

View file

@ -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 {

View file

@ -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();

View file

@ -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();