From b53dc01b453eaafecfcf4748e50438200e070441 Mon Sep 17 00:00:00 2001 From: KernelDeimos <7225168+KernelDeimos@users.noreply.github.com> Date: Wed, 15 Oct 2025 13:24:14 -0400 Subject: [PATCH] devex: show full time in dev log output --- src/backend/src/Extension.js | 3 ++- src/backend/src/modules/core/lib/log.js | 27 ++++++++++++++----- src/putility/src/libs/time.js | 36 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/backend/src/Extension.js b/src/backend/src/Extension.js index 8e3b9d1ab..227b3e2ea 100644 --- a/src/backend/src/Extension.js +++ b/src/backend/src/Extension.js @@ -21,6 +21,7 @@ const { AdvancedBase } = require("@heyputer/putility"); const EmitterFeature = require("@heyputer/putility/src/features/EmitterFeature"); const { Context } = require("./util/context"); const { ExtensionServiceState } = require("./ExtensionService"); +const { display_time } = require("@heyputer/putility/src/libs/time"); /** * This class creates the `extension` global that is seen by Puter backend @@ -281,7 +282,7 @@ class Extension extends AdvancedBase { get console () { const extensionConsole = Object.create(console); extensionConsole.log = (...a) => { - console.log(`\x1B[${this.terminal_color};1m(extension/${this.name})\x1B[0m`, ...a); + console.log(`${display_time(new Date())} \x1B[${this.terminal_color};1m(extension/${this.name})\x1B[0m`, ...a); }; return extensionConsole; } diff --git a/src/backend/src/modules/core/lib/log.js b/src/backend/src/modules/core/lib/log.js index 49c29c910..003b2d80e 100644 --- a/src/backend/src/modules/core/lib/log.js +++ b/src/backend/src/modules/core/lib/log.js @@ -17,7 +17,15 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -const log_epoch = Date.now(); +const { display_time, module_epoch } = require('@heyputer/putility/src/libs/time.js'); +const config = require('../../../config.js'); + +// Example: +// log("booting"); // → "14:07:12 booting" +// (next day) log("tick"); // → "16 00:00:01 tick" +// (next month) log("tick"); // → "11-01 00:00:01 tick" +// (next year) log("tick"); // → "2026-01-01 00:00:01 tick" + /** * Stringifies a log entry into a formatted string for console output. @@ -39,18 +47,26 @@ const stringify_log_entry = ({ prefix, log_lvl, crumbs, message, fields, objects if ( ! m ) return; lines.push(m); m = ''; + }; + + m = ''; + + if ( ! config.show_relative_time ) { + m += `${display_time(fields.timestamp)} `; } - m = prefix ? `${prefix} ` : ''; + m += prefix ? `${prefix} ` : ''; m += `\x1B[${log_lvl.esc}m[${log_lvl.label}\x1B[0m`; for ( const crumb of crumbs ) { m += `::${crumb}`; } m += `\x1B[${log_lvl.esc}m]\x1B[0m`; if ( fields.timestamp ) { - // display seconds since logger epoch - const n = (fields.timestamp - log_epoch) / 1000; - m += ` (${n.toFixed(3)}s)`; + if ( config.show_relative_time ) { + // display seconds since logger epoch + const n = (fields.timestamp - module_epoch) / 1000; + m += ` (${n.toFixed(3)}s)`; + } } m += ` ${message} `; lf(); @@ -69,5 +85,4 @@ const stringify_log_entry = ({ prefix, log_lvl, crumbs, message, fields, objects module.exports = { stringify_log_entry, - log_epoch, }; diff --git a/src/putility/src/libs/time.js b/src/putility/src/libs/time.js index 84c77bdea..7d1b87636 100644 --- a/src/putility/src/libs/time.js +++ b/src/putility/src/libs/time.js @@ -42,11 +42,47 @@ class DAY extends TimeUnit { static value = 24 * HOUR; } +const module_epoch = Date.now(); +const module_epoch_d = new Date(); + +/** + * Displays the current time in only the level of detail necessary based on + * the time this module was loaded. i.e. If this module was loaded on + * 2025-01-01 10:32:40, 5 minutes later this function would return "10:37:40", + * one day later this function would return "02 10:32:40", and one month later + * this function would return "02-01 10:32:40". + * @param {*} now - current time as Date object + */ +const display_time = (now) => { + const pad2 = n => String(n).padStart(2, "0"); + + const yyyy = now.getFullYear(); + const mm = pad2(now.getMonth() + 1); + const dd = pad2(now.getDate()); + const HH = pad2(now.getHours()); + const MM = pad2(now.getMinutes()); + const SS = pad2(now.getSeconds()); + const time = `${HH}:${MM}:${SS}`; + + const needYear = yyyy !== module_epoch_d.getFullYear(); + const needMonth = needYear || (now.getMonth() !== module_epoch_d.getMonth()); + const needDay = needMonth || (now.getDate() !== module_epoch_d.getDate()); + + if ( needYear ) return `${yyyy}-${mm}-${dd} ${time}`; + if ( needMonth ) return `${mm}-${dd} ${time}`; + if ( needDay ) return `${dd} ${time}`; + return time; // same calendar day as first log +}; + + module.exports = { MILLISECOND, SECOND, MINUTE, HOUR, DAY, + module_epoch, + module_epoch_d, + display_time, };