Skip to content

Commit

Permalink
chore: unified our logging infrastructure to only use log.XYZ
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Elsinga <frank@elsinga.de>
Closes: louislam#4392

Squashed commit of the following:

commit 75c4c35
Merge: 14e3c3c dd75890
Author: Frank Elsinga <frank@elsinga.de>
Date:   Fri Sep 13 22:46:21 2024 +0800

    Merge branch 'master' into unified-logging-infrastructure

commit 14e3c3c
Merge: d622dfb c919d2c
Author: Frank Elsinga <frank@elsinga.de>
Date:   Mon Jun 3 16:00:56 2024 +0200

    Merge branch 'master' into unified-logging-infrastructure

commit d622dfb
Author: Frank Elsinga <frank@elsinga.de>
Date:   Thu Jan 18 22:31:10 2024 +0100

    unified our logging infrastructure
  • Loading branch information
SapphicCode committed Sep 19, 2024
1 parent 48d91cd commit 448c8ac
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 68 deletions.
6 changes: 3 additions & 3 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,12 @@ class Database {
let title = await Settings.get("title");

if (title) {
console.log("Migrating Status Page");
log.info("database", "Migrating Status Page");

let statusPageCheck = await R.findOne("status_page", " slug = 'default' ");

if (statusPageCheck !== null) {
console.log("Migrating Status Page - Skip, default slug record is already existing");
log.info("database", "Migrating Status Page - Skip, default slug record is already existing");
return;
}

Expand Down Expand Up @@ -566,7 +566,7 @@ class Database {
await Settings.set("entryPage", "statusPage-default", "general");
}

console.log("Migrating Status Page - Done");
log.info("database", "Migrating Status Page - Done");
}

}
Expand Down
61 changes: 22 additions & 39 deletions server/modules/apicache/apicache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let url = require("url");
let MemoryCache = require("./memory-cache");
const { log } = require("../../../src/util");

