-
Notifications
You must be signed in to change notification settings - Fork 109
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
Allow DateTimeFormatter constants to be used for the date pattern. #373
Open
jamezp
wants to merge
1
commit into
jboss-logging:main
Choose a base branch
from
jamezp:date-format
base: main
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
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 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
77 changes: 77 additions & 0 deletions
77
src/main/java/org/jboss/logmanager/formatters/StandardDateFormat.java
This file contains 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,77 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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.jboss.logmanager.formatters; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Standard date formats. | ||
* | ||
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a> | ||
*/ | ||
enum StandardDateFormat { | ||
/** | ||
* Format: {@code HH:mm:ss,SSS} | ||
*/ | ||
ABSOLUTE("HH:mm:ss,SSS"), | ||
/** | ||
* Format: {@code yyyyMMddHHmmssSSS} | ||
*/ | ||
COMPACT("yyyyMMddHHmmssSSS"), | ||
/** | ||
* Format: {@code yyyy-MM-dd HH:mm:ss,SSS} | ||
*/ | ||
DEFAULT("yyyy-MM-dd HH:mm:ss,SSS"), | ||
/** | ||
* Format: {@code yyyy-MM-dd'T'HH:mm:ss,SSS} | ||
*/ | ||
ISO8601("yyyy-MM-dd'T'HH:mm:ss,SSS"), | ||
; | ||
|
||
private final DateTimeFormatter formatter; | ||
private final String pattern; | ||
|
||
StandardDateFormat(final String pattern) { | ||
this.formatter = DateTimeFormatter.ofPattern(pattern); | ||
this.pattern = pattern; | ||
} | ||
|
||
/** | ||
* Resolves the requested date format based on the pattern. If the pattern is {@code null}, then the {@link #DEFAULT} | ||
* pattern is used. If the pattern matches one of the constants, that format will be returned. Otherwise, the | ||
* pattern is assumed to be a valid {@link DateTimeFormatter#ofPattern(String)} format pattern}. | ||
* | ||
* @param pattern the pattern to return the formatter for | ||
* | ||
* @return the formatter used to format timestamps | ||
*/ | ||
static DateTimeFormatter resolve(final String pattern) { | ||
if (pattern == null) { | ||
return DEFAULT.formatter; | ||
} | ||
for (StandardDateFormat constant : values()) { | ||
if (pattern.toUpperCase(Locale.ROOT).equals(constant.name()) || pattern.equals(constant.pattern)) { | ||
return constant.formatter; | ||
} | ||
} | ||
return DateTimeFormatter.ofPattern(pattern); | ||
} | ||
} |
This file contains 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
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.
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.
What if instead of the enum, you did something along these lines in (half-Java, half-pseudocode):
This keeps the code minimal and simple, is guaranteed to be compatible with future
DateTimeFormatter
utilizations of#
, and is forward-compatible as well in case we want to support fancier things like putting the format name in mid-string or whatever.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.
Do you mean the text format would be something like
%d{#ISO_DATE}
?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.
Exactly. This way we never need to be concerned about syntactic conflicts between constant names and format strings.
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.
On the other hand, that character really has no meaningful use from the user standpoint, with the exception of identifying, by convention, date/time pattern formats from constants — something you can achieve performing proper parsing.
I find it more confusing, in the sense that single characters in that particular string (e.g.:
%date{ISO8601} |- %highlight(%5level) in %cyan(%logger{32}:%line) %magenta([%t - %m%n%ex
) have a particular meaning.Still it's a minor detail, but it amounts.
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.
@x80486 The main reason for the prefix character is most characters are reserved for a
DateTimePattern
. WhileISO_DATE
orISO08601
is likely not going to bean issue, it could be a valid pattern at some point.