Skip to content

Commit c7c086a

Browse files
committed
feat: prettier logger
1 parent 3a00121 commit c7c086a

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

src/logger.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
class Logger {
22
enabled: boolean = false
3-
prefix: string = 'GHREPO'
3+
prefix: string = 'ghrepo'
4+
5+
#typeColor = {
6+
log: 0,
7+
error: 31,
8+
warn: 33,
9+
}
410

511
#log(type: 'log' | 'error' | 'warn', ...args: any[]) {
612
if (this.enabled) {
@@ -9,7 +15,16 @@ class Logger {
915
}
1016

1117
#verboseLog(type: 'log' | 'error' | 'warn', ...args: any[]) {
12-
console[type](`[${this.prefix}]`, ...args)
18+
const prefix = `\x1b[34m[${this.prefix}]`
19+
const color = this.#typeColor[type] ?? 0
20+
const colorStr = `\x1b[${color}m`
21+
const time = new Date()
22+
const hrs = time.getHours().toString().padStart(2, '0')
23+
const mns = time.getMinutes().toString().padStart(2, '0')
24+
const scs = time.getSeconds().toString().padStart(2, '0')
25+
const timeStr = `\x1b[2m${hrs}:${mns}:${scs}`
26+
const typeStr = type !== 'log' ? ' ' + type.toUpperCase() : ''
27+
console[type](`${timeStr} ${prefix}${colorStr}${typeStr}`, ...args)
1328
}
1429

1530
log(...args: any[]) {

test/logger.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'
2+
import { logger } from '../src/logger'
3+
4+
describe('logger', () => {
5+
const originalConsole = console
6+
let spies: Record<'log' | 'error' | 'warn', ReturnType<typeof vi.fn>>
7+
8+
const expected = {
9+
log: 0,
10+
error: 31,
11+
warn: 33,
12+
} as const
13+
14+
beforeEach(() => {
15+
spies = {
16+
log: vi.fn(),
17+
error: vi.fn(),
18+
warn: vi.fn(),
19+
}
20+
21+
console = {
22+
...originalConsole,
23+
...(spies as any),
24+
}
25+
})
26+
27+
afterEach(() => {
28+
console = originalConsole
29+
})
30+
31+
beforeAll(() => {
32+
logger.enabled = true
33+
})
34+
35+
test('appends time properly', () => {
36+
logger.log('test')
37+
const args = spies.log.mock.calls[0]
38+
const joined = args.join(' ')
39+
const timeIdx = 4
40+
const timeStr = joined.slice(timeIdx, timeIdx + 8)
41+
expect(timeStr).toMatch(/^\d{2}:\d{2}:\d{2}$/)
42+
})
43+
44+
describe('colors', () => {
45+
test('colors prefix properly', () => {
46+
const prefix = logger.prefix
47+
logger.log('test')
48+
const args = spies.log.mock.calls[0]
49+
const joined = args.join(' ')
50+
const prefixIdx = joined.indexOf(prefix)
51+
const prefixColorIdx = prefixIdx - 6
52+
53+
expect(prefixIdx).not.toBe(-1)
54+
expect(joined.slice(prefixColorIdx, prefixColorIdx + prefix.length)).toBe('\x1b[34m[')
55+
})
56+
57+
test('colors time properly', () => {
58+
logger.log('test')
59+
const args = spies.log.mock.calls[0]
60+
originalConsole.log('ARGS: ', JSON.stringify(spies.log.mock.calls))
61+
const joined = args.join(' ')
62+
const timeColorIdx = 0
63+
expect(joined.slice(timeColorIdx, timeColorIdx + 4)).toBe('\x1b[2m')
64+
})
65+
66+
for (const type of ['error', 'warn'] as const) {
67+
test(`colors ${type} properly`, () => {
68+
logger[type]('test')
69+
const args = spies[type].mock.calls[0]
70+
const joined = args.join(' ')
71+
const typeIdx = joined.indexOf(type.toUpperCase())
72+
const typeColorIdx = typeIdx - 6
73+
expect(joined.slice(typeColorIdx, typeColorIdx + 5)).toBe(`\x1b[${expected[type]}m`)
74+
})
75+
}
76+
})
77+
})

0 commit comments

Comments
 (0)