From 2c5a52d65e1a31322e4cae2813b65380a27173f2 Mon Sep 17 00:00:00 2001 From: wclr Date: Thu, 1 Jul 2021 21:21:10 +0500 Subject: [PATCH] fix date formatting broken in #275 --- src/log.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/log.ts b/src/log.ts index bbf2f24..feea168 100644 --- a/src/log.ts +++ b/src/log.ts @@ -19,6 +19,12 @@ export type Log = LogFn & { type LogLevel = keyof typeof colors +const formatDate = (date: Date) => { + return [date.getHours(), date.getMinutes(), date.getSeconds()] + .map((n) => n.toString().padStart(2, '0')) + .join(':') +} + /** * Logs a message to the console. The level is displayed in ANSI colors, * either bright red in case of an error or green otherwise. @@ -26,7 +32,7 @@ type LogLevel = keyof typeof colors export const makeLog = function (cfg: Config) { function log(msg: string, level: LogLevel) { if (cfg.quiet && level === 'info') return - if (cfg.timestamp) msg = color(new Date(cfg.timestamp).toLocaleString(), '30;1') + ' ' + msg + if (cfg.timestamp) msg = color(formatDate(new Date()), '30;1') + ' ' + msg const c = colors[level.toLowerCase() as LogLevel] || '32' console.log('[' + color(level.toUpperCase(), c) + '] ' + msg) } @@ -42,7 +48,7 @@ export const makeLog = function (cfg: Config) { if (!cfg.debug) return log(util.format.apply(util, arguments), 'debug') } - log.info = function () { + log.info = function () { log(util.format.apply(util, arguments), 'info') }