Skip to content

Commit

Permalink
fix date formatting broken in #275
Browse files Browse the repository at this point in the history
  • Loading branch information
wclr committed Jul 1, 2021
1 parent 181d721 commit 2c5a52d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ 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.
*/
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)
}
Expand All @@ -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')
}

Expand Down

0 comments on commit 2c5a52d

Please sign in to comment.