|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package software.amazon.lambda.powertools.logging.internal; |
| 16 | + |
| 17 | +import static software.amazon.lambda.powertools.logging.internal.LoggingConstants.LAMBDA_LOG_FORMAT; |
| 18 | +import static software.amazon.lambda.powertools.logging.internal.LoggingConstants.LOG_DATE_RFC3339_FORMAT; |
| 19 | + |
| 20 | +import java.util.Locale; |
| 21 | +import java.util.TimeZone; |
| 22 | +import org.apache.logging.log4j.core.LogEvent; |
| 23 | +import org.apache.logging.log4j.core.time.MutableInstant; |
| 24 | +import org.apache.logging.log4j.layout.template.json.JsonTemplateLayoutDefaults; |
| 25 | +import org.apache.logging.log4j.layout.template.json.resolver.EventResolver; |
| 26 | +import org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverConfig; |
| 27 | +import org.apache.logging.log4j.layout.template.json.util.InstantFormatter; |
| 28 | +import org.apache.logging.log4j.layout.template.json.util.JsonWriter; |
| 29 | + |
| 30 | +/** |
| 31 | + * Default timestamp used by log4j is not RFC3339, which is used by Lambda internally to filter logs. |
| 32 | + * When `AWS_LAMBDA_LOG_FORMAT` is set to JSON (i.e. using Lambda logging configuration), we should use the appropriate pattern, |
| 33 | + * otherwise logs with invalid date format are considered as INFO. |
| 34 | + * Inspired from org.apache.logging.log4j.layout.template.json.resolver.TimestampResolver |
| 35 | + * |
| 36 | + * TODO: remove in v2 an replace with the good pattern in LambdaJsonLayout.json |
| 37 | + */ |
| 38 | +public class LambdaTimestampResolver implements EventResolver { |
| 39 | + |
| 40 | + private final EventResolver internalResolver; |
| 41 | + |
| 42 | + public LambdaTimestampResolver(final TemplateResolverConfig config) { |
| 43 | + final PatternResolverContext patternResolverContext = |
| 44 | + PatternResolverContext.fromConfig(config); |
| 45 | + internalResolver = new PatternResolver(patternResolverContext); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void resolve(LogEvent value, JsonWriter jsonWriter) { |
| 50 | + internalResolver.resolve(value, jsonWriter); |
| 51 | + } |
| 52 | + |
| 53 | + static String getName() { |
| 54 | + return "lambda-timestamp"; |
| 55 | + } |
| 56 | + |
| 57 | + private static final class PatternResolverContext { |
| 58 | + |
| 59 | + public static final String PATTERN = "pattern"; |
| 60 | + private final InstantFormatter formatter; |
| 61 | + |
| 62 | + private final StringBuilder lastFormattedInstantBuffer = new StringBuilder(); |
| 63 | + |
| 64 | + private final MutableInstant lastFormattedInstant = new MutableInstant(); |
| 65 | + |
| 66 | + private PatternResolverContext( |
| 67 | + final String pattern, |
| 68 | + final TimeZone timeZone, |
| 69 | + final Locale locale) { |
| 70 | + this.formatter = InstantFormatter |
| 71 | + .newBuilder() |
| 72 | + .setPattern(pattern) |
| 73 | + .setTimeZone(timeZone) |
| 74 | + .setLocale(locale) |
| 75 | + .build(); |
| 76 | + lastFormattedInstant.initFromEpochSecond(-1, 0); |
| 77 | + } |
| 78 | + |
| 79 | + private static PatternResolverContext fromConfig( |
| 80 | + final TemplateResolverConfig config) { |
| 81 | + final String pattern = readPattern(config); |
| 82 | + final TimeZone timeZone = readTimeZone(config); |
| 83 | + final Locale locale = config.getLocale(new String[]{PATTERN, "locale"}); |
| 84 | + return new PatternResolverContext(pattern, timeZone, locale); |
| 85 | + } |
| 86 | + |
| 87 | + private static String readPattern(final TemplateResolverConfig config) { |
| 88 | + final String format = config.getString(new String[]{PATTERN, "format"}); |
| 89 | + return format != null |
| 90 | + ? format |
| 91 | + : getLambdaTimestampFormatOrDefault(); |
| 92 | + } |
| 93 | + |
| 94 | + private static String getLambdaTimestampFormatOrDefault() { |
| 95 | + return "JSON".equals(LAMBDA_LOG_FORMAT) ? LOG_DATE_RFC3339_FORMAT : |
| 96 | + JsonTemplateLayoutDefaults.getTimestampFormatPattern(); |
| 97 | + } |
| 98 | + |
| 99 | + private static TimeZone readTimeZone(final TemplateResolverConfig config) { |
| 100 | + final String timeZoneId = config.getString(new String[]{PATTERN, "timeZone"}); |
| 101 | + if (timeZoneId == null) { |
| 102 | + return JsonTemplateLayoutDefaults.getTimeZone(); |
| 103 | + } |
| 104 | + boolean found = false; |
| 105 | + for (final String availableTimeZone : TimeZone.getAvailableIDs()) { |
| 106 | + if (availableTimeZone.equalsIgnoreCase(timeZoneId)) { |
| 107 | + found = true; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + if (!found) { |
| 112 | + throw new IllegalArgumentException( |
| 113 | + "invalid timestamp time zone: " + config); |
| 114 | + } |
| 115 | + return TimeZone.getTimeZone(timeZoneId); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private static final class PatternResolver implements EventResolver { |
| 121 | + |
| 122 | + private final PatternResolverContext patternResolverContext; |
| 123 | + |
| 124 | + private PatternResolver(final PatternResolverContext patternResolverContext) { |
| 125 | + this.patternResolverContext = patternResolverContext; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public synchronized void resolve( |
| 130 | + final LogEvent logEvent, |
| 131 | + final JsonWriter jsonWriter) { |
| 132 | + |
| 133 | + // Format timestamp if it doesn't match the last cached one. |
| 134 | + final boolean instantMatching = patternResolverContext.formatter.isInstantMatching( |
| 135 | + patternResolverContext.lastFormattedInstant, |
| 136 | + logEvent.getInstant()); |
| 137 | + if (!instantMatching) { |
| 138 | + |
| 139 | + // Format the timestamp. |
| 140 | + patternResolverContext.lastFormattedInstantBuffer.setLength(0); |
| 141 | + patternResolverContext.lastFormattedInstant.initFrom(logEvent.getInstant()); |
| 142 | + patternResolverContext.formatter.format( |
| 143 | + patternResolverContext.lastFormattedInstant, |
| 144 | + patternResolverContext.lastFormattedInstantBuffer); |
| 145 | + |
| 146 | + // Write the formatted timestamp. |
| 147 | + final StringBuilder jsonWriterStringBuilder = jsonWriter.getStringBuilder(); |
| 148 | + final int startIndex = jsonWriterStringBuilder.length(); |
| 149 | + jsonWriter.writeString(patternResolverContext.lastFormattedInstantBuffer); |
| 150 | + |
| 151 | + // Cache the written value. |
| 152 | + patternResolverContext.lastFormattedInstantBuffer.setLength(0); |
| 153 | + patternResolverContext.lastFormattedInstantBuffer.append( |
| 154 | + jsonWriterStringBuilder, |
| 155 | + startIndex, |
| 156 | + jsonWriterStringBuilder.length()); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + // Write the cached formatted timestamp. |
| 161 | + else { |
| 162 | + jsonWriter.writeRawString( |
| 163 | + patternResolverContext.lastFormattedInstantBuffer); |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments