-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce LegacyAppender
that forwards log records to the legacy platform.
#14354
Changes from 3 commits
2dd3f40
0666d03
6342f69
575be11
b9aa1ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Schema, typeOfSchema } from '../../../types/schema'; | ||
import { LogRecord } from '../../../logging/LogRecord'; | ||
import { DisposableAppender } from '../../../logging/appenders/Appenders'; | ||
import { LegacyKbnServer } from '../../LegacyKbnServer'; | ||
|
||
const createSchema = (schema: Schema) => { | ||
const { literal, object } = schema; | ||
|
||
return object({ kind: literal('legacy-appender') }); | ||
}; | ||
|
||
const schemaType = typeOfSchema(createSchema); | ||
/** @internal */ | ||
export type LegacyAppenderConfigType = typeof schemaType; | ||
|
||
/** | ||
* Simple appender that just forwards `LogRecord` to the legacy KbnServer log. | ||
* @internal | ||
*/ | ||
export class LegacyAppender implements DisposableAppender { | ||
static createConfigSchema = createSchema; | ||
|
||
constructor(private readonly kbnServer: LegacyKbnServer) {} | ||
|
||
/** | ||
* Forwards `LogRecord` to the legacy platform that will layout and | ||
* write record to the configured destination. | ||
* @param record `LogRecord` instance to forward to. | ||
*/ | ||
append(record: LogRecord) { | ||
this.kbnServer.server.log( | ||
[record.level.id.toLowerCase(), ...record.context.split('.')], | ||
record.error || record.message, | ||
record.timestamp | ||
); | ||
} | ||
|
||
async dispose() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as mockSchema from '../../../../lib/schema'; | ||
import { LogLevel } from '../../../../logging/LogLevel'; | ||
import { LogRecord } from '../../../../logging/LogRecord'; | ||
import { LegacyAppender } from '../LegacyAppender'; | ||
|
||
test('`createConfigSchema()` creates correct schema.', () => { | ||
const appenderSchema = LegacyAppender.createConfigSchema(mockSchema); | ||
const validConfig = { kind: 'legacy-appender' }; | ||
expect(appenderSchema.validate(validConfig)).toEqual({ | ||
kind: 'legacy-appender' | ||
}); | ||
|
||
const wrongConfig = { kind: 'not-legacy-appender' }; | ||
expect(() => appenderSchema.validate(wrongConfig)).toThrow(); | ||
}); | ||
|
||
test('`append()` correctly pushes records to legacy platform.', () => { | ||
const timestamp = new Date(Date.UTC(2012, 1, 1, 11, 22, 33, 44)); | ||
const records: LogRecord[] = [ | ||
{ | ||
timestamp, | ||
message: 'message-1', | ||
context: 'context-1', | ||
level: LogLevel.Trace | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-2', | ||
context: 'context-2', | ||
level: LogLevel.Debug | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-3', | ||
context: 'context-3.sub-context-3', | ||
level: LogLevel.Info | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-4', | ||
context: 'context-4.sub-context-4', | ||
level: LogLevel.Warn | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-5-with-error', | ||
context: 'context-5', | ||
error: new Error('Some Error'), | ||
level: LogLevel.Error | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-6-with-message', | ||
context: 'context-6', | ||
level: LogLevel.Error | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-7-with-error', | ||
context: 'context-7.sub-context-7.sub-sub-context-7', | ||
error: new Error('Some Fatal Error'), | ||
level: LogLevel.Fatal | ||
}, | ||
{ | ||
timestamp, | ||
message: 'message-8-with-message', | ||
context: 'context-8.sub-context-8.sub-sub-context-8', | ||
level: LogLevel.Fatal | ||
} | ||
]; | ||
|
||
const legacyLogStub = jest.fn(); | ||
const appender = new LegacyAppender( | ||
{ server: { log: legacyLogStub } } as any | ||
); | ||
|
||
for (const record of records) { | ||
appender.append(record); | ||
} | ||
|
||
expect(legacyLogStub.mock.calls).toMatchSnapshot(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of snapshots imo 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it works pretty well for cases when arguments are predictable, borrowed the approach from your logger mock's |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`\`append()\` correctly pushes records to legacy platform. 1`] = ` | ||
Array [ | ||
Array [ | ||
Array [ | ||
"trace", | ||
"context-1", | ||
], | ||
"message-1", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"debug", | ||
"context-2", | ||
], | ||
"message-2", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"info", | ||
"context-3", | ||
"sub-context-3", | ||
], | ||
"message-3", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"warn", | ||
"context-4", | ||
"sub-context-4", | ||
], | ||
"message-4", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"error", | ||
"context-5", | ||
], | ||
[Error: Some Error], | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"error", | ||
"context-6", | ||
], | ||
"message-6-with-message", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"fatal", | ||
"context-7", | ||
"sub-context-7", | ||
"sub-sub-context-7", | ||
], | ||
[Error: Some Fatal Error], | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
Array [ | ||
Array [ | ||
"fatal", | ||
"context-8", | ||
"sub-context-8", | ||
"sub-sub-context-8", | ||
], | ||
"message-8-with-message", | ||
2012-02-01T11:22:33.044Z, | ||
], | ||
] | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: it seems in the legacy platform we can't provide both message and error :/