let t = {
ms: 1,
Expand Down Expand Up @@ -90,24 +91,6 @@ function ApiCache() {
instances.push(this);
this.id = instances.length;

/**
* Logs a message to the console if the `DEBUG` environment variable is set.
* @param {string} a The first argument to log.
* @param {string} b The second argument to log.
* @param {string} c The third argument to log.
* @param {string} d The fourth argument to log, and so on... (optional)
*
* Generated by Trelent
*/
function debug(a, b, c, d) {
let arr = ["\x1b[36m[apicache]\x1b[0m", a, b, c, d].filter(function (arg) {
return arg !== undefined;
});
let debugEnv = process.env.DEBUG && process.env.DEBUG.split(",").indexOf("apicache") !== -1;

return (globalOptions.debug || debugEnv) && console.log.apply(null, arr);
}

/**
* Returns true if the given request and response should be logged.
* @param {Object} request The HTTP request object.
Expand Down Expand Up @@ -146,7 +129,7 @@ function ApiCache() {
let groupName = req.apicacheGroup;

if (groupName) {
debug("group detected \"" + groupName + "\"");
log.debug("apicache", `group detected "${groupName}"`);
let group = (index.groups[groupName] = index.groups[groupName] || []);
group.unshift(key);
}
Expand Down Expand Up @@ -212,7 +195,7 @@ function ApiCache() {
redis.hset(key, "duration", duration);
redis.expire(key, duration / 1000, expireCallback || function () {});
} catch (err) {
debug("[apicache] error in redis.hset()");
log.debug("apicache", `error in redis.hset(): ${err}`);
}
} else {
memCache.add(key, value, duration, expireCallback);
Expand Down Expand Up @@ -320,10 +303,10 @@ function ApiCache() {

// display log entry
let elapsed = new Date() - req.apicacheTimer;
debug("adding cache entry for \"" + key + "\" @ " + strDuration, logDuration(elapsed));
debug("_apicache.headers: ", res._apicache.headers);
debug("res.getHeaders(): ", getSafeHeaders(res));
debug("cacheObject: ", cacheObject);
log.debug("apicache", `adding cache entry for "${key}" @ ${strDuration} ${logDuration(elapsed)}`);
log.debug("apicache", `_apicache.headers: ${res._apicache.headers}`);
log.debug("apicache", `res.getHeaders(): ${getSafeHeaders(res)}`);
log.debug("apicache", `cacheObject: ${cacheObject}`);
}
}

Expand Down Expand Up @@ -402,10 +385,10 @@ function ApiCache() {
let redis = globalOptions.redisClient;

if (group) {
debug("clearing group \"" + target + "\"");
log.debug("apicache", `clearing group "${target}"`);

group.forEach(function (key) {
debug("clearing cached entry for \"" + key + "\"");
log.debug("apicache", `clearing cached entry for "${key}"`);
clearTimeout(timers[key]);
delete timers[key];
if (!globalOptions.redisClient) {
Expand All @@ -414,15 +397,15 @@ function ApiCache() {
try {
redis.del(key);
} catch (err) {
console.log("[apicache] error in redis.del(\"" + key + "\")");
log.info("apicache", "error in redis.del(\"" + key + "\")");
}
}
index.all = index.all.filter(doesntMatch(key));
});

delete index.groups[target];
} else if (target) {
debug("clearing " + (isAutomatic ? "expired" : "cached") + " entry for \"" + target + "\"");
log.debug("apicache", `clearing ${isAutomatic ? "expired" : "cached"} entry for "${target}"`);
clearTimeout(timers[target]);
delete timers[target];
// clear actual cached entry
Expand All @@ -432,7 +415,7 @@ function ApiCache() {
try {
redis.del(target);
} catch (err) {
console.log("[apicache] error in redis.del(\"" + target + "\")");
log.error("apicache", "error in redis.del(\"" + target + "\")");
}
}

Expand All @@ -449,7 +432,7 @@ function ApiCache() {
}
});
} else {
debug("clearing entire index");
log.debug("apicache", "clearing entire index");

if (!redis) {
memCache.clear();
Expand All @@ -461,7 +444,7 @@ function ApiCache() {
try {
redis.del(key);
} catch (err) {
console.log("[apicache] error in redis.del(\"" + key + "\")");
log.error("apicache", `error in redis.del("${key}"): ${err}`);
}
});
}
Expand Down Expand Up @@ -528,7 +511,7 @@ function ApiCache() {

/**
* Get index of a group
* @param {string} group
* @param {string} group
* @returns {number}
*/
this.getIndex = function (group) {
Expand All @@ -543,9 +526,9 @@ function ApiCache() {
* Express middleware
* @param {(string|number)} strDuration Duration to cache responses
* for.
* @param {function(Object, Object):boolean} middlewareToggle
* @param {function(Object, Object):boolean} middlewareToggle
* @param {Object} localOptions Options for APICache
* @returns
* @returns
*/
this.middleware = function cache(strDuration, middlewareToggle, localOptions) {
let duration = instance.getDuration(strDuration);
Expand Down Expand Up @@ -752,7 +735,7 @@ function ApiCache() {
*/
let cache = function (req, res, next) {
function bypass() {
debug("bypass detected, skipping cache.");
log.debug("apicache", "bypass detected, skipping cache.");
return next();
}

Expand Down Expand Up @@ -805,7 +788,7 @@ function ApiCache() {
// send if cache hit from memory-cache
if (cached) {
let elapsed = new Date() - req.apicacheTimer;
debug("sending cached (memory-cache) version of", key, logDuration(elapsed));
log.debug("apicache", `sending cached (memory-cache) version of ${key} ${logDuration(elapsed)}`);

perf.hit(key);
return sendCachedResponse(req, res, cached, middlewareToggle, next, duration);
Expand All @@ -817,7 +800,7 @@ function ApiCache() {
redis.hgetall(key, function (err, obj) {
if (!err && obj && obj.response) {
let elapsed = new Date() - req.apicacheTimer;
debug("sending cached (redis) version of", key, logDuration(elapsed));
log.debug("apicache", "sending cached (redis) version of "+ key+" "+ logDuration(elapsed));

perf.hit(key);
return sendCachedResponse(
Expand Down Expand Up @@ -859,7 +842,7 @@ function ApiCache() {

/**
* Process options
* @param {Object} options
* @param {Object} options
* @returns {Object}
*/
this.options = function (options) {
Expand All @@ -873,7 +856,7 @@ function ApiCache() {
}

if (globalOptions.trackPerformance) {
debug("WARNING: using trackPerformance flag can cause high memory usage!");
log.debug("apicache", "WARNING: using trackPerformance flag can cause high memory usage!");
}

return this;
Expand Down
5 changes: 3 additions & 2 deletions server/monitor-types/real-browser-monitor-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (process.platform === "win32") {
* @returns {Promise<boolean>} The executable is allowed?
*/
async function isAllowedChromeExecutable(executablePath) {
console.log(config.args);
log.info("Chromium", config.args);
if (config.args["allow-all-chrome-exec"] || process.env.UPTIME_KUMA_ALLOW_ALL_CHROME_EXEC === "1") {
return true;
}
Expand Down Expand Up @@ -102,7 +102,8 @@ async function getBrowser() {
*/
async function getRemoteBrowser(remoteBrowserID, userId) {
let remoteBrowser = await RemoteBrowser.get(remoteBrowserID, userId);
log.debug("MONITOR", `Using remote browser: ${remoteBrowser.name} (${remoteBrowser.id})`);
log.debug("Chromium", `Using remote browser: ${remoteBrowser.name} (${remoteBrowser.id})`);
browser = chromium.connect(remoteBrowser.url);
browser = await chromium.connect(remoteBrowser.url);
return browser;
}
Expand Down
5 changes: 3 additions & 2 deletions server/notification-providers/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { getMonitorRelativeURL, UP } = require("../../src/util");
const { Settings } = require("../settings");
const { log } = require("../../src/util");

class Slack extends NotificationProvider {
name = "slack";
Expand All @@ -18,10 +19,10 @@ class Slack extends NotificationProvider {
let currentPrimaryBaseURL = await Settings.get("primaryBaseURL");

if (!currentPrimaryBaseURL) {
console.log("Move the url to be the primary base URL");
log.error("notification", "Move the url to be the primary base URL");
await Settings.set("primaryBaseURL", url, "general");
} else {
console.log("Already there, no need to move the primary base URL");
log.debug("notification", "Already there, no need to move the primary base URL");
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ let needSetup = false;
});

} catch (e) {
console.error(e);
log.error("server", e);

callback({
ok: false,
Expand Down
2 changes: 1 addition & 1 deletion server/socket-handlers/api-key-socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports.apiKeySocketHandler = (socket) => {
ok: true,
});
} catch (e) {
console.error(e);
log.error("apikeys", e);
callback({
ok: false,
msg: e.message,
Expand Down
4 changes: 2 additions & 2 deletions server/socket-handlers/cloudflared-socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ module.exports.autoStart = async (token) => {
} else {
// Override the current token via args or env var
await Settings.set("cloudflaredTunnelToken", token);
console.log("Use cloudflared token from args or env var");
log.info("cloudflare", "Use cloudflared token from args or env var");
}

if (token) {
console.log("Start cloudflared");
log.info("cloudflare", "Start cloudflared");
cloudflared.token = token;
cloudflared.start();
}
Expand Down
8 changes: 4 additions & 4 deletions server/socket-handlers/maintenance-socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
});

} catch (e) {
console.error(e);
log.error("maintenance", e);
callback({
ok: false,
msg: e.message,
Expand Down Expand Up @@ -177,7 +177,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
ok: true,
});
} catch (e) {
console.error(e);
log.error("maintenance", e);
callback({
ok: false,
msg: e.message,
Expand All @@ -201,7 +201,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
});

} catch (e) {
console.error(e);
log.error("maintenance", e);
callback({
ok: false,
msg: e.message,
Expand All @@ -225,7 +225,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
});

} catch (e) {
console.error(e);
log.error("maintenance", e);
callback({
ok: false,
msg: e.message,
Expand Down
2 changes: 1 addition & 1 deletion server/socket-handlers/status-page-socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ module.exports.statusPageSocketHandler = (socket) => {
});

} catch (error) {
console.error(error);
log.error("socket", error);
callback({
ok: false,
msg: error.message,
Expand Down
Loading

0 comments on commit 448c8ac

Please sign in to comment.