Merge pull request #910 from andreasbrett/logging

introduce consistent logging
This commit is contained in:
Louis Lam 2022-04-13 23:47:08 +08:00 committed by GitHub
commit 17dcf6d3a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 332 additions and 180 deletions

View file

@ -2,7 +2,6 @@ const basicAuth = require("express-basic-auth");
const passwordHash = require("./password-hash"); const passwordHash = require("./password-hash");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { setting } = require("./util-server"); const { setting } = require("./util-server");
const { debug } = require("../src/util");
const { loginRateLimiter } = require("./rate-limiter"); const { loginRateLimiter } = require("./rate-limiter");
/** /**

View file

@ -1,7 +1,7 @@
const fs = require("fs"); const fs = require("fs");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { setSetting, setting } = require("./util-server"); const { setSetting, setting } = require("./util-server");
const { debug, sleep } = require("../src/util"); const { log, sleep } = require("../src/util");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const knex = require("knex"); const knex = require("knex");
@ -80,7 +80,7 @@ class Database {
fs.mkdirSync(Database.uploadDir, { recursive: true }); fs.mkdirSync(Database.uploadDir, { recursive: true });
} }
console.log(`Data Dir: ${Database.dataDir}`); log.info("db", `Data Dir: ${Database.dataDir}`);
} }
static async connect(testMode = false, autoloadModels = true, noLog = false) { static async connect(testMode = false, autoloadModels = true, noLog = false) {
@ -135,10 +135,10 @@ class Database {
await R.exec("PRAGMA synchronous = FULL"); await R.exec("PRAGMA synchronous = FULL");
if (!noLog) { if (!noLog) {
console.log("SQLite config:"); log.info("db", "SQLite config:");
console.log(await R.getAll("PRAGMA journal_mode")); log.info("db", await R.getAll("PRAGMA journal_mode"));
console.log(await R.getAll("PRAGMA cache_size")); log.info("db", await R.getAll("PRAGMA cache_size"));
console.log("SQLite Version: " + await R.getCell("SELECT sqlite_version()")); log.info("db", "SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
} }
} }
@ -149,15 +149,15 @@ class Database {
version = 0; version = 0;
} }
console.info("Your database version: " + version); log.info("db", "Your database version: " + version);
console.info("Latest database version: " + this.latestVersion); log.info("db", "Latest database version: " + this.latestVersion);
if (version === this.latestVersion) { if (version === this.latestVersion) {
console.info("Database patch not needed"); log.info("db", "Database patch not needed");
} else if (version > this.latestVersion) { } else if (version > this.latestVersion) {
console.info("Warning: Database version is newer than expected"); log.info("db", "Warning: Database version is newer than expected");
} else { } else {
console.info("Database patch is needed"); log.info("db", "Database patch is needed");
this.backup(version); this.backup(version);
@ -165,17 +165,17 @@ class Database {
try { try {
for (let i = version + 1; i <= this.latestVersion; i++) { for (let i = version + 1; i <= this.latestVersion; i++) {
const sqlFile = `./db/patch${i}.sql`; const sqlFile = `./db/patch${i}.sql`;
console.info(`Patching ${sqlFile}`); log.info("db", `Patching ${sqlFile}`);
await Database.importSQLFile(sqlFile); await Database.importSQLFile(sqlFile);
console.info(`Patched ${sqlFile}`); log.info("db", `Patched ${sqlFile}`);
await setSetting("database_version", i); await setSetting("database_version", i);
} }
} catch (ex) { } catch (ex) {
await Database.close(); await Database.close();
console.error(ex); log.error("db", ex);
console.error("Start Uptime-Kuma failed due to issue patching the database"); log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
console.error("Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues"); log.error("db", "Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
this.restore(); this.restore();
process.exit(1); process.exit(1);
@ -191,15 +191,15 @@ class Database {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
static async patch2() { static async patch2() {
console.log("Database Patch 2.0 Process"); log.info("db", "Database Patch 2.0 Process");
let databasePatchedFiles = await setting("databasePatchedFiles"); let databasePatchedFiles = await setting("databasePatchedFiles");
if (! databasePatchedFiles) { if (! databasePatchedFiles) {
databasePatchedFiles = {}; databasePatchedFiles = {};
} }
debug("Patched files:"); log.debug("db", "Patched files:");
debug(databasePatchedFiles); log.debug("db", databasePatchedFiles);
try { try {
for (let sqlFilename in this.patchList) { for (let sqlFilename in this.patchList) {
@ -207,15 +207,15 @@ class Database {
} }
if (this.patched) { if (this.patched) {
console.log("Database Patched Successfully"); log.info("db", "Database Patched Successfully");
} }
} catch (ex) { } catch (ex) {
await Database.close(); await Database.close();
console.error(ex); log.error("db", ex);
console.error("Start Uptime-Kuma failed due to issue patching the database"); log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues"); log.error("db", "Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
this.restore(); this.restore();
@ -302,16 +302,16 @@ class Database {
let value = this.patchList[sqlFilename]; let value = this.patchList[sqlFilename];
if (! value) { if (! value) {
console.log(sqlFilename + " skip"); log.info("db", sqlFilename + " skip");
return; return;
} }
// Check if patched // Check if patched
if (! databasePatchedFiles[sqlFilename]) { if (! databasePatchedFiles[sqlFilename]) {
console.log(sqlFilename + " is not patched"); log.info("db", sqlFilename + " is not patched");
if (value.parents) { if (value.parents) {
console.log(sqlFilename + " need parents"); log.info("db", sqlFilename + " need parents");
for (let parentSQLFilename of value.parents) { for (let parentSQLFilename of value.parents) {
await this.patch2Recursion(parentSQLFilename, databasePatchedFiles); await this.patch2Recursion(parentSQLFilename, databasePatchedFiles);
} }
@ -319,14 +319,14 @@ class Database {
this.backup(dayjs().format("YYYYMMDDHHmmss")); this.backup(dayjs().format("YYYYMMDDHHmmss"));
console.log(sqlFilename + " is patching"); log.info("db", sqlFilename + " is patching");
this.patched = true; this.patched = true;
await this.importSQLFile("./db/" + sqlFilename); await this.importSQLFile("./db/" + sqlFilename);
databasePatchedFiles[sqlFilename] = true; databasePatchedFiles[sqlFilename] = true;
console.log(sqlFilename + " was patched successfully"); log.info("db", sqlFilename + " was patched successfully");
} else { } else {
debug(sqlFilename + " is already patched, skip"); log.debug("db", sqlFilename + " is already patched, skip");
} }
} }
@ -378,7 +378,7 @@ class Database {
}; };
process.addListener("unhandledRejection", listener); process.addListener("unhandledRejection", listener);
console.log("Closing the database"); log.info("db", "Closing the database");
while (true) { while (true) {
Database.noReject = true; Database.noReject = true;
@ -388,10 +388,10 @@ class Database {
if (Database.noReject) { if (Database.noReject) {
break; break;
} else { } else {
console.log("Waiting to close the database"); log.info("db", "Waiting to close the database");
} }
} }
console.log("SQLite closed"); log.info("db", "SQLite closed");
process.removeListener("unhandledRejection", listener); process.removeListener("unhandledRejection", listener);
} }
@ -403,7 +403,7 @@ class Database {
*/ */
static backup(version) { static backup(version) {
if (! this.backupPath) { if (! this.backupPath) {
console.info("Backing up the database"); log.info("db", "Backing up the database");
this.backupPath = this.dataDir + "kuma.db.bak" + version; this.backupPath = this.dataDir + "kuma.db.bak" + version;
fs.copyFileSync(Database.path, this.backupPath); fs.copyFileSync(Database.path, this.backupPath);
@ -426,7 +426,7 @@ class Database {
*/ */
static restore() { static restore() {
if (this.backupPath) { if (this.backupPath) {
console.error("Patching the database failed!!! Restoring the backup"); log.error("db", "Patching the database failed!!! Restoring the backup");
const shmPath = Database.path + "-shm"; const shmPath = Database.path + "-shm";
const walPath = Database.path + "-wal"; const walPath = Database.path + "-wal";
@ -445,7 +445,7 @@ class Database {
fs.unlinkSync(walPath); fs.unlinkSync(walPath);
} }
} catch (e) { } catch (e) {
console.log("Restore failed; you may need to restore the backup manually"); log.error("db", "Restore failed; you may need to restore the backup manually");
process.exit(1); process.exit(1);
} }
@ -461,14 +461,14 @@ class Database {
} }
} else { } else {
console.log("Nothing to restore"); log.info("db", "Nothing to restore");
} }
} }
static getSize() { static getSize() {
debug("Database.getSize()"); log.debug("db", "Database.getSize()");
let stats = fs.statSync(Database.path); let stats = fs.statSync(Database.path);
debug(stats); log.debug("db", stats);
return stats.size; return stats.size;
} }

View file

@ -3,6 +3,7 @@
Modified with 0 dependencies Modified with 0 dependencies
*/ */
let fs = require("fs"); let fs = require("fs");
const { log } = require("../src/util");
let ImageDataURI = (() => { let ImageDataURI = (() => {
@ -14,7 +15,7 @@ let ImageDataURI = (() => {
*/ */
function decode(dataURI) { function decode(dataURI) {
if (!/data:image\//.test(dataURI)) { if (!/data:image\//.test(dataURI)) {
console.log("ImageDataURI :: Error :: It seems that it is not an Image Data URI. Couldn't match \"data:image/\""); log.error("image-data-uri", "It seems that it is not an Image Data URI. Couldn't match \"data:image/\"");
return null; return null;
} }
@ -35,7 +36,7 @@ let ImageDataURI = (() => {
*/ */
function encode(data, mediaType) { function encode(data, mediaType) {
if (!data || !mediaType) { if (!data || !mediaType) {
console.log("ImageDataURI :: Error :: Missing some of the required params: data, mediaType "); log.error("image-data-uri", "Missing some of the required params: data, mediaType");
return null; return null;
} }

View file

@ -1,6 +1,7 @@
const path = require("path"); const path = require("path");
const Bree = require("bree"); const Bree = require("bree");
const { SHARE_ENV } = require("worker_threads"); const { SHARE_ENV } = require("worker_threads");
const { log } = require("../src/util");
let bree; let bree;
const jobs = [ const jobs = [
{ {
@ -18,7 +19,7 @@ const initBackgroundJobs = function (args) {
workerData: args, workerData: args,
}, },
workerMessageHandler: (message) => { workerMessageHandler: (message) => {
console.log("[Background Job]:", message); log.info("jobs", message);
} }
}); });

0
server/logger.js Normal file
View file

View file

@ -6,7 +6,7 @@ dayjs.extend(utc);
dayjs.extend(timezone); 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 { log, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util");
const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog } = 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");
@ -193,7 +193,7 @@ class Monitor extends BeanModel {
rejectUnauthorized: !this.getIgnoreTls(), rejectUnauthorized: !this.getIgnoreTls(),
}; };
debug(`[${this.name}] Prepare Options for axios`); log.debug("monitor", `[${this.name}] Prepare Options for axios`);
const options = { const options = {
url: this.url, url: this.url,
@ -230,8 +230,8 @@ class Monitor extends BeanModel {
options.httpsAgent = new https.Agent(httpsAgentOptions); options.httpsAgent = new https.Agent(httpsAgentOptions);
} }
debug(`[${this.name}] Axios Options: ${JSON.stringify(options)}`); log.debug("monitor", `[${this.name}] Axios Options: ${JSON.stringify(options)}`);
debug(`[${this.name}] Axios Request`); log.debug("monitor", `[${this.name}] Axios Request`);
let res = await axios.request(options); let res = await axios.request(options);
bean.msg = `${res.status} - ${res.statusText}`; bean.msg = `${res.status} - ${res.statusText}`;
@ -240,29 +240,30 @@ class Monitor extends BeanModel {
// Check certificate if https is used // Check certificate if https is used
let certInfoStartTime = dayjs().valueOf(); let certInfoStartTime = dayjs().valueOf();
if (this.getUrl()?.protocol === "https:") { if (this.getUrl()?.protocol === "https:") {
debug(`[${this.name}] Check cert`); log.debug("monitor", `[${this.name}] Check cert`);
try { try {
let tlsInfoObject = checkCertificate(res); let tlsInfoObject = checkCertificate(res);
tlsInfo = await this.updateTlsInfo(tlsInfoObject); tlsInfo = await this.updateTlsInfo(tlsInfoObject);
if (!this.getIgnoreTls() && this.isEnabledExpiryNotification()) { if (!this.getIgnoreTls() && this.isEnabledExpiryNotification()) {
debug(`[${this.name}] call sendCertNotification`); log.debug("monitor", `[${this.name}] call sendCertNotification`);
await this.sendCertNotification(tlsInfoObject); await this.sendCertNotification(tlsInfoObject);
} }
} catch (e) { } catch (e) {
if (e.message !== "No TLS certificate in response") { if (e.message !== "No TLS certificate in response") {
console.error(e.message); log.error("monitor", "Caught error");
log.error("monitor", e.message);
} }
} }
} }
if (process.env.TIMELOGGER === "1") { if (process.env.TIMELOGGER === "1") {
debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms"); log.debug("monitor", "Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms");
} }
if (process.env.UPTIME_KUMA_LOG_RESPONSE_BODY_MONITOR_ID == this.id) { if (process.env.UPTIME_KUMA_LOG_RESPONSE_BODY_MONITOR_ID == this.id) {
console.log(res.data); log.info("monitor", res.data);
} }
if (this.type === "http") { if (this.type === "http") {
@ -342,7 +343,7 @@ class Monitor extends BeanModel {
time time
]); ]);
debug("heartbeatCount" + heartbeatCount + " " + time); log.debug("monitor", "heartbeatCount" + heartbeatCount + " " + time);
if (heartbeatCount <= 0) { if (heartbeatCount <= 0) {
// Fix #922, since previous heartbeat could be inserted by api, it should get from database // Fix #922, since previous heartbeat could be inserted by api, it should get from database
@ -426,7 +427,7 @@ class Monitor extends BeanModel {
} }
} }
debug(`[${this.name}] Check isImportant`); log.debug("monitor", `[${this.name}] Check isImportant`);
let isImportant = Monitor.isImportantBeat(isFirstBeat, previousBeat?.status, bean.status); let isImportant = Monitor.isImportantBeat(isFirstBeat, previousBeat?.status, bean.status);
// Mark as important if status changed, ignore pending pings, // Mark as important if status changed, ignore pending pings,
@ -434,11 +435,11 @@ class Monitor extends BeanModel {
if (isImportant) { if (isImportant) {
bean.important = true; bean.important = true;
debug(`[${this.name}] sendNotification`); log.debug("monitor", `[${this.name}] sendNotification`);
await Monitor.sendNotification(isFirstBeat, this, bean); await Monitor.sendNotification(isFirstBeat, this, bean);
// Clear Status Page Cache // Clear Status Page Cache
debug(`[${this.name}] apicache clear`); log.debug("monitor", `[${this.name}] apicache clear`);
apicache.clear(); apicache.clear();
} else { } else {
@ -446,33 +447,33 @@ class Monitor extends BeanModel {
} }
if (bean.status === UP) { if (bean.status === UP) {
console.info(`Monitor #${this.id} '${this.name}': Successful Response: ${bean.ping} ms | Interval: ${beatInterval} seconds | Type: ${this.type}`); log.info("monitor", `Monitor #${this.id} '${this.name}': Successful Response: ${bean.ping} ms | Interval: ${beatInterval} seconds | Type: ${this.type}`);
} else if (bean.status === PENDING) { } else if (bean.status === PENDING) {
if (this.retryInterval > 0) { if (this.retryInterval > 0) {
beatInterval = this.retryInterval; beatInterval = this.retryInterval;
} }
console.warn(`Monitor #${this.id} '${this.name}': Pending: ${bean.msg} | Max retries: ${this.maxretries} | Retry: ${retries} | Retry Interval: ${beatInterval} seconds | Type: ${this.type}`); log.warn("monitor", `Monitor #${this.id} '${this.name}': Pending: ${bean.msg} | Max retries: ${this.maxretries} | Retry: ${retries} | Retry Interval: ${beatInterval} seconds | Type: ${this.type}`);
} else { } else {
console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Interval: ${beatInterval} seconds | Type: ${this.type}`); log.warn("monitor", `Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Interval: ${beatInterval} seconds | Type: ${this.type}`);
} }
debug(`[${this.name}] Send to socket`); log.debug("monitor", `[${this.name}] Send to socket`);
io.to(this.user_id).emit("heartbeat", bean.toJSON()); io.to(this.user_id).emit("heartbeat", bean.toJSON());
Monitor.sendStats(io, this.id, this.user_id); Monitor.sendStats(io, this.id, this.user_id);
debug(`[${this.name}] Store`); log.debug("monitor", `[${this.name}] Store`);
await R.store(bean); await R.store(bean);
debug(`[${this.name}] prometheus.update`); log.debug("monitor", `[${this.name}] prometheus.update`);
prometheus.update(bean, tlsInfo); prometheus.update(bean, tlsInfo);
previousBeat = bean; previousBeat = bean;
if (! this.isStop) { if (! this.isStop) {
debug(`[${this.name}] SetTimeout for next check.`); log.debug("monitor", `[${this.name}] SetTimeout for next check.`);
this.heartbeatInterval = setTimeout(safeBeat, beatInterval * 1000); this.heartbeatInterval = setTimeout(safeBeat, beatInterval * 1000);
} else { } else {
console.log(`[${this.name}] isStop = true, no next check.`); log.info("monitor", `[${this.name}] isStop = true, no next check.`);
} }
}; };
@ -483,10 +484,10 @@ class Monitor extends BeanModel {
} catch (e) { } catch (e) {
console.trace(e); console.trace(e);
errorLog(e, false); errorLog(e, false);
console.error("Please report to https://github.com/louislam/uptime-kuma/issues"); log.error("monitor", "Please report to https://github.com/louislam/uptime-kuma/issues");
if (! this.isStop) { if (! this.isStop) {
console.log("Try to restart the monitor"); log.info("monitor", "Try to restart the monitor");
this.heartbeatInterval = setTimeout(safeBeat, this.interval * 1000); this.heartbeatInterval = setTimeout(safeBeat, this.interval * 1000);
} }
} }
@ -550,17 +551,17 @@ class Monitor extends BeanModel {
if (isValidObjects) { if (isValidObjects) {
if (oldCertInfo.certInfo.fingerprint256 !== checkCertificateResult.certInfo.fingerprint256) { if (oldCertInfo.certInfo.fingerprint256 !== checkCertificateResult.certInfo.fingerprint256) {
debug("Resetting sent_history"); log.debug("monitor", "Resetting sent_history");
await R.exec("DELETE FROM notification_sent_history WHERE type = 'certificate' AND monitor_id = ?", [ await R.exec("DELETE FROM notification_sent_history WHERE type = 'certificate' AND monitor_id = ?", [
this.id this.id
]); ]);
} else { } else {
debug("No need to reset sent_history"); log.debug("monitor", "No need to reset sent_history");
debug(oldCertInfo.certInfo.fingerprint256); log.debug("monitor", oldCertInfo.certInfo.fingerprint256);
debug(checkCertificateResult.certInfo.fingerprint256); log.debug("monitor", checkCertificateResult.certInfo.fingerprint256);
} }
} else { } else {
debug("Not valid object"); log.debug("monitor", "Not valid object");
} }
} catch (e) { } } catch (e) { }
@ -581,7 +582,7 @@ class Monitor extends BeanModel {
await Monitor.sendUptime(24 * 30, io, monitorID, userID); await Monitor.sendUptime(24 * 30, io, monitorID, userID);
await Monitor.sendCertInfo(io, monitorID, userID); await Monitor.sendCertInfo(io, monitorID, userID);
} else { } else {
debug("No clients in the room, no need to send stats"); log.debug("monitor", "No clients in the room, no need to send stats");
} }
} }
@ -728,8 +729,8 @@ class Monitor extends BeanModel {
try { try {
await Notification.send(JSON.parse(notification.config), msg, await monitor.toJSON(), bean.toJSON()); await Notification.send(JSON.parse(notification.config), msg, await monitor.toJSON(), bean.toJSON());
} catch (e) { } catch (e) {
console.error("Cannot send notification to " + notification.name); log.error("monitor", "Cannot send notification to " + notification.name);
console.log(e); log.error("monitor", e);
} }
} }
} }
@ -746,7 +747,7 @@ class Monitor extends BeanModel {
if (tlsInfoObject && tlsInfoObject.certInfo && tlsInfoObject.certInfo.daysRemaining) { if (tlsInfoObject && tlsInfoObject.certInfo && tlsInfoObject.certInfo.daysRemaining) {
const notificationList = await Monitor.getNotificationList(this); const notificationList = await Monitor.getNotificationList(this);
debug("call sendCertNotificationByTargetDays"); log.debug("monitor", "call sendCertNotificationByTargetDays");
await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 21, notificationList); await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 21, notificationList);
await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 14, notificationList); await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 14, notificationList);
await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 7, notificationList); await this.sendCertNotificationByTargetDays(tlsInfoObject.certInfo.daysRemaining, 7, notificationList);
@ -756,7 +757,7 @@ class Monitor extends BeanModel {
async sendCertNotificationByTargetDays(daysRemaining, targetDays, notificationList) { async sendCertNotificationByTargetDays(daysRemaining, targetDays, notificationList) {
if (daysRemaining > targetDays) { if (daysRemaining > targetDays) {
debug(`No need to send cert notification. ${daysRemaining} > ${targetDays}`); log.debug("monitor", `No need to send cert notification. ${daysRemaining} > ${targetDays}`);
return; return;
} }
@ -770,21 +771,21 @@ class Monitor extends BeanModel {
// Sent already, no need to send again // Sent already, no need to send again
if (row) { if (row) {
debug("Sent already, no need to send again"); log.debug("monitor", "Sent already, no need to send again");
return; return;
} }
let sent = false; let sent = false;
debug("Send certificate notification"); log.debug("monitor", "Send certificate notification");
for (let notification of notificationList) { for (let notification of notificationList) {
try { try {
debug("Sending to " + notification.name); log.debug("monitor", "Sending to " + notification.name);
await Notification.send(JSON.parse(notification.config), `[${this.name}][${this.url}] Certificate will be expired in ${daysRemaining} days`); await Notification.send(JSON.parse(notification.config), `[${this.name}][${this.url}] Certificate will be expired in ${daysRemaining} days`);
sent = true; sent = true;
} catch (e) { } catch (e) {
console.error("Cannot send cert notification to " + notification.name); log.error("monitor", "Cannot send cert notification to " + notification.name);
console.error(e); log.error("monitor", e);
} }
} }
@ -796,7 +797,7 @@ class Monitor extends BeanModel {
]); ]);
} }
} else { } else {
debug("No notification, no need to send cert notification"); log.debug("monitor", "No notification, no need to send cert notification");
} }
} }

View file

@ -1,7 +1,7 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const axios = require("axios"); const axios = require("axios");
const Crypto = require("crypto"); const Crypto = require("crypto");
const { debug } = require("../../src/util"); const { log } = require("../../src/util");
class Matrix extends NotificationProvider { class Matrix extends NotificationProvider {
name = "matrix"; name = "matrix";
@ -17,11 +17,11 @@ class Matrix extends NotificationProvider {
.slice(0, size) .slice(0, size)
); );
debug("Random String: " + randomString); log.debug("notification", "Random String: " + randomString);
const roomId = encodeURIComponent(notification.internalRoomId); const roomId = encodeURIComponent(notification.internalRoomId);
debug("Matrix Room ID: " + roomId); log.debug("notification", "Matrix Room ID: " + roomId);
try { try {
let config = { let config = {

View file

@ -24,6 +24,7 @@ const Feishu = require("./notification-providers/feishu");
const AliyunSms = require("./notification-providers/aliyun-sms"); const AliyunSms = require("./notification-providers/aliyun-sms");
const DingDing = require("./notification-providers/dingding"); const DingDing = require("./notification-providers/dingding");
const Bark = require("./notification-providers/bark"); const Bark = require("./notification-providers/bark");
const { log } = require("../src/util");
const SerwerSMS = require("./notification-providers/serwersms"); const SerwerSMS = require("./notification-providers/serwersms");
const Stackfield = require("./notification-providers/stackfield"); const Stackfield = require("./notification-providers/stackfield");
const WeCom = require("./notification-providers/wecom"); const WeCom = require("./notification-providers/wecom");
@ -36,7 +37,7 @@ class Notification {
providerList = {}; providerList = {};
static init() { static init() {
console.log("Prepare Notification Providers"); log.info("notification", "Prepare Notification Providers");
this.providerList = {}; this.providerList = {};

View file

@ -1,4 +1,5 @@
const PrometheusClient = require("prom-client"); const PrometheusClient = require("prom-client");
const { log } = require("../src/util");
const commonLabels = [ const commonLabels = [
"monitor_name", "monitor_name",
@ -56,7 +57,8 @@ class Prometheus {
} }
monitor_cert_is_valid.set(this.monitorLabelValues, is_valid); monitor_cert_is_valid.set(this.monitorLabelValues, is_valid);
} catch (e) { } catch (e) {
console.error(e); log.error("prometheus", "Caught error");
log.error("prometheus", e);
} }
try { try {
@ -64,14 +66,16 @@ class Prometheus {
monitor_cert_days_remaining.set(this.monitorLabelValues, tlsInfo.certInfo.daysRemaining); monitor_cert_days_remaining.set(this.monitorLabelValues, tlsInfo.certInfo.daysRemaining);
} }
} catch (e) { } catch (e) {
console.error(e); log.error("prometheus", "Caught error");
log.error("prometheus", e);
} }
} }
try { try {
monitor_status.set(this.monitorLabelValues, heartbeat.status); monitor_status.set(this.monitorLabelValues, heartbeat.status);
} catch (e) { } catch (e) {
console.error(e); log.error("prometheus", "Caught error");
log.error("prometheus", e);
} }
try { try {
@ -82,7 +86,8 @@ class Prometheus {
monitor_response_time.set(this.monitorLabelValues, -1); monitor_response_time.set(this.monitorLabelValues, -1);
} }
} catch (e) { } catch (e) {
console.error(e); log.error("prometheus", "Caught error");
log.error("prometheus", e);
} }
} }

View file

@ -1,5 +1,5 @@
const { RateLimiter } = require("limiter"); const { RateLimiter } = require("limiter");
const { debug } = require("../src/util"); const { log } = require("../src/util");
class KumaRateLimiter { class KumaRateLimiter {
constructor(config) { constructor(config) {
@ -9,7 +9,7 @@ class KumaRateLimiter {
async pass(callback, num = 1) { async pass(callback, num = 1) {
const remainingRequests = await this.removeTokens(num); const remainingRequests = await this.removeTokens(num);
debug("Rate Limit (remainingRequests):" + remainingRequests); log.info("rate-limit", "remaining requests: " + remainingRequests);
if (remainingRequests < 0) { if (remainingRequests < 0) {
if (callback) { if (callback) {
callback({ callback({

View file

@ -5,7 +5,7 @@ const server = require("../server");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
const Monitor = require("../model/monitor"); const Monitor = require("../model/monitor");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const { UP, flipStatus, debug } = require("../../src/util"); const { UP, flipStatus, log } = require("../../src/util");
const StatusPage = require("../model/status_page"); const StatusPage = require("../model/status_page");
let router = express.Router(); let router = express.Router();
@ -62,8 +62,8 @@ router.get("/api/push/:pushToken", async (request, response) => {
duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second"); duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
} }
debug("PreviousStatus: " + previousStatus); log.debug("router", "PreviousStatus: " + previousStatus);
debug("Current Status: " + status); log.debug("router", "Current Status: " + status);
bean.important = Monitor.isImportantBeat(isFirstBeat, previousStatus, status); bean.important = Monitor.isImportantBeat(isFirstBeat, previousStatus, status);
bean.monitor_id = monitor.id; bean.monitor_id = monitor.id;
@ -124,7 +124,7 @@ router.get("/api/status-page/:slug", cache("5 minutes"), async (request, respons
// Public Group List // Public Group List
const publicGroupList = []; const publicGroupList = [];
const showTags = !!statusPage.show_tags; const showTags = !!statusPage.show_tags;
debug("Show Tags???" + showTags);
const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [ const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [
statusPage.id statusPage.id
]); ]);

View file

@ -11,40 +11,42 @@ if (nodeVersion < requiredVersion) {
} }
const args = require("args-parser")(process.argv); const args = require("args-parser")(process.argv);
const { sleep, debug, getRandomInt, genSecret } = require("../src/util"); const { sleep, log, getRandomInt, genSecret, debug } = require("../src/util");
const config = require("./config"); const config = require("./config");
debug(args); log.info("server", "Welcome to Uptime Kuma");
log.debug("server", "Arguments");
log.debug("server", args);
if (! process.env.NODE_ENV) { if (! process.env.NODE_ENV) {
process.env.NODE_ENV = "production"; process.env.NODE_ENV = "production";
} }
console.log("Node Env: " + process.env.NODE_ENV); log.info("server", "Node Env: " + process.env.NODE_ENV);
console.log("Importing Node libraries"); log.info("server", "Importing Node libraries");
const fs = require("fs"); const fs = require("fs");
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
console.log("Importing 3rd-party libraries"); log.info("server", "Importing 3rd-party libraries");
debug("Importing express"); log.debug("server", "Importing express");
const express = require("express"); const express = require("express");
debug("Importing socket.io"); log.debug("server", "Importing socket.io");
const { Server } = require("socket.io"); const { Server } = require("socket.io");
debug("Importing redbean-node"); log.debug("server", "Importing redbean-node");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
debug("Importing jsonwebtoken"); log.debug("server", "Importing jsonwebtoken");
const jwt = require("jsonwebtoken"); const jwt = require("jsonwebtoken");
debug("Importing http-graceful-shutdown"); log.debug("server", "Importing http-graceful-shutdown");
const gracefulShutdown = require("http-graceful-shutdown"); const gracefulShutdown = require("http-graceful-shutdown");
debug("Importing prometheus-api-metrics"); log.debug("server", "Importing prometheus-api-metrics");
const prometheusAPIMetrics = require("prometheus-api-metrics"); const prometheusAPIMetrics = require("prometheus-api-metrics");
debug("Importing compare-versions"); log.debug("server", "Importing compare-versions");
const compareVersions = require("compare-versions"); const compareVersions = require("compare-versions");
const { passwordStrength } = require("check-password-strength"); const { passwordStrength } = require("check-password-strength");
debug("Importing 2FA Modules"); log.debug("server", "Importing 2FA Modules");
const notp = require("notp"); const notp = require("notp");
const base32 = require("thirty-two"); const base32 = require("thirty-two");
@ -69,23 +71,23 @@ class UptimeKumaServer {
const server = module.exports = new UptimeKumaServer(); const server = module.exports = new UptimeKumaServer();
console.log("Importing this project modules"); log.info("server", "Importing this project modules");
debug("Importing Monitor"); log.debug("server", "Importing Monitor");
const Monitor = require("./model/monitor"); const Monitor = require("./model/monitor");
debug("Importing Settings"); 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, errorLog, doubleCheckPassword } = require("./util-server");
debug("Importing Notification"); log.debug("server", "Importing Notification");
const { Notification } = require("./notification"); const { Notification } = require("./notification");
Notification.init(); Notification.init();
debug("Importing Proxy"); log.debug("server", "Importing Proxy");
const { Proxy } = require("./proxy"); const { Proxy } = require("./proxy");
debug("Importing Database"); log.debug("server", "Importing Database");
const Database = require("./database"); const Database = require("./database");
debug("Importing Background Jobs"); log.debug("server", "Importing Background Jobs");
const { initBackgroundJobs, stopBackgroundJobs } = require("./jobs"); const { initBackgroundJobs, stopBackgroundJobs } = require("./jobs");
const { loginRateLimiter, twoFaRateLimiter } = require("./rate-limiter"); const { loginRateLimiter, twoFaRateLimiter } = require("./rate-limiter");
@ -94,7 +96,7 @@ const { login } = require("./auth");
const passwordHash = require("./password-hash"); const passwordHash = require("./password-hash");
const checkVersion = require("./check-version"); const checkVersion = require("./check-version");
console.info("Version: " + checkVersion.version); log.info("server", "Version: " + checkVersion.version);
// If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available and the unspecified IPv4 address (0.0.0.0) otherwise. // If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available and the unspecified IPv4 address (0.0.0.0) otherwise.
// Dual-stack support for (::) // Dual-stack support for (::)
@ -103,7 +105,7 @@ let hostEnv = FBSD ? null : process.env.HOST;
let hostname = args.host || process.env.UPTIME_KUMA_HOST || hostEnv; let hostname = args.host || process.env.UPTIME_KUMA_HOST || hostEnv;
if (hostname) { if (hostname) {
console.log("Custom hostname: " + hostname); log.info("server", "Custom hostname: " + hostname);
} }
const port = [args.port, process.env.UPTIME_KUMA_PORT, process.env.PORT, 3001] const port = [args.port, process.env.UPTIME_KUMA_PORT, process.env.PORT, 3001]
@ -129,22 +131,22 @@ const twofa_verification_opts = {
const testMode = !!args["test"] || false; const testMode = !!args["test"] || false;
if (config.demoMode) { if (config.demoMode) {
console.log("==== Demo Mode ===="); log.info("server", "==== Demo Mode ====");
} }
console.log("Creating express and socket.io instance"); log.info("server", "Creating express and socket.io instance");
const app = express(); const app = express();
let httpServer; let httpServer;
if (sslKey && sslCert) { if (sslKey && sslCert) {
console.log("Server Type: HTTPS"); log.info("server", "Server Type: HTTPS");
httpServer = https.createServer({ httpServer = https.createServer({
key: fs.readFileSync(sslKey), key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert) cert: fs.readFileSync(sslCert)
}, app); }, app);
} else { } else {
console.log("Server Type: HTTP"); log.info("server", "Server Type: HTTP");
httpServer = http.createServer(app); httpServer = http.createServer(app);
} }
@ -200,7 +202,7 @@ try {
} catch (e) { } catch (e) {
// "dist/index.html" is not necessary for development // "dist/index.html" is not necessary for development
if (process.env.NODE_ENV !== "development") { if (process.env.NODE_ENV !== "development") {
console.error("Error: Cannot find 'dist/index.html', did you install correctly?"); log.error("server", "Error: Cannot find 'dist/index.html', did you install correctly?");
process.exit(1); process.exit(1);
} }
} }
@ -212,7 +214,7 @@ try {
exports.entryPage = await setting("entryPage"); exports.entryPage = await setting("entryPage");
await StatusPage.loadDomainMappingList(); await StatusPage.loadDomainMappingList();
console.log("Adding route"); log.info("server", "Adding route");
// *************************** // ***************************
// Normal Router here // Normal Router here
@ -270,7 +272,7 @@ try {
} }
}); });
console.log("Adding socket handler"); log.info("server", "Adding socket handler");
io.on("connection", async (socket) => { io.on("connection", async (socket) => {
sendInfo(socket); sendInfo(socket);
@ -278,7 +280,7 @@ try {
totalClient++; totalClient++;
if (needSetup) { if (needSetup) {
console.log("Redirect to setup page"); log.info("server", "Redirect to setup page");
socket.emit("setup"); socket.emit("setup");
} }
@ -291,33 +293,40 @@ try {
// *************************** // ***************************
socket.on("loginByToken", async (token, callback) => { socket.on("loginByToken", async (token, callback) => {
log.info("auth", `Login by token. IP=${getClientIp(socket)}`);
try { try {
let decoded = jwt.verify(token, jwtSecret); let decoded = jwt.verify(token, jwtSecret);
console.log("Username from JWT: " + decoded.username); log.info("auth", "Username from JWT: " + decoded.username);
let user = await R.findOne("user", " username = ? AND active = 1 ", [ let user = await R.findOne("user", " username = ? AND active = 1 ", [
decoded.username, decoded.username,
]); ]);
if (user) { if (user) {
debug("afterLogin"); log.debug("auth", "afterLogin");
afterLogin(socket, user); afterLogin(socket, user);
log.debug("auth", "afterLogin ok");
debug("afterLogin ok"); log.info("auth", `Successfully logged in user ${decoded.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: true, ok: true,
}); });
} else { } else {
log.info("auth", `Inactive or deleted user ${decoded.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: "The user is inactive or deleted.", msg: "The user is inactive or deleted.",
}); });
} }
} catch (error) { } catch (error) {
log.error("auth", `Invalid token. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: "Invalid token.", msg: "Invalid token.",
@ -327,7 +336,7 @@ try {
}); });
socket.on("login", async (data, callback) => { socket.on("login", async (data, callback) => {
console.log("Login"); log.info("auth", `Login by username + password. IP=${getClientIp(socket)}`);
// Checking // Checking
if (typeof callback !== "function") { if (typeof callback !== "function") {
@ -340,6 +349,7 @@ try {
// Login Rate Limit // Login Rate Limit
if (! await loginRateLimiter.pass(callback)) { if (! await loginRateLimiter.pass(callback)) {
log.info("auth", `Too many failed requests for user ${data.username}. IP=${getClientIp(socket)}`);
return; return;
} }
@ -348,6 +358,9 @@ try {
if (user) { if (user) {
if (user.twofa_status == 0) { if (user.twofa_status == 0) {
afterLogin(socket, user); afterLogin(socket, user);
log.info("auth", `Successfully logged in user ${data.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: true, ok: true,
token: jwt.sign({ token: jwt.sign({
@ -357,6 +370,9 @@ try {
} }
if (user.twofa_status == 1 && !data.token) { if (user.twofa_status == 1 && !data.token) {
log.info("auth", `2FA token required for user ${data.username}. IP=${getClientIp(socket)}`);
callback({ callback({
tokenRequired: true, tokenRequired: true,
}); });
@ -373,6 +389,8 @@ try {
socket.userID, socket.userID,
]); ]);
log.info("auth", `Successfully logged in user ${data.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: true, ok: true,
token: jwt.sign({ token: jwt.sign({
@ -380,6 +398,9 @@ try {
}, jwtSecret), }, jwtSecret),
}); });
} else { } else {
log.warn("auth", `Invalid token provided for user ${data.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: "Invalid Token!", msg: "Invalid Token!",
@ -387,6 +408,9 @@ try {
} }
} }
} else { } else {
log.warn("auth", `Incorrect username or password for user ${data.username}. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: "Incorrect username or password.", msg: "Incorrect username or password.",
@ -469,11 +493,16 @@ try {
socket.userID, socket.userID,
]); ]);
log.info("auth", `Saved 2FA token. IP=${getClientIp(socket)}`);
callback({ callback({
ok: true, ok: true,
msg: "2FA Enabled.", msg: "2FA Enabled.",
}); });
} catch (error) { } catch (error) {
log.error("auth", `Error changing 2FA token. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: error.message, msg: error.message,
@ -491,11 +520,16 @@ try {
await doubleCheckPassword(socket, currentPassword); await doubleCheckPassword(socket, currentPassword);
await TwoFA.disable2FA(socket.userID); await TwoFA.disable2FA(socket.userID);
log.info("auth", `Disabled 2FA token. IP=${getClientIp(socket)}`);
callback({ callback({
ok: true, ok: true,
msg: "2FA Disabled.", msg: "2FA Disabled.",
}); });
} catch (error) { } catch (error) {
log.error("auth", `Error disabling 2FA token. IP=${getClientIp(socket)}`);
callback({ callback({
ok: false, ok: false,
msg: error.message, msg: error.message,
@ -622,6 +656,8 @@ try {
await server.sendMonitorList(socket); await server.sendMonitorList(socket);
await startMonitor(socket.userID, bean.id); await startMonitor(socket.userID, bean.id);
log.info("monitor", `Added Monitor: ${monitor.id} User ID: ${socket.userID}`);
callback({ callback({
ok: true, ok: true,
msg: "Added Successfully.", msg: "Added Successfully.",
@ -629,6 +665,9 @@ try {
}); });
} catch (e) { } catch (e) {
log.error("monitor", `Error adding Monitor: ${monitor.id} User ID: ${socket.userID}`);
callback({ callback({
ok: false, ok: false,
msg: e.message, msg: e.message,
@ -691,7 +730,7 @@ try {
}); });
} catch (e) { } catch (e) {
console.error(e); log.error("monitor", e);
callback({ callback({
ok: false, ok: false,
msg: e.message, msg: e.message,
@ -707,7 +746,7 @@ try {
ok: true, ok: true,
}); });
} catch (e) { } catch (e) {
console.error(e); log.error("monitor", e);
callback({ callback({
ok: false, ok: false,
msg: e.message, msg: e.message,
@ -719,7 +758,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Get Monitor: ${monitorID} User ID: ${socket.userID}`); log.info("monitor", `Get Monitor: ${monitorID} User ID: ${socket.userID}`);
let bean = await R.findOne("monitor", " id = ? AND user_id = ? ", [ let bean = await R.findOne("monitor", " id = ? AND user_id = ? ", [
monitorID, monitorID,
@ -743,7 +782,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`); log.info("monitor", `Get Monitor Beats: ${monitorID} User ID: ${socket.userID}`);
if (period == null) { if (period == null) {
throw new Error("Invalid period."); throw new Error("Invalid period.");
@ -814,7 +853,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`); log.info("manage", `Delete Monitor: ${monitorID} User ID: ${socket.userID}`);
if (monitorID in server.monitorList) { if (monitorID in server.monitorList) {
server.monitorList[monitorID].stop(); server.monitorList[monitorID].stop();
@ -1146,7 +1185,7 @@ try {
let backupData = JSON.parse(uploadedJSON); let backupData = JSON.parse(uploadedJSON);
console.log(`Importing Backup, User ID: ${socket.userID}, Version: ${backupData.version}`); log.info("manage", `Importing Backup, User ID: ${socket.userID}, Version: ${backupData.version}`);
let notificationListData = backupData.notificationList; let notificationListData = backupData.notificationList;
let proxyListData = backupData.proxyList; let proxyListData = backupData.proxyList;
@ -1341,7 +1380,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Clear Events Monitor: ${monitorID} User ID: ${socket.userID}`); log.info("manage", `Clear Events Monitor: ${monitorID} User ID: ${socket.userID}`);
await R.exec("UPDATE heartbeat SET msg = ?, important = ? WHERE monitor_id = ? ", [ await R.exec("UPDATE heartbeat SET msg = ?, important = ? WHERE monitor_id = ? ", [
"", "",
@ -1367,7 +1406,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Clear Heartbeats Monitor: ${monitorID} User ID: ${socket.userID}`); log.info("manage", `Clear Heartbeats Monitor: ${monitorID} User ID: ${socket.userID}`);
await R.exec("DELETE FROM heartbeat WHERE monitor_id = ?", [ await R.exec("DELETE FROM heartbeat WHERE monitor_id = ?", [
monitorID monitorID
@ -1391,7 +1430,7 @@ try {
try { try {
checkLogin(socket); checkLogin(socket);
console.log(`Clear Statistics User ID: ${socket.userID}`); log.info("manage", `Clear Statistics User ID: ${socket.userID}`);
await R.exec("DELETE FROM heartbeat"); await R.exec("DELETE FROM heartbeat");
@ -1413,24 +1452,24 @@ try {
databaseSocketHandler(socket); databaseSocketHandler(socket);
proxySocketHandler(socket); proxySocketHandler(socket);
debug("added all socket handlers"); log.debug("server", "added all socket handlers");
// *************************** // ***************************
// Better do anything after added all socket handlers here // Better do anything after added all socket handlers here
// *************************** // ***************************
debug("check auto login"); log.debug("auth", "check auto login");
if (await setting("disableAuth")) { if (await setting("disableAuth")) {
console.log("Disabled Auth: auto login to admin"); log.info("auth", "Disabled Auth: auto login to admin");
afterLogin(socket, await R.findOne("user")); afterLogin(socket, await R.findOne("user"));
socket.emit("autoLogin"); socket.emit("autoLogin");
} else { } else {
debug("need auth"); log.debug("auth", "need auth");
} }
}); });
console.log("Init the server"); log.info("server", "Init the server");
httpServer.once("error", async (err) => { httpServer.once("error", async (err) => {
console.error("Cannot listen: " + err.message); console.error("Cannot listen: " + err.message);
@ -1439,9 +1478,9 @@ try {
httpServer.listen(port, hostname, () => { httpServer.listen(port, hostname, () => {
if (hostname) { if (hostname) {
console.log(`Listening on ${hostname}:${port}`); log.info("server", `Listening on ${hostname}:${port}`);
} else { } else {
console.log(`Listening on ${port}`); log.info("server", `Listening on ${port}`);
} }
startMonitors(); startMonitors();
checkVersion.startInterval(); checkVersion.startInterval();
@ -1555,13 +1594,13 @@ async function getMonitorJSONList(userID) {
*/ */
async function initDatabase(testMode = false) { async function initDatabase(testMode = false) {
if (! fs.existsSync(Database.path)) { if (! fs.existsSync(Database.path)) {
console.log("Copying Database"); log.info("server", "Copying Database");
fs.copyFileSync(Database.templatePath, Database.path); fs.copyFileSync(Database.templatePath, Database.path);
} }
console.log("Connecting to the Database"); log.info("server", "Connecting to the Database");
await Database.connect(testMode); await Database.connect(testMode);
console.log("Connected"); log.info("server", "Connected");
// Patch the database // Patch the database
await Database.patch(); await Database.patch();
@ -1571,16 +1610,16 @@ async function initDatabase(testMode = false) {
]); ]);
if (! jwtSecretBean) { if (! jwtSecretBean) {
console.log("JWT secret is not found, generate one."); log.info("server", "JWT secret is not found, generate one.");
jwtSecretBean = await initJWTSecret(); jwtSecretBean = await initJWTSecret();
console.log("Stored JWT secret into database"); log.info("server", "Stored JWT secret into database");
} else { } else {
console.log("Load JWT secret from database."); log.info("server", "Load JWT secret from database.");
} }
// If there is no record in user table, it is a new Uptime Kuma instance, need to setup // If there is no record in user table, it is a new Uptime Kuma instance, need to setup
if ((await R.count("user")) === 0) { if ((await R.count("user")) === 0) {
console.log("No user, need setup"); log.info("server", "No user, need setup");
needSetup = true; needSetup = true;
} }
@ -1597,7 +1636,7 @@ async function initDatabase(testMode = false) {
async function startMonitor(userID, monitorID) { async function startMonitor(userID, monitorID) {
await checkOwner(userID, monitorID); await checkOwner(userID, monitorID);
console.log(`Resume Monitor: ${monitorID} User ID: ${userID}`); log.info("manage", `Resume Monitor: ${monitorID} User ID: ${userID}`);
await R.exec("UPDATE monitor SET active = 1 WHERE id = ? AND user_id = ? ", [ await R.exec("UPDATE monitor SET active = 1 WHERE id = ? AND user_id = ? ", [
monitorID, monitorID,
@ -1630,7 +1669,7 @@ async function restartMonitor(userID, monitorID) {
async function pauseMonitor(userID, monitorID) { async function pauseMonitor(userID, monitorID) {
await checkOwner(userID, monitorID); await checkOwner(userID, monitorID);
console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`); log.info("manage", `Pause Monitor: ${monitorID} User ID: ${userID}`);
await R.exec("UPDATE monitor SET active = 0 WHERE id = ? AND user_id = ? ", [ await R.exec("UPDATE monitor SET active = 0 WHERE id = ? AND user_id = ? ", [
monitorID, monitorID,
@ -1666,10 +1705,10 @@ async function startMonitors() {
* Generated by Trelent * Generated by Trelent
*/ */
async function shutdownFunction(signal) { async function shutdownFunction(signal) {
console.log("Shutdown requested"); log.info("server", "Shutdown requested");
console.log("Called signal: " + signal); log.info("server", "Called signal: " + signal);
console.log("Stopping all monitors"); log.info("server", "Stopping all monitors");
for (let id in server.monitorList) { for (let id in server.monitorList) {
let monitor = server.monitorList[id]; let monitor = server.monitorList[id];
monitor.stop(); monitor.stop();
@ -1681,8 +1720,12 @@ async function shutdownFunction(signal) {
await cloudflaredStop(); await cloudflaredStop();
} }
function getClientIp(socket) {
return socket.client.conn.remoteAddress.replace(/^.*:/, "");
}
function finalFunction() { function finalFunction() {
console.log("Graceful shutdown successful!"); log.info("server", "Graceful shutdown successful!");
} }
gracefulShutdown(httpServer, { gracefulShutdown(httpServer, {

View file

@ -1,7 +1,7 @@
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { checkLogin, setSettings, setSetting } = require("../util-server"); const { checkLogin, setSettings, setSetting } = require("../util-server");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const { debug } = require("../../src/util"); const { log } = require("../../src/util");
const ImageDataURI = require("../image-data-uri"); const ImageDataURI = require("../image-data-uri");
const Database = require("../database"); const Database = require("../database");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
@ -202,8 +202,8 @@ module.exports.statusPageSocketHandler = (socket) => {
group.id = groupBean.id; group.id = groupBean.id;
} }
// Delete groups that not in the list // Delete groups that are not in the list
debug("Delete groups that not in the list"); log.debug("socket", "Delete groups that are not in the list");
const slots = groupIDList.map(() => "?").join(","); const slots = groupIDList.map(() => "?").join(",");
const data = [ const data = [
@ -226,7 +226,7 @@ module.exports.statusPageSocketHandler = (socket) => {
}); });
} catch (error) { } catch (error) {
console.error(error); log.error("socket", error);
callback({ callback({
ok: false, ok: false,

View file

@ -1,7 +1,7 @@
const tcpp = require("tcp-ping"); const tcpp = require("tcp-ping");
const Ping = require("./ping-lite"); const Ping = require("./ping-lite");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { debug, genSecret } = require("../src/util"); const { log, genSecret } = require("../src/util");
const passwordHash = require("./password-hash"); const passwordHash = require("./password-hash");
const { Resolver } = require("dns"); const { Resolver } = require("dns");
const child_process = require("child_process"); const child_process = require("child_process");
@ -119,7 +119,7 @@ exports.setting = async function (key) {
try { try {
const v = JSON.parse(value); const v = JSON.parse(value);
debug(`Get Setting: ${key}: ${v}`); log.debug("util", `Get Setting: ${key}: ${v}`);
return v; return v;
} catch (e) { } catch (e) {
return value; return value;
@ -206,7 +206,7 @@ const parseCertificateInfo = function (info) {
const existingList = {}; const existingList = {};
while (link) { while (link) {
debug(`[${i}] ${link.fingerprint}`); log.debug("util", `[${i}] ${link.fingerprint}`);
if (!link.valid_from || !link.valid_to) { if (!link.valid_from || !link.valid_to) {
break; break;
@ -221,7 +221,7 @@ const parseCertificateInfo = function (info) {
if (link.issuerCertificate == null) { if (link.issuerCertificate == null) {
break; break;
} else if (link.issuerCertificate.fingerprint in existingList) { } else if (link.issuerCertificate.fingerprint in existingList) {
debug(`[Last] ${link.issuerCertificate.fingerprint}`); log.debug("util", `[Last] ${link.issuerCertificate.fingerprint}`);
link.issuerCertificate = null; link.issuerCertificate = null;
break; break;
} else { } else {
@ -242,7 +242,7 @@ exports.checkCertificate = function (res) {
const info = res.request.res.socket.getPeerCertificate(true); const info = res.request.res.socket.getPeerCertificate(true);
const valid = res.request.res.socket.authorized || false; const valid = res.request.res.socket.authorized || false;
debug("Parsing Certificate Info"); log.debug("util", "Parsing Certificate Info");
const parsedInfo = parseCertificateInfo(info); const parsedInfo = parseCertificateInfo(info);
return { return {
@ -367,7 +367,6 @@ exports.startUnitTest = async () => {
*/ */
exports.convertToUTF8 = (body) => { exports.convertToUTF8 = (body) => {
const guessEncoding = chardet.detect(body); const guessEncoding = chardet.detect(body);
//debug("Guess Encoding: " + guessEncoding);
const str = iconv.decode(body, guessEncoding); const str = iconv.decode(body, guessEncoding);
return str.toString(); return str.toString();
}; };

View file

@ -52,7 +52,7 @@
<script> <script>
import Confirm from "../../components/Confirm.vue"; import Confirm from "../../components/Confirm.vue";
import { debug } from "../../util.ts"; import { log } from "../../util.ts";
import { useToast } from "vue-toastification"; import { useToast } from "vue-toastification";
const toast = useToast(); const toast = useToast();
@ -91,13 +91,13 @@ export default {
methods: { methods: {
loadDatabaseSize() { loadDatabaseSize() {
debug("load database size"); log.debug("monitorhistory", "load database size");
this.$root.getSocket().emit("getDatabaseSize", (res) => { this.$root.getSocket().emit("getDatabaseSize", (res) => {
if (res.ok) { if (res.ok) {
this.databaseSize = res.size; this.databaseSize = res.size;
debug("database size: " + res.size); log.debug("monitorhistory", "database size: " + res.size);
} else { } else {
debug(res); log.debug("monitorhistory", res);
} }
}); });
}, },
@ -108,7 +108,7 @@ export default {
this.loadDatabaseSize(); this.loadDatabaseSize();
toast.success("Done"); toast.success("Done");
} else { } else {
debug(res); log.debug("monitorhistory", res);
} }
}); });
}, },

View file

@ -7,7 +7,7 @@
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0;
const _dayjs = require("dayjs"); const _dayjs = require("dayjs");
const dayjs = _dayjs; const dayjs = _dayjs;
exports.isDev = process.env.NODE_ENV === "development"; exports.isDev = process.env.NODE_ENV === "development";
@ -44,12 +44,60 @@ function ucfirst(str) {
return firstLetter.toUpperCase() + str.substr(1); return firstLetter.toUpperCase() + str.substr(1);
} }
exports.ucfirst = ucfirst; exports.ucfirst = ucfirst;
/**
* @deprecated Use log.debug
* @since https://github.com/louislam/uptime-kuma/pull/910
* @param msg
*/
function debug(msg) { function debug(msg) {
if (exports.isDev) { exports.log.log("", msg, "debug");
console.log(msg);
}
} }
exports.debug = debug; exports.debug = debug;
class Logger {
log(module, msg, level) {
module = module.toUpperCase();
level = level.toUpperCase();
const now = new Date().toISOString();
const formattedMessage = (typeof msg === "string") ? `${now} [${module}] ${level}: ${msg}` : msg;
if (level === "INFO") {
console.info(formattedMessage);
}
else if (level === "WARN") {
console.warn(formattedMessage);
}
else if (level === "ERROR") {
console.error(formattedMessage);
}
else if (level === "DEBUG") {
if (exports.isDev) {
console.debug(formattedMessage);
}
}
else {
console.log(formattedMessage);
}
}
info(module, msg) {
this.log(module, msg, "info");
}
warn(module, msg) {
this.log(module, msg, "warn");
}
error(module, msg) {
this.log(module, msg, "error");
}
debug(module, msg) {
this.log(module, msg, "debug");
}
exception(module, exception, msg) {
let finalMessage = exception;
if (msg) {
finalMessage = `${msg}: ${exception}`;
}
this.log(module, finalMessage, "error");
}
}
exports.log = new Logger();
function polyfill() { function polyfill() {
/** /**
* String.prototype.replaceAll() polyfill * String.prototype.replaceAll() polyfill

View file

@ -49,12 +49,66 @@ export function ucfirst(str: string) {
return firstLetter.toUpperCase() + str.substr(1); return firstLetter.toUpperCase() + str.substr(1);
} }
/**
* @deprecated Use log.debug
* @since https://github.com/louislam/uptime-kuma/pull/910
* @param msg
*/
export function debug(msg: any) { export function debug(msg: any) {
log.log("", msg, "debug");
}
class Logger {
log(module: string, msg: any, level: string) {
module = module.toUpperCase();
level = level.toUpperCase();
const now = new Date().toISOString();
const formattedMessage = (typeof msg === "string") ? `${now} [${module}] ${level}: ${msg}` : msg;
if (level === "INFO") {
console.info(formattedMessage);
} else if (level === "WARN") {
console.warn(formattedMessage);
} else if (level === "ERROR") {
console.error(formattedMessage);
} else if (level === "DEBUG") {
if (isDev) { if (isDev) {
console.log(msg); console.debug(formattedMessage);
}
} else {
console.log(formattedMessage);
}
}
info(module: string, msg: any) {
this.log(module, msg, "info");
}
warn(module: string, msg: any) {
this.log(module, msg, "warn");
}
error(module: string, msg: any) {
this.log(module, msg, "error");
}
debug(module: string, msg: any) {
this.log(module, msg, "debug");
}
exception(module: string, exception: any, msg: any) {
let finalMessage = exception
if (msg) {
finalMessage = `${msg}: ${exception}`
}
this.log(module, finalMessage , "error");
} }
} }
export const log = new Logger();
declare global { interface String { replaceAll(str: string, newStr: string): string; } } declare global { interface String { replaceAll(str: string, newStr: string): string; } }