-
Notifications
You must be signed in to change notification settings - Fork 41.2k
logstash structured logging to support timestamp customization #42980
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
Comments
I think you can do this via a custom logging.structured.format.console=logstash
logging.structured.json.customizer=task.gh42980.LogstashTimestampCustomizer package task.gh42980;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.springframework.boot.json.JsonWriter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class LogstashTimestampCustomizer implements StructureLoggingJsonMembersCustomizer<ILoggingEvent> {
@Override
public void customize(JsonWriter.Members<ILoggingEvent> members) {
members.applyingValueProcessor((path, value) -> {
if ("@timestamp".equals(path.name())) {
OffsetDateTime time = OffsetDateTime.parse(value.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(time.truncatedTo(ChronoUnit.SECONDS));
}
return value;
});
}
} {"@timestamp":"2024-11-04T11:19:06+02:00","@version":"1","message":"Starting service [Tomcat]","logger_name":"org.apache.catalina.core.StandardService","thread_name":"main","level":"INFO","level_value":20000}
|
IMO, it would be convenient to have something like this additionally to public class LogstashTimestampCustomizer implements StructureLoggingJsonMembersCustomizer<ILoggingEvent> {
@Override
public void customize(JsonWriter.Members<ILoggingEvent> members) {
members.addOrReplace("@timestamp", (event) -> event.getInstant().truncatedTo(ChronoUnit.SECONDS));
}
} |
"Too much" for you personally or did you find a specification we violate? @philwebb : WDYT about #42980 (comment)? |
Yes, but I think this should be configurable through properties.
relative to default spring-boot console output. original comment updated |
With regards to For the customizer, you can use the public class LogstashTimestampCustomizer implements StructureLoggingJsonMembersCustomizer<ILoggingEvent> {
private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
@Override
public void customize(JsonWriter.Members<ILoggingEvent> members) {
members.applyingValueProcessor(ValueProcessor.of(this::truncate).whenHasPath("@timestamp"));
}
private String truncate(String value) {
OffsetDateTime time = OffsetDateTime.parse(value, formatter);
return formatter.format(time.truncatedTo(ChronoUnit.SECONDS));
}
} |
There's a workaround if you really want to change the timestamp format. Given that the default config of https://github.com/logfellow/logstash-logback-encoder also uses a non-truncated date format, I don't think we should do anything here. |
Uh oh!
There was an error while loading. Please reload this page.
LOG_DATEFORMAT_PATTERN
propertyLOG_DATEFORMAT_PATTERN
default:yyyy-MM-dd'T'HH:mm:ss.SSSXXX
As is now, logstash timestamp has too much sub-second precision, relative to
LOG_DATEFORMAT_PATTERN
:logstash timestamp on Java17+:
2024-11-04T08:55:57.001407539-08:00
LOG_DATEFORMAT_PATTERN:
2024-11-04T09:00:40.584-08:00
logstash doc claims their default is
yyyy-MM-dd'T'HH:mm:ss.SSS
e.g.2019-11-03T10:15:30.123+01:00
but I don't think they realize that Java9+ produces greater than 3 digit sub-second precision.The text was updated successfully, but these errors were encountered: