@contember/logger 2.2.0-alpha.2 → 2.2.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/index.cjs +1 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +2 -1
- package/dist/development/src/handlers/PrettyPrintLoggerHandler.cjs +25 -13
- package/dist/development/src/handlers/PrettyPrintLoggerHandler.cjs.map +1 -1
- package/dist/development/src/handlers/PrettyPrintLoggerHandler.js +26 -14
- package/dist/development/src/handlers/PrettyPrintLoggerHandler.js.map +1 -1
- package/dist/development/src/proxy.cjs +2 -0
- package/dist/development/src/proxy.cjs.map +1 -1
- package/dist/development/src/proxy.js +2 -0
- package/dist/development/src/proxy.js.map +1 -1
- package/dist/production/index.cjs +1 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +2 -1
- package/dist/production/src/handlers/PrettyPrintLoggerHandler.cjs +25 -13
- package/dist/production/src/handlers/PrettyPrintLoggerHandler.cjs.map +1 -1
- package/dist/production/src/handlers/PrettyPrintLoggerHandler.js +26 -14
- package/dist/production/src/handlers/PrettyPrintLoggerHandler.js.map +1 -1
- package/dist/production/src/proxy.cjs +2 -0
- package/dist/production/src/proxy.cjs.map +1 -1
- package/dist/production/src/proxy.js +2 -0
- package/dist/production/src/proxy.js.map +1 -1
- package/dist/types/formatting.d.ts.map +1 -1
- package/dist/types/handlers/FingerCrossedLoggerHandler.d.ts.map +1 -1
- package/dist/types/handlers/JsonStreamLoggerHandler.d.ts.map +1 -1
- package/dist/types/handlers/LoggerHandlerList.d.ts.map +1 -1
- package/dist/types/handlers/NullLoggerHandler.d.ts.map +1 -1
- package/dist/types/handlers/PrettyPrintLoggerHandler.d.ts +6 -2
- package/dist/types/handlers/PrettyPrintLoggerHandler.d.ts.map +1 -1
- package/dist/types/handlers/TestLoggerHandler.d.ts.map +1 -1
- package/dist/types/impl.d.ts.map +1 -1
- package/dist/types/proxy.d.ts +1 -0
- package/dist/types/proxy.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -4
- package/src/handlers/PrettyPrintLoggerHandler.ts +36 -15
- package/src/proxy.ts +2 -0
- package/tests/cases/unit/fixtures/jsonHandlerConsumer.ts +3 -0
- package/tests/cases/unit/fixtures/prettyPrintHandlerConsumer.ts +3 -0
- package/tests/cases/unit/prettyPrintHandler.test.ts +60 -0
- package/tests/cases/unit/treeShaking.test.ts +26 -0
- package/tests/tsconfig.json +10 -0
- package/tsconfig.json +4 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Writable } from 'node:stream'
|
|
3
|
+
import { createLogger, LogTextStyler, PrettyPrintLoggerHandler } from '../../../src/index.js'
|
|
4
|
+
|
|
5
|
+
const originalForceColor = process.env.FORCE_COLOR
|
|
6
|
+
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
if (originalForceColor === undefined) {
|
|
9
|
+
delete process.env.FORCE_COLOR
|
|
10
|
+
} else {
|
|
11
|
+
process.env.FORCE_COLOR = originalForceColor
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const capture = () => {
|
|
16
|
+
const chunks: string[] = []
|
|
17
|
+
const stream = new Writable({
|
|
18
|
+
write(chunk, _encoding, callback) {
|
|
19
|
+
chunks.push(String(chunk))
|
|
20
|
+
callback()
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
return { stream, text: () => chunks.join('') }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const logWithFormatter = () => {
|
|
27
|
+
const { stream, text } = capture()
|
|
28
|
+
let receivedStyle: LogTextStyler | undefined
|
|
29
|
+
const handler = new PrettyPrintLoggerHandler(stream, {
|
|
30
|
+
formatters: {
|
|
31
|
+
uri: ({ attributes, style }) => {
|
|
32
|
+
receivedStyle = style
|
|
33
|
+
return { line: style('green', `GET ${attributes.uri}`) }
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
createLogger(handler).warn('hello', { uri: '/x', other: 1 })
|
|
38
|
+
return { text: text(), receivedStyle }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('PrettyPrintLoggerHandler', () => {
|
|
42
|
+
test('writes plain text when colors are off', () => {
|
|
43
|
+
process.env.FORCE_COLOR = '0'
|
|
44
|
+
const { text, receivedStyle } = logWithFormatter()
|
|
45
|
+
expect(text).toMatch(/^\d{4}-\d\d-\d\dT[\d:]+\.\d+Z warn {2}hello\n/)
|
|
46
|
+
expect(text).toContain('GET /x\n')
|
|
47
|
+
expect(text).toContain('other: 1\n')
|
|
48
|
+
expect(text).not.toContain('\u001b[')
|
|
49
|
+
expect(receivedStyle?.(['bold', 'red'], 'x')).toBe('x')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('styles the level, attributes and formatter output when colors are forced', () => {
|
|
53
|
+
process.env.FORCE_COLOR = '1'
|
|
54
|
+
const { text, receivedStyle } = logWithFormatter()
|
|
55
|
+
expect(text).toContain('\u001b[93mwarn \u001b[39m')
|
|
56
|
+
expect(text).toContain('\u001b[32mGET /x\u001b[39m\n')
|
|
57
|
+
expect(text).toContain('\u001b[2mother: 1\n\u001b[22m')
|
|
58
|
+
expect(receivedStyle?.(['bold', 'red'], 'x')).toBe('\u001b[1m\u001b[31mx\u001b[39m\u001b[22m')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
|
|
4
|
+
const bundle = async (fixture: string): Promise<string> => {
|
|
5
|
+
const result = await Bun.build({
|
|
6
|
+
entrypoints: [fileURLToPath(new URL(`./fixtures/${fixture}`, import.meta.url))],
|
|
7
|
+
target: 'node',
|
|
8
|
+
})
|
|
9
|
+
expect(result.success).toBe(true)
|
|
10
|
+
return await result.outputs[0].text()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('tree shaking', () => {
|
|
14
|
+
test('a bundle without the pretty print handler drops it and its color detection', async () => {
|
|
15
|
+
const code = await bundle('jsonHandlerConsumer.ts')
|
|
16
|
+
expect(code).toContain('JsonStreamLoggerHandler')
|
|
17
|
+
expect(code).not.toContain('PrettyPrintLoggerHandler')
|
|
18
|
+
expect(code).not.toContain('styleText')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('a bundle with the pretty print handler keeps it', async () => {
|
|
22
|
+
const code = await bundle('prettyPrintHandlerConsumer.ts')
|
|
23
|
+
expect(code).toContain('PrettyPrintLoggerHandler')
|
|
24
|
+
expect(code).toContain('styleText')
|
|
25
|
+
})
|
|
26
|
+
})
|