forked from wclr/ts-node-dev
/
log.ts
58 lines (48 loc) · 1.4 KB
/
log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const util = require('util')
import { Config } from './cfg'
const colors = {
info: '36',
error: '31;1',
warn: '33',
debug: '90',
}
type LogFn = (msg: string, level: string) => void
export type Log = LogFn & {
error: (...p: any[]) => void
warn: (...p: any[]) => void
info: (...p: any[]) => void
debug: (...p: any[]) => void
}
type LogLevel = keyof typeof colors
/**
* 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
const c = colors[level.toLowerCase() as LogLevel] || '32'
console.log('[' + color(level.toUpperCase(), c) + '] ' + msg)
}
function color(s: string, c: string) {
if (process.stdout.isTTY) {
return '\x1B[' + c + 'm' + s + '\x1B[0m'
}
return s
}
log.debug = function () {
if (!cfg.debug) return
log(util.format.apply(util, arguments), 'debug')
}
log.info = function () {
log(util.format.apply(util, arguments), 'info')
}
log.warn = function () {
log(util.format.apply(util, arguments), 'warn')
}
log.error = function () {
log(util.format.apply(util, arguments), 'error')
}
return log as Log
}