33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { AppLogger } from '../src/logging/app-logger';
|
|
|
|
test('日志为单行 JSON,递归脱敏并安全处理循环对象与 bigint', () => {
|
|
const output: string[] = [];
|
|
const logger = new AppLogger('info', (line) => output.push(line));
|
|
const nested: Record<string, unknown> = {
|
|
password: 'private-password',
|
|
amount: 9n,
|
|
};
|
|
nested.self = nested;
|
|
logger.log('test_event', {
|
|
authorization: 'Bearer private-token',
|
|
qiniuAk: 'private-ak',
|
|
QINIU_SK: 'private-sk',
|
|
nested,
|
|
body: { code: 'login-secret' },
|
|
});
|
|
logger.debug('hidden');
|
|
assert.equal(output.length, 1);
|
|
const log = JSON.parse(output[0]!);
|
|
assert.equal(log.context.authorization, '[REDACTED]');
|
|
assert.equal(log.context.nested.password, '[REDACTED]');
|
|
assert.equal(log.context.nested.self, '[Circular]');
|
|
assert.equal(log.context.nested.amount, '9');
|
|
assert.equal(log.context.body, '[REDACTED]');
|
|
assert.equal(log.context.qiniuAk, '[REDACTED]');
|
|
assert.equal(log.context.QINIU_SK, '[REDACTED]');
|
|
assert.ok(!output[0]!.includes('private-'));
|
|
assert.ok(!output[0]!.includes('login-secret'));
|
|
});
|