mirror of
https://github.com/HeyPuter/puter.git
synced 2026-05-04 08:30:39 +00:00
devex: show full time in dev log output
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,15 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user