2019-09-27 09:28:09 +00:00
|
|
|
import _fs from 'fs-extra';
|
|
|
|
import {
|
|
|
|
logger,
|
|
|
|
setMeta,
|
2020-01-11 08:16:22 +00:00
|
|
|
addMeta,
|
|
|
|
removeMeta,
|
2019-09-27 09:28:09 +00:00
|
|
|
levels,
|
|
|
|
getErrors,
|
|
|
|
addStream,
|
|
|
|
} from '../../lib/logger';
|
|
|
|
import { add } from '../../lib/util/host-rules';
|
2019-07-15 09:04:05 +00:00
|
|
|
|
|
|
|
jest.unmock('../../lib/logger');
|
|
|
|
|
2019-09-27 09:28:09 +00:00
|
|
|
jest.mock('fs-extra');
|
|
|
|
const fs: any = _fs;
|
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
describe('logger', () => {
|
|
|
|
it('inits', () => {
|
|
|
|
expect(logger).toBeDefined();
|
|
|
|
});
|
|
|
|
it('supports logging with metadata', () => {
|
|
|
|
logger.debug({ some: 'meta' }, 'some meta');
|
|
|
|
});
|
|
|
|
it('supports logging with only metadata', () => {
|
|
|
|
logger.debug({ some: 'meta' });
|
|
|
|
});
|
|
|
|
it('supports logging without metadata', () => {
|
|
|
|
logger.debug('some meta');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets meta', () => {
|
|
|
|
setMeta({ any: 'test' });
|
|
|
|
});
|
|
|
|
|
2020-01-11 08:16:22 +00:00
|
|
|
it('adds meta', () => {
|
|
|
|
addMeta({ new: 'test' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes meta', () => {
|
|
|
|
removeMeta(['new']);
|
|
|
|
});
|
|
|
|
|
2019-07-15 09:04:05 +00:00
|
|
|
it('sets level', () => {
|
|
|
|
levels('stdout', 'debug');
|
|
|
|
});
|
2019-08-15 10:43:13 +00:00
|
|
|
|
|
|
|
it('saves errors', () => {
|
|
|
|
levels('stdout', 'fatal');
|
|
|
|
logger.error('some meta');
|
|
|
|
logger.error({ some: 'meta' });
|
|
|
|
logger.error({ some: 'meta' }, 'message');
|
|
|
|
expect(getErrors()).toMatchSnapshot();
|
|
|
|
});
|
2019-09-27 09:28:09 +00:00
|
|
|
|
|
|
|
it('should contain path or stream parameters', () => {
|
|
|
|
expect(() =>
|
|
|
|
addStream({
|
|
|
|
name: 'logfile',
|
|
|
|
level: 'error',
|
|
|
|
})
|
|
|
|
).toThrow("Missing 'stream' or 'path' for bunyan stream");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't support rotating files", () => {
|
|
|
|
expect(() =>
|
|
|
|
addStream({
|
|
|
|
name: 'logfile',
|
|
|
|
path: 'file.log',
|
|
|
|
level: 'error',
|
|
|
|
type: 'rotating-file',
|
|
|
|
})
|
|
|
|
).toThrow("Rotating files aren't supported");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports file-based logging', () => {
|
|
|
|
let chunk = null;
|
|
|
|
fs.createWriteStream.mockReturnValueOnce({
|
|
|
|
writable: true,
|
|
|
|
write(x) {
|
|
|
|
chunk = x;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
addStream({
|
|
|
|
name: 'logfile',
|
|
|
|
path: 'file.log',
|
|
|
|
level: 'error',
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.error('foo');
|
|
|
|
|
|
|
|
expect(JSON.parse(chunk).msg).toEqual('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles cycles', () => {
|
|
|
|
let logged = null;
|
|
|
|
fs.createWriteStream.mockReturnValueOnce({
|
|
|
|
writable: true,
|
|
|
|
write(x) {
|
|
|
|
logged = JSON.parse(x);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
addStream({
|
|
|
|
name: 'logfile',
|
|
|
|
path: 'file.log',
|
|
|
|
level: 'error',
|
|
|
|
});
|
|
|
|
|
|
|
|
const meta = { foo: null, bar: [] };
|
|
|
|
meta.foo = meta;
|
|
|
|
meta.bar.push(meta);
|
|
|
|
logger.error(meta, 'foo');
|
|
|
|
expect(logged.msg).toEqual('foo');
|
|
|
|
expect(logged.foo.foo).toEqual('[Circular]');
|
|
|
|
expect(logged.foo.bar).toEqual(['[Circular]']);
|
|
|
|
expect(logged.bar).toEqual('[Circular]');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sanitizes secrets', () => {
|
|
|
|
let logged = null;
|
|
|
|
fs.createWriteStream.mockReturnValueOnce({
|
|
|
|
writable: true,
|
|
|
|
write(x) {
|
|
|
|
logged = JSON.parse(x);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
addStream({
|
|
|
|
name: 'logfile',
|
|
|
|
path: 'file.log',
|
|
|
|
level: 'error',
|
|
|
|
});
|
|
|
|
add({ password: 'secret"password' });
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
foo: 'secret"password',
|
|
|
|
bar: ['somethingelse', 'secret"password'],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(logged.foo).not.toEqual('secret"password');
|
|
|
|
expect(logged.bar[0]).toEqual('somethingelse');
|
|
|
|
expect(logged.foo).toContain('redacted');
|
|
|
|
expect(logged.bar[1]).toContain('redacted');
|
|
|
|
});
|
2019-07-15 09:04:05 +00:00
|
|
|
});
|