-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Feature/2.x/enumerating and exposing named patters #3789
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
Open
ashr123
wants to merge
22
commits into
apache:2.x
Choose a base branch
from
ashr123:feature/2.x/enumerating-and-exposing-named-patters
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b552557
Created an enum for choosing a named pattern.
ashr123 9d330d4
Applied `spotless:apply`
ashr123 8f427a0
Added changelog entry
ashr123 b19d830
No need for (another) default hard-coded named pattern
ashr123 3df9d88
Fixed small bug
ashr123 1966a31
fixes some of @vy comments
ashr123 3418e4c
changed enum to `NamedDatePattern`
ashr123 9758413
now taking all named patterns automatically
ashr123 0ef914b
Improve style and cross references in docs
vy 1f4916a
Fix XML typo
vy 4ce71a1
Improve changelog description
vy fe83085
Merge branch '2.x' into feature/2.x/enumerating-and-exposing-named-pa…
vy 890ba62
Apply suggestion from @ppkarwasz
ashr123 4ec6c39
implemented @ppkarwasz & @vy chamges regarding `FixedDateFormat.Fixed…
ashr123 6b311cd
Added a JavaDoc for `NamedInstantPattern#getPattern`
9657563
fix: expose legacy `FixedFormat` patterns via `NamedInstantPattern`
ppkarwasz 3799799
fix: precision of `InstantPatternLegacyFormatter`
ppkarwasz e3c6f6d
fix: dynamically compute legacy date format pattern
ppkarwasz 41f4b1b
fix: missing Javadoc heading
ppkarwasz bb8b41f
fix: hardcode legacy patterns
ppkarwasz 8c84355
fix: revert fix for #3816
ppkarwasz 24ddd0e
fix: Do not test precision of NamedInstantPattern
ppkarwasz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ore-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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. | ||
*/ | ||
package org.apache.logging.log4j.core.pattern; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Instant; | ||
import org.apache.logging.log4j.core.time.MutableInstant; | ||
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternFormatter; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
class NamedInstantPatternTest { | ||
|
||
@ParameterizedTest | ||
@EnumSource(NamedInstantPattern.class) | ||
void compatibilityOfLegacyPattern(NamedInstantPattern namedPattern) { | ||
InstantPatternFormatter legacyFormatter = InstantPatternFormatter.newBuilder() | ||
.setPattern(namedPattern.getLegacyPattern()) | ||
.setLegacyFormattersEnabled(true) | ||
.build(); | ||
InstantPatternFormatter formatter = InstantPatternFormatter.newBuilder() | ||
.setPattern(namedPattern.getPattern()) | ||
.setLegacyFormattersEnabled(false) | ||
.build(); | ||
Instant javaTimeInstant = Instant.now(); | ||
MutableInstant instant = new MutableInstant(); | ||
instant.initFromEpochSecond(javaTimeInstant.getEpochSecond(), javaTimeInstant.getNano()); | ||
assertThat(legacyFormatter.format(instant)).isEqualTo(formatter.format(instant)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/NamedInstantPattern.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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. | ||
*/ | ||
package org.apache.logging.log4j.core.pattern; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
|
||
/** | ||
* Represents named date & time patterns for formatting log timestamps. | ||
* | ||
* @see DatePatternConverter | ||
* @since 2.26.0 | ||
*/ | ||
@NullMarked | ||
public enum NamedInstantPattern { | ||
ABSOLUTE("HH:mm:ss,SSS"), | ||
|
||
ABSOLUTE_MICROS("HH:mm:ss,SSSSSS", "HH:mm:ss,nnnnnn"), | ||
|
||
ABSOLUTE_NANOS("HH:mm:ss,SSSSSSSSS", "HH:mm:ss,nnnnnnnnn"), | ||
|
||
ABSOLUTE_PERIOD("HH:mm:ss.SSS"), | ||
|
||
COMPACT("yyyyMMddHHmmssSSS"), | ||
|
||
DATE("dd MMM yyyy HH:mm:ss,SSS"), | ||
|
||
DATE_PERIOD("dd MMM yyyy HH:mm:ss.SSS"), | ||
|
||
DEFAULT("yyyy-MM-dd HH:mm:ss,SSS"), | ||
|
||
DEFAULT_MICROS("yyyy-MM-dd HH:mm:ss,SSSSSS", "yyyy-MM-dd HH:mm:ss,nnnnnn"), | ||
|
||
DEFAULT_NANOS("yyyy-MM-dd HH:mm:ss,SSSSSSSSS", "yyyy-MM-dd HH:mm:ss,nnnnnnnnn"), | ||
|
||
DEFAULT_PERIOD("yyyy-MM-dd HH:mm:ss.SSS"), | ||
|
||
ISO8601_BASIC("yyyyMMdd'T'HHmmss,SSS"), | ||
|
||
ISO8601_BASIC_PERIOD("yyyyMMdd'T'HHmmss.SSS"), | ||
|
||
ISO8601("yyyy-MM-dd'T'HH:mm:ss,SSS"), | ||
|
||
ISO8601_OFFSET_DATE_TIME_HH("yyyy-MM-dd'T'HH:mm:ss,SSSx", "yyyy-MM-dd'T'HH:mm:ss,SSSX"), | ||
|
||
ISO8601_OFFSET_DATE_TIME_HHMM("yyyy-MM-dd'T'HH:mm:ss,SSSxx", "yyyy-MM-dd'T'HH:mm:ss,SSSXX"), | ||
|
||
ISO8601_OFFSET_DATE_TIME_HHCMM("yyyy-MM-dd'T'HH:mm:ss,SSSxxx", "yyyy-MM-dd'T'HH:mm:ss,SSSXXX"), | ||
|
||
ISO8601_PERIOD("yyyy-MM-dd'T'HH:mm:ss.SSS"), | ||
|
||
ISO8601_PERIOD_MICROS("yyyy-MM-dd'T'HH:mm:ss.SSSSSS", "yyyy-MM-dd'T'HH:mm:ss.nnnnnn"), | ||
|
||
US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS"), | ||
|
||
US_MONTH_DAY_YEAR4_TIME("dd/MM/yyyy HH:mm:ss.SSS"); | ||
|
||
private final String pattern; | ||
private final String legacyPattern; | ||
|
||
NamedInstantPattern(String pattern) { | ||
this(pattern, pattern); | ||
} | ||
|
||
NamedInstantPattern(String pattern, String legacyPattern) { | ||
this.pattern = pattern; | ||
this.legacyPattern = legacyPattern; | ||
} | ||
|
||
/** | ||
* Returns the date-time pattern string compatible with {@link java.time.format.DateTimeFormatter} | ||
* that is associated with this named pattern. | ||
* | ||
* @return the date-time pattern string for use with {@code DateTimeFormatter} | ||
*/ | ||
public String getPattern() { | ||
ashr123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return pattern; | ||
} | ||
|
||
/** | ||
* Returns the legacy {@link org.apache.logging.log4j.core.util.datetime.FixedDateFormat} pattern | ||
* associated with this named pattern. | ||
* <p> | ||
* If legacy formatters are enabled, output is produced for | ||
* {@code FixedDateFormat} and {@code FastDateFormat}. To convert the {@code DateTimeFormatter} | ||
* to its legacy counterpart, the following transformations need to be applied: | ||
* </p> | ||
* <table> | ||
* <caption>Pattern Differences</caption> | ||
* <thead> | ||
* <tr> | ||
* <th></th> | ||
* <th>Microseconds</th> | ||
* <th>Nanoseconds</th> | ||
* <th>Time-zone</th> | ||
* </tr> | ||
* </thead> | ||
* <tbody> | ||
* <tr> | ||
* <td>Legacy formatter directive</td> | ||
* <td><code>nnnnnn</code></td> | ||
* <td><code>nnnnnnnnn</code></td> | ||
* <td><code>X</code>, <code>XX</code>, <code>XXX</code></td> | ||
* </tr> | ||
* <tr> | ||
* <td>{@code DateTimeFormatter} directive</td> | ||
* <td><code>SSSSSS</code></td> | ||
* <td><code>SSSSSSSSS</code></td> | ||
* <td><code>x</code>, <code>xx</code>, <code>xxx</code></td> | ||
* </tr> | ||
* </tbody> | ||
* </table> | ||
* <h4>Rationale</h4> | ||
* <ul> | ||
* <li> | ||
* <p> | ||
* Legacy formatters are largely compatible with the {@code SimpleDateFormat} specification, | ||
* but introduce a custom {@code n} pattern letter, unique to Log4j, to represent sub-millisecond precision. | ||
* This {@code n} is not part of the standard {@code SimpleDateFormat}. | ||
* </p> | ||
* <p> | ||
* In legacy formatters, repeating {@code n} increases the precision, similar to how repeated {@code S} | ||
* is used for fractional seconds in {@code DateTimeFormatter}. | ||
* </p> | ||
* <p> | ||
* In contrast, {@code DateTimeFormatter} interprets {@code n} as nano-of-second. | ||
* In Java 17, both {@code n} and {@code N} always output nanosecond precision, | ||
* regardless of the number of pattern letters. | ||
* </p> | ||
* </li> | ||
* <li> | ||
* <p> | ||
* Legacy formatters use <code>X</code>, <code>XX</code>, and <code>XXX</code> to format time zones as | ||
* <code>+00</code>, <code>+0000</code>, or <code>+00:00</code>, following {@code SimpleDateFormat} conventions. | ||
* In contrast, {@code DateTimeFormatter} outputs <code>Z</code> for zero-offset when using <code>X</code>. | ||
* To ensure numeric output for zero-offset (e.g., <code>+00</code>), | ||
* we use <code>x</code>, <code>xx</code>, or <code>xxx</code> instead. | ||
* </p> | ||
* </li> | ||
* </ul> | ||
* | ||
* @return the legacy pattern string as used in | ||
* {@link org.apache.logging.log4j.core.util.datetime.FixedDateFormat.FixedFormat} | ||
*/ | ||
String getLegacyPattern() { | ||
return legacyPattern; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/changelog/.2.x.x/exported_named_patterns_into_public_enum.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://logging.apache.org/xml/ns" | ||
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
type="added"> | ||
<issue id="3789" link="https://github.com/apache/logging-log4j2/pull/3789"/> | ||
<description format="asciidoc">Add and export `org.apache.logging.log4j.core.pattern.NamedInstantPattern` enabling users to programmatically access named date & time patterns supported by Pattern Layout</description> | ||
</entry> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.