Initial commit
This commit is contained in:
commit
2683eae6b3
36 files changed
+7369
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { createLogger } from '../src/logger.js'
|
||||
|
||||
function capture() {
|
||||
const lines = []
|
||||
return {
|
||||
lines,
|
||||
stream: {
|
||||
write(line) {
|
||||
lines.push(line)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test('writes readable text logs and hides debug lines by default', () => {
|
||||
const sink = capture()
|
||||
const logger = createLogger({
|
||||
level: 'info',
|
||||
format: 'text',
|
||||
stream: sink.stream,
|
||||
bindings: { app: 'test' },
|
||||
})
|
||||
|
||||
logger.debug('hidden detail', { value: 1 })
|
||||
logger.info('server started', { port: 8080, name: 'a b' })
|
||||
logger.warn('careful', { attempt: 2 })
|
||||
|
||||
assert.equal(sink.lines.length, 2)
|
||||
assert.match(sink.lines[0], /^\d{4}-\d{2}-\d{2}T/)
|
||||
assert.match(sink.lines[0], /INFO {2}server started app=test port=8080 name="a b"/)
|
||||
assert.match(sink.lines[1], /WARN {2}careful app=test attempt=2/)
|
||||
})
|
||||
|
||||
test('redacts credentials but keeps configuration booleans', () => {
|
||||
const sink = capture()
|
||||
const logger = createLogger({ level: 'info', stream: sink.stream })
|
||||
|
||||
logger.info('config', {
|
||||
apiKey: 'secret-value',
|
||||
WIKI_API_KEY: 'secret-value',
|
||||
apiKeyConfigured: true,
|
||||
password: 'hunter2',
|
||||
normal: 'visible',
|
||||
})
|
||||
|
||||
const line = sink.lines.join('')
|
||||
assert.doesNotMatch(line, /secret-value|hunter2/)
|
||||
assert.match(line, /apiKey=\[redacted\]/)
|
||||
assert.match(line, /WIKI_API_KEY=\[redacted\]/)
|
||||
assert.match(line, /apiKeyConfigured=true/)
|
||||
assert.match(line, /normal=visible/)
|
||||
})
|
||||
|
||||
test('emits one JSON object per line with error details', () => {
|
||||
const sink = capture()
|
||||
const logger = createLogger({ level: 'debug', format: 'json', stream: sink.stream })
|
||||
const error = Object.assign(new Error('request failed'), { code: 'WIKI_ERROR', status: 502 })
|
||||
|
||||
logger.child({ jobId: 'job-1' }).error('export failed', { error, apiKey: 'nope' })
|
||||
|
||||
assert.equal(sink.lines.length, 1)
|
||||
const record = JSON.parse(sink.lines[0])
|
||||
assert.equal(record.level, 'error')
|
||||
assert.equal(record.message, 'export failed')
|
||||
assert.equal(record.jobId, 'job-1')
|
||||
assert.equal(record.apiKey, '[redacted]')
|
||||
assert.equal(record.error.message, 'request failed')
|
||||
assert.equal(record.error.code, 'WIKI_ERROR')
|
||||
assert.match(record.error.stack, /request failed/)
|
||||
})
|
||||
|
||||
test('silent level disables all output', () => {
|
||||
const sink = capture()
|
||||
const logger = createLogger({ level: 'silent', stream: sink.stream })
|
||||
|
||||
logger.error('nothing here')
|
||||
|
||||
assert.equal(sink.lines.length, 0)
|
||||
})
|
||||
Reference in new issue
Block a user