-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Use log4j pattern syntax #57433
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
Use log4j pattern syntax #57433
Changes from 8 commits
2b6ddda
864daaf
94a89b8
bf3215a
96f1edb
9f491d4
20ecaa8
c8bc7e4
2b0fa2d
d98db45
d9fa26b
56cdef0
28d8c82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ import { last } from 'lodash'; | |
| import { Conversion } from './type'; | ||
| import { LogRecord } from '../../log_record'; | ||
|
|
||
| const timestampRegExp = /{timestamp({(?<format>[^}]+)})?({(?<timezone>[^}]+)})?}/gi; | ||
| const dateRegExp = /%date({(?<format>[^}]+)})?({(?<timezone>[^}]+)})?/g; | ||
|
|
||
| const formats = { | ||
| ISO8601: 'ISO8601', | ||
|
|
@@ -54,10 +54,11 @@ function formatDate(date: Date, dateFormat: string = formats.ISO8601, timezone?: | |
| } | ||
|
|
||
| function validateDateFormat(input: string) { | ||
| if (Reflect.has(formats, input)) return; | ||
| throw new Error( | ||
| `Date format expected one of ${Reflect.ownKeys(formats).join(', ')}, but given: ${input}` | ||
| ); | ||
| if (!Reflect.has(formats, input)) { | ||
| throw new Error( | ||
| `Date format expected one of ${Reflect.ownKeys(formats).join(', ')}, but given: ${input}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function validateTimezone(timezone: string) { | ||
|
|
@@ -66,21 +67,27 @@ function validateTimezone(timezone: string) { | |
| } | ||
|
|
||
| function validate(rawString: string) { | ||
| for (const matched of rawString.matchAll(timestampRegExp)) { | ||
|
Contributor
Author
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. TIL
Contributor
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. What was the issue outside of the scope of tests?
Contributor
Author
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. I contacted @elastic/kibana-operations to clarify this. The test and runtime envs should be identical
Contributor
Author
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. should be fixed in #57467 |
||
| const { format, timezone } = matched.groups!; | ||
| // clone regexp for exec since it's stateful with g flag | ||
| const regExp = new RegExp(dateRegExp, 'g'); | ||
| let matched: RegExpExecArray | null; | ||
| do { | ||
| matched = regExp.exec(rawString); | ||
| if (matched) { | ||
| const { format, timezone } = matched.groups!; | ||
|
|
||
| if (format) { | ||
| validateDateFormat(format); | ||
| } | ||
| if (timezone) { | ||
| validateTimezone(timezone); | ||
| if (format) { | ||
| validateDateFormat(format); | ||
| } | ||
| if (timezone) { | ||
| validateTimezone(timezone); | ||
| } | ||
| } | ||
| } | ||
| } while (matched); | ||
| } | ||
|
|
||
| export const TimestampConversion: Conversion = { | ||
| pattern: timestampRegExp, | ||
| formatter(record: LogRecord, highlight: boolean, ...matched: any[]) { | ||
| export const DateConversion: Conversion = { | ||
| pattern: dateRegExp, | ||
| convert(record: LogRecord, highlight: boolean, ...matched: any[]) { | ||
| const groups: Record<string, string | undefined> = last(matched); | ||
| const { format, timezone } = groups; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| export { Conversion } from './type'; | ||
|
|
||
| export { LoggerConversion } from './logger'; | ||
| export { LevelConversion } from './level'; | ||
| export { MessageConversion } from './message'; | ||
| export { MetaConversion } from './meta'; | ||
| export { PidConversion } from './pid'; | ||
| export { DateConversion } from './date'; |
Uh oh!
There was an error while loading. Please reload this page.