-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Conversation
Pinging @elastic/kibana-platform (Team:Platform) |
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
💔 Build FailedTest FailuresKibana Pipeline / kibana-intake-agent / Jest Tests.src/core/server/logging/layouts.format timestamp supports specifying a predefined format ABSOLUTEStandard Out
Stack Trace
History
To update your PR or re-run it, just comment with: |
@@ -45,6 +45,7 @@ jest.mock('moment-timezone', () => { | |||
// timezone in all tests. | |||
const moment = jest.requireActual('moment-timezone'); | |||
moment.tz.guess = () => 'America/New_York'; | |||
moment.tz.setDefault('America/New_York'); |
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.
Unifying timezone across all the tests. Should be compatible with the guess
result.
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.
APM changes LGTM! Thanks
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
Just to be clear, these differences only affect appenders configured with the new logging config, correct? The legacy logging functionality is untouched? |
import { Conversion } from './type'; | ||
import { LogRecord } from '../../log_record'; | ||
|
||
const timestampRegExp = /{timestamp({(?<format>[^}]+)})?({(?<timezone>[^}]+)})?}/gi; |
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.
I think maybe my last comment here was missed: #41956 (comment)
Is there a reason we can't support the same syntax as log4j? Eg. %timestamp{options}
instead of {timestamp{options}}
?
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.
my bad, I forgot to answer: agree, we can switch to log4h-like syntax, but I will do it in a separate PR since it affects all the tests. added to #41956
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.
Please ignore the personal NIT if you feel like it.
The only (minor) thing that bothers me is regarding colorization. With current implementation, level/context will not colorize their outer []
. This make sense technically, as the []
are manually specified around the {level}
parts in the format, but I wonder if we should find a way to allow to colorize that.
My point being that with, say
pattern = "[{timestamp}][{level}][{context}]{meta} {message}";
, I think the user might want to be able to have the whole [WARN]
colorized instead of uncolorized('[')colorized('WARN')uncolorized(']')
?
pattern: /{message}/gi, | ||
formatter(record: LogRecord) { | ||
// Error stack is much more useful than just the message. | ||
return (record.error && record.error.stack) || record.message; |
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.
In case of errors, can we have both error
and message
on the record?
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.
yes, but record.message === record.error.message
. I suspect it was done in this way to simplify typings
kibana/src/core/server/logging/logger.ts
Line 162 in 5abca85
message: errorOrMessage.message, |
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.
Tested locally, worked as expected 👍
pid: 5355, | ||
meta: {}, | ||
}) | ||
).toBe('[2012-02-01T14:30:22.011Z][DEBUG][context-meta][{}] message-meta'); |
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.
Is it useful to include meta in the layout if it's an empty object?
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.
an object could be empty or stringified to an empty object. In this case, I'd rather show it explicitly in logs. Otherwise, we will have complaints on
meta-object disappearing from the logs.
let something = undefined;
// ....
log('message', { something }); // absence from the log since stringified to an empty object
export const LevelConversion: Conversion = { | ||
pattern: /{level}/gi, | ||
formatter(record: LogRecord, highlight: boolean) { | ||
let message = record.level.id.toUpperCase().padEnd(5); |
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.
TIL about the padEnd
and padStart
methods.
} | ||
|
||
export const TimestampConversion: Conversion = { | ||
pattern: timestampRegExp, |
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.
For some of the more complex regexes, what do you think about testing them directly? It's sort of covered in the tests on PatternLayout, but it could be useful to test the edge cases of the regex explicitly. Maybe would make sense to do after changing to the log4j syntax.
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.
I could move tests here, but I think it's more important to test conversion compatibility. That would be especially important when we introduce nested patterns (like from example with colorization) %blue{[%level]}
…6982) * log meta in pattern layout * address pgayvallet comment * add conversion patterns for timestamp * use comparison instead of inline snapshots * logs. use elasticsearch and LP compatible timezone format in json layout * use regexp groups and dot separator for ms. as in default iso format * use America/New_York timezone as default as it set in guess anyway * fix APM tests. they need to reset timezone locally and restore it after * fix logging tests
@pgayvallet I fixed your nits, but didn't push changes. will do in the following PR |
…57419) * log meta in pattern layout * address pgayvallet comment * add conversion patterns for timestamp * use comparison instead of inline snapshots * logs. use elasticsearch and LP compatible timezone format in json layout * use regexp groups and dot separator for ms. as in default iso format * use America/New_York timezone as default as it set in guess anyway * fix APM tests. they need to reset timezone locally and restore it after * fix logging tests
Summary
Meta
pattern: 'layout':
PR introduces
{meta}
conversion. meta data logged (if present) as a result of JSON stringified:pattern: 'json'
continues logging meta-data as a separate field to prevent property overlapping.
This is different from LP format and elasticsearch format, where meta data allowed to override log record fields!
Timestamp
pattern: 'layout'
PR introduces additional config fields for
timestamp
:date format
andtimezone
. Declared similarly to log4j format:{timestamp{FORMAT}{TIMEZONE}}
.I'm a bit hesitant to allow users to specify any custom date format since it introduces an implicit dependency on
moment
, so I added only 5 predefined formats for now:We can allow custom formats later if see that it's required.
Compat with legacy: LP provides
timestamp
configuration only and always logs in format ofHH:mm:ss.SSS
.pattern: 'json'
The current format is identical to elasticsearch ESJsonLayout
yyyy-MM-dd'T'HH:mm:ss,SSSZZ
, but uses.
separator to be closer tomoment().toISOString()
https://github.com/elastic/elasticsearch/blob/a4a79670f85d5c135c1ad668c387808fffd733f7/server/src/main/java/org/elasticsearch/common/logging/ESJsonLayout.java#L91
LP
@timestamp
format forjson
layout:YYYY-MM-DDTHH:mm:ssZZ
users cannot specify a different format.
Checklist
Delete any items that are not applicable to this PR.
For maintainers