Skip to content
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

[NP] Add meta-data and timestamp formatting for Log Record #56982

Merged
merged 11 commits into from
Feb 12, 2020
22 changes: 11 additions & 11 deletions src/core/server/logging/__snapshots__/logging_service.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions src/core/server/logging/layouts/conversions/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/

import chalk from 'chalk';

import { Conversion } from './type';
import { LogRecord } from '../../log_record';

export const ContextConversion: Conversion = {
pattern: /{context}/gi,
mshustov marked this conversation as resolved.
Show resolved Hide resolved
formatter(record: LogRecord, highlight: boolean) {
let message = record.context;
mshustov marked this conversation as resolved.
Show resolved Hide resolved
if (highlight) {
message = chalk.magenta(message);
}
return message;
},
};
44 changes: 44 additions & 0 deletions src/core/server/logging/layouts/conversions/level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

import chalk from 'chalk';

import { Conversion } from './type';
import { LogLevel } from '../../log_level';
import { LogRecord } from '../../log_record';

const LEVEL_COLORS = new Map([
[LogLevel.Fatal, chalk.red],
[LogLevel.Error, chalk.red],
[LogLevel.Warn, chalk.yellow],
[LogLevel.Debug, chalk.green],
[LogLevel.Trace, chalk.blue],
]);

export const LevelConversion: Conversion = {
pattern: /{level}/gi,
formatter(record: LogRecord, highlight: boolean) {
let message = record.level.id.toUpperCase().padEnd(5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about the padEnd and padStart methods.

if (highlight && LEVEL_COLORS.has(record.level)) {
const color = LEVEL_COLORS.get(record.level)!;
message = color(message);
}
return message;
},
};
29 changes: 29 additions & 0 deletions src/core/server/logging/layouts/conversions/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/

import { Conversion } from './type';
import { LogRecord } from '../../log_record';

export const MessageConversion: Conversion = {
pattern: /{message}/gi,
formatter(record: LogRecord) {
// Error stack is much more useful than just the message.
return (record.error && record.error.stack) || record.message;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of errors, can we have both error and message on the record?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but record.message === record.error.message. I suspect it was done in this way to simplify typings

message: errorOrMessage.message,

},
};
27 changes: 27 additions & 0 deletions src/core/server/logging/layouts/conversions/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/
import { Conversion } from './type';
import { LogRecord } from '../../log_record';

export const MetaConversion: Conversion = {
pattern: /{meta}/gi,
formatter(record: LogRecord) {
return record.meta ? `[${JSON.stringify(record.meta)}]` : '';
mshustov marked this conversation as resolved.
Show resolved Hide resolved
},
};
28 changes: 28 additions & 0 deletions src/core/server/logging/layouts/conversions/pid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*/

import { Conversion } from './type';
import { LogRecord } from '../../log_record';

export const PidConversion: Conversion = {
pattern: /{pid}/gi,
formatter(record: LogRecord) {
return String(record.pid);
},
};
Loading