kibana/packages/kbn-legacy-logging/src/legacy_logging_server.test.ts
Pierre Gayvallet e176def756
create kbn-legacy-logging package (#77678)
* create kbn-legacy-logging package and start to move things

* fix rotator tests

* fix logging system test mocks

* move logging format to the package

* move logging setup to package

* adapt legacy logging server

* remove usage of legacy config in the legacy logging server

* move legacy logging server to package

* remove `??` syntax from package

* update generated doc

* fix a few things due to month old merge

* remove typings from project

* move reconfigureLogging to package

* add basic README file

* update generated doc

* remove old typings

* add typing for legacy logging events

* remove `??` from packages

* fix / improve event types usages

* remove suffix from tsconfig
2020-11-22 20:46:38 +01:00

116 lines
2.8 KiB
TypeScript

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
jest.mock('./setup_logging');
import { LegacyLoggingServer, LogRecord } from './legacy_logging_server';
test('correctly forwards log records.', () => {
const loggingServer = new LegacyLoggingServer({ events: {} });
const onLogMock = jest.fn();
loggingServer.events.on('log', onLogMock);
const timestamp = 1554433221100;
const firstLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: {
id: 'info',
value: 5,
},
context: 'some-context',
message: 'some-message',
};
const secondLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: {
id: 'error',
value: 3,
},
context: 'some-context.sub-context',
message: 'some-message',
meta: { unknown: 2 },
error: new Error('some-error'),
};
const thirdLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: {
id: 'trace',
value: 7,
},
context: 'some-context.sub-context',
message: 'some-message',
meta: { tags: ['important', 'tags'], unknown: 2 },
};
loggingServer.log(firstLogRecord);
loggingServer.log(secondLogRecord);
loggingServer.log(thirdLogRecord);
expect(onLogMock).toHaveBeenCalledTimes(3);
const [[firstCall], [secondCall], [thirdCall]] = onLogMock.mock.calls;
expect(firstCall).toMatchInlineSnapshot(`
Object {
"data": "some-message",
"tags": Array [
"info",
"some-context",
],
"timestamp": 1554433221100,
}
`);
expect(secondCall).toMatchInlineSnapshot(`
Object {
"data": [Error: some-error],
"tags": Array [
"error",
"some-context",
"sub-context",
],
"timestamp": 1554433221100,
}
`);
expect(thirdCall).toMatchInlineSnapshot(`
Object {
"data": Object {
Symbol(log message with metadata): Object {
"message": "some-message",
"metadata": Object {
"unknown": 2,
},
},
},
"tags": Array [
"debug",
"some-context",
"sub-context",
"important",
"tags",
],
"timestamp": 1554433221100,
}
`);
});