Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ppkarwasz added a comment to the discussion: Getting current date-time format (or default format) You might consider a different approach to keep the date format consistent between your application output and Log4j logs. Here’s a flexible strategy: 1. **Define a shared default format**, such as the one Log4j uses by default: `-MM-dd HH:mm:ss,SSS`. 2. **Document a system property**, for example `myapp.date.format`, to allow users to override the default format if needed. 3. **Use this property in your application code**: ```java String pattern = System.getProperty("myapp.date.format", "-MM-dd HH:mm:ss,SSS"); ``` 4. **Add the same default value to your Log4j configuration** to ensure both log and app output are in sync: ```xml ``` 5. **Reference the system property in your `PatternLayout`**: ```xml ``` The `${sys:myapp.date.format}` expression will try to find the `myapp.date.format` system property and fallback to `${myapp.date.format}`. This approach gives you consistent date formatting across your application and logs, while also allowing users to customize the format through configuration rather than modifying code. GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601663 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ppkarwasz added a comment to the discussion: Getting current date-time format (or default format) If you chose the `-MM-dd HH:mm:ss,SSS` format because it was one of the few formats optimized for low garbage generation by `FixedDateFormat`, it's worth noting that this limitation no longer applies as of Log4j 2.25.0. We deprecated `FixedDateFormat` primarily because #3121 introduced a new, more consistent timestamp formatting subsystem. This new implementation is also optimized for performance and low garbage generation, but with the added flexibility of supporting **any** `DateTimeFormatter` pattern—including formats like the one used in the `Last-Modified` HTTP header. GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601787 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Support for template names [logging-log4net]
GitHub user martinRocks edited a discussion: Support for template names When will log4net allow the use of template names? For example, a log message like this: ```_log.InfoFormat("Started {time}", DateTime.Now);``` so that the message comes out in Json format like this: ``` { "date": "2025-06-27T12:09:20.6955070-04:00", "level": "INFO", "appname": "App.exe", "logger": "Log", "thread": "1", "ndc": "(null)", "message": { "Format": "Started {time}", "time": [ "2025-06-27T12:09:20.6935074-04:00" ] } } ``` I'm currently using the library log4net.Ext.Json to export the json. If this does work, please tell me how, because when I try it, I get the error: ``` log4net:WARN Exception while rendering format [Started {time}] System.FormatException: Input string was not in a correct format. at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at log4net.Util.SystemStringFormat.StringFormat(IFormatProvider provider, String format, Object[] args) in /home/alle/git/apache/logging-log4net/src/log4net/Util/SystemStringFormat.cs:line 86 ``` Also, I'm using these libraries. log4net, version=3.1.0 log4net.Ext.Json, version=3.0.3 log4net.Ext.Json.Net, version=3.0.3 Newtonsoft.Json, version=13.0.3 GitHub link: https://github.com/apache/logging-log4net/discussions/261 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Support for template names [logging-log4net]
GitHub user FreeAndNil added a comment to the discussion: Support for template names I am not aware of a possible solution to your problem. @gdziadkiewicz do you have an idea? GitHub link: https://github.com/apache/logging-log4net/discussions/261#discussioncomment-13599719 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Support for template names [logging-log4net]
GitHub user martinRocks added a comment to the discussion: Support for template names Thank you for the quick reply. I have created a small demo app for how I have things configured. If I have anything set wrong, please let me know. I hope this helps explain my problem. I am surprised that more people haven't asked for this. Maybe, now that I think about it, there are 2 problems. One, the json output seems incorrect, IE missing the parameter names. And two, the console output throws an error. [Log4netJson.zip](https://github.com/user-attachments/files/20953541/Log4netJson.zip) GitHub link: https://github.com/apache/logging-log4net/discussions/261#discussioncomment-13600373 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern: ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "nn", "SS"), ABSOLUTE_NANOS("HH:mm:ss,SSS", "n", "S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "", ""), COMPACT("MMddHHmmssSSS", "", ""), DATE("dd MMM HH:mm:ss,SSS", "", ""), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "", ""), DEFAULT("-MM-dd HH:mm:ss,SSS", "nn", "SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,SSS", "n", "S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "", ""), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "", ""), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "", ""), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "", ""), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSS", "X", "x"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS", "XX", "xx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS", "XXX", "xxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "", ""), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.", "nn", "SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "", ""), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "", ""); private final String basePattern, compactPostfix, nonCompactPostfix; NamedPattern(String basePattern, String compactPostfix, String nonCompactPostfix) { this.basePattern = basePattern; this.compactPostfix = compactPostfix; this.nonCompactPostfix = nonCompactPostfix; } public String decodeNamedPattern(boolean isCompact) { return basePattern + (isCompact ? compactPostfix : nonCompactPostfix); } } ``` And inside `DatePatternConverter`: ```java static String decodeNamedPattern(final String pattern) { try { return NamedPattern.valueOf(pattern) .decodeNamedPattern(InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED); } catch (IllegalArgumentException e) { return pattern; } } ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600805 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern: ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "nn", "SS"), ABSOLUTE_MICROS("HH:mm:ss,", "nn", "SS"), ABSOLUTE_NANOS("HH:mm:ss,", "n", "S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "", ""), COMPACT("MMddHHmmssSSS", "", ""), DATE("dd MMM HH:mm:ss,SSS", "", ""), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "", ""), DEFAULT("-MM-dd HH:mm:ss,SSS", "", ""), DEFAULT_MICROS("-MM-dd HH:mm:ss,", "nn", "SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,", "n", "S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "", ""), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "", ""), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "", ""), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "", ""), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSS", "X", "x"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS", "XX", "xx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS", "XXX", "xxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "", ""), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.", "nn", "SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "", ""), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "", ""); private final String basePattern, compactPostfix, nonCompactPostfix; NamedPattern(String basePattern, String compactPostfix, String nonCompactPostfix) { this.basePattern = basePattern; this.compactPostfix = compactPostfix; this.nonCompactPostfix = nonCompactPostfix; } public String decodeNamedPattern(boolean isCompact) { return basePattern + (isCompact ? compactPostfix : nonCompactPostfix); } } ``` And inside `DatePatternConverter`: ```java static String decodeNamedPattern(final String pattern) { try { return NamedPattern.valueOf(pattern) .decodeNamedPattern(InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED); } catch (IllegalArgumentException e) { return pattern; } } ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600805 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern: ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "nn", "SS"), ABSOLUTE_MICROS("HH:mm:ss,", "nn", "SS"), ABSOLUTE_NANOS("HH:mm:ss,", "n", "S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "", ""), COMPACT("MMddHHmmssSSS", "", ""), DATE("dd MMM HH:mm:ss,SSS", "", ""), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "", ""), DEFAULT("-MM-dd HH:mm:ss,SSS", "", ""), DEFAULT_MICROS("-MM-dd HH:mm:ss,", "nn", "SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,", "n", "S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "", ""), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "", ""), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "", ""), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "", ""), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSS", "X", "x"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS", "XX", "xx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS", "XXX", "xxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "", ""), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.", "nn", "SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "", ""), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "", ""); private final String basePattern, compactPostfix, nonCompactPostfix; NamedPattern(String basePattern, String compactPostfix, String nonCompactPostfix) { this.basePattern = basePattern; this.compactPostfix = compactPostfix; this.nonCompactPostfix = nonCompactPostfix; } public String decodeNamedPattern(boolean isCompact) { return basePattern + (isCompact ? compactPostfix : nonCompactPostfix); } } ``` And inside `DatePatternConverter`: ```java static String decodeNamedPattern(final String pattern) { try { return NamedPattern.valueOf(pattern) .decodeNamedPattern(InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED); } catch (IllegalArgumentException e) { return pattern; } } ``` Or ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "HH:mm:ss,SSS"), ABSOLUTE_MICROS("HH:mm:ss,nn", "HH:mm:ss,SS"), ABSOLUTE_NANOS("HH:mm:ss,n", "HH:mm:ss,S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "HH:mm:ss.SSS"), COMPACT("MMddHHmmssSSS", "MMddHHmmssSSS"), DATE("dd MMM HH:mm:ss,SSS", "dd MMM HH:mm:ss,SSS"), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "dd MMM HH:mm:ss.SSS"), DEFAULT("-MM-dd HH:mm:ss,SSS", "-MM-dd HH:mm:ss,SSS"), DEFAULT_MICROS("-MM-dd HH:mm:ss,nn", "-MM-dd HH:mm:ss,SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,n", "-MM-dd HH:mm:ss,S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "-MM-dd HH:mm:ss.SSS"), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "MMdd'T'HHmmss,SSS"), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "MMdd'T'HHmmss.SSS"), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "-MM-dd'T'HH:mm:ss,SSS"), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSSX", "-MM-dd'T'HH:mm:ss,SSSx"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXX", "-MM-dd'T'HH:mm:ss,SSSxx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXXX", "-MM-dd'T'HH:mm:ss,SSSxxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "-MM-dd'T'HH:mm:ss.SSS"), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.nn", "-MM-dd'T'HH:mm:ss.SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "dd/MM/yy HH:mm:ss.SSS"), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "dd/MM/ HH:mm:ss.SSS"); private final String compactPattern, nonCompactPattern; NamedPattern2(String compactPattern, String nonCompactPattern) { this.compactPattern = compactPattern; this.nonCompactPattern = nonCompactPattern; } public String getCompactPattern() { return compactPattern; } public String getNonCompactPattern() { return nonCompactPattern; } } ``` And ```java static String decodeNamedPattern(final String pattern) { try { final NamedPattern namedPattern = NamedPattern.valueOf(pattern); return InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED ? namedPattern.getCompactPattern() : namedPattern.getNonCompactPattern(); } catch (IllegalArgumentException e) { return pattern; } } ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600805 This i
Re: [D] Support for template names [logging-log4net]
GitHub user FreeAndNil added a comment to the discussion: Support for template names Thanks for the detailed explanation and the sample app. I believe there might be a misunderstanding regarding how log4net handles message templates. The kind of structured logging you're describing (e.g., using {time} placeholders with named parameters in JSON output) isn't something log4net supports natively. As far as I know, log4net doesn't parse message templates like "Started {time}" in the same way that libraries like Serilog do. Instead, it expects traditional .NET format strings with positional arguments (e.g., {0}, {1}, etc.), which is likely why you're seeing the System.FormatException. If you've seen this working elsewhere or have a reference to how it might be done in log4net, I'd be really interested to see it—maybe there's an extension or approach I'm not aware of. GitHub link: https://github.com/apache/logging-log4net/discussions/261#discussioncomment-13600564 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ppkarwasz added a comment to the discussion: Getting current date-time format (or default format) Thanks for the question! Could you tell us more about your use case? For example, are you trying to introspect the effective Log4j Core configuration at runtime, or are you attempting to parse the output of a `PatternLayout`? If you're aiming to parse logs, keep in mind that most formats produced by `PatternLayout` aren't designed to be reliably machine-parsed. In such cases, we strongly recommend using a structured logging format (like `JsonTemplateLayout`) instead. As for the default datetime pattern, it's currently `-MM-dd HH:mm:ss,SSS`. This pattern has been stable for a long time and is unlikely to change anytime soon due to backward compatibility. We could make the `DatePatternConverter#DEFAULT_PATTERN` public, but referencing it in your own code may not be ideal, though—it’s a compile-time constant. If Log4j Core changes that value in a future version, your compiled code will retain the old value unless you recompile against the updated library. Let us know more about what you're trying to achieve—we're happy to offer more specific guidance! GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600712 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 added a comment to the discussion: Getting current date-time format (or default format) I'm not trying to introspect anything, but I do `println` (in addition to logs) some data I got from `HttpClient` and I'm printing dates (as Last-Modified) with the default pattern cause I want my logs and my output to be format the same way.. Regarding compilation, I create jar-with-dependencies so I' not worrying about that GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600749 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) I'm not trying to introspect anything, but I do `println` (in addition to logs) some data I got from `HttpClient` and I'm printing dates (as Last-Modified) with the default pattern cause I want my logs and my output to be format the same way.. Regarding compilation, I create jar-with-dependencies so I'm not worrying about that GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600749 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 added a comment to the discussion: Getting current date-time format (or default format) by the way, I've noticed that in `org.apache.logging.log4j.core.pattern.DatePatternConverter#decodeNamedPattern`, inside the switch-case `case "DEFAULT"`, you're using it again instead of referencing `DEFAULT_PATTERN` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600771 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 added a comment to the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern? GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600805 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern: ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "nn", "SS"), ABSOLUTE_MICROS("HH:mm:ss,", "nn", "SS"), ABSOLUTE_NANOS("HH:mm:ss,", "n", "S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "", ""), COMPACT("MMddHHmmssSSS", "", ""), DATE("dd MMM HH:mm:ss,SSS", "", ""), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "", ""), DEFAULT("-MM-dd HH:mm:ss,SSS", "", ""), DEFAULT_MICROS("-MM-dd HH:mm:ss,", "nn", "SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,", "n", "S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "", ""), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "", ""), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "", ""), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "", ""), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSS", "X", "x"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS", "XX", "xx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS", "XXX", "xxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "", ""), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.", "nn", "SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "", ""), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "", ""); private final String basePattern, compactPostfix, nonCompactPostfix; NamedPattern(String basePattern, String compactPostfix, String nonCompactPostfix) { this.basePattern = basePattern; this.compactPostfix = compactPostfix; this.nonCompactPostfix = nonCompactPostfix; } public String decodeNamedPattern(boolean isCompact) { return basePattern + (isCompact ? compactPostfix : nonCompactPostfix); } } ``` And inside `DatePatternConverter`: ```java static String decodeNamedPattern(final String pattern) { try { return NamedPattern.valueOf(pattern) .decodeNamedPattern(InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED); } catch (IllegalArgumentException e) { return pattern; } } ``` Or ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "HH:mm:ss,SSS"), ABSOLUTE_MICROS("HH:mm:ss,nn", "HH:mm:ss,SS"), ABSOLUTE_NANOS("HH:mm:ss,n", "HH:mm:ss,S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "HH:mm:ss.SSS"), COMPACT("MMddHHmmssSSS", "MMddHHmmssSSS"), DATE("dd MMM HH:mm:ss,SSS", "dd MMM HH:mm:ss,SSS"), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "dd MMM HH:mm:ss.SSS"), DEFAULT("-MM-dd HH:mm:ss,SSS", "-MM-dd HH:mm:ss,SSS"), DEFAULT_MICROS("-MM-dd HH:mm:ss,nn", "-MM-dd HH:mm:ss,SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,n", "-MM-dd HH:mm:ss,S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "-MM-dd HH:mm:ss.SSS"), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "MMdd'T'HHmmss,SSS"), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "MMdd'T'HHmmss.SSS"), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "-MM-dd'T'HH:mm:ss,SSS"), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSSX", "-MM-dd'T'HH:mm:ss,SSSx"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXX", "-MM-dd'T'HH:mm:ss,SSSxx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXXX", "-MM-dd'T'HH:mm:ss,SSSxxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "-MM-dd'T'HH:mm:ss.SSS"), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.nn", "-MM-dd'T'HH:mm:ss.SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "dd/MM/yy HH:mm:ss.SSS"), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "dd/MM/ HH:mm:ss.SSS"); private final String compactPattern, nonCompactPattern; NamedPattern(String compactPattern, String nonCompactPattern) { this.compactPattern = compactPattern; this.nonCompactPattern = nonCompactPattern; } public String getCompactPattern() { return compactPattern; } public String getNonCompactPattern() { return nonCompactPattern; } } ``` And ```java static String decodeNamedPattern(final String pattern) { try { final NamedPattern namedPattern = NamedPattern.valueOf(pattern); return InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED ? namedPattern.getCompactPattern() : namedPattern.getNonCompactPattern(); } catch (IllegalArgumentException e) { return pattern; } } ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13600805 This is
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ppkarwasz added a comment to the discussion: Getting current date-time format (or default format) > I'm not trying to introspect anything, but I do `println` (in addition to > logs) some data I got from `HttpClient` and I'm printing dates (as > Last-Modified) with the default pattern cause I want my logs and my output to > be format the same way. That’s a nice detail—keeping your output visually consistent with the logs can be quite helpful! That said, have you considered what happens if someone changes the logging pattern or switches from console to file logging? In such cases, your `println` output might no longer match. If consistency is important, it might be more robust to use the logger itself for that kind of output rather than relying on `System.out`. Would you be open to sharing a code snippet? There might already be a built-in feature or a cleaner workaround we can suggest. > In addition, why can't all the named patterns be an enum with 3 fields (base > pattern, compact postfix and non-compact postfix) and a getter with a > `boolean compact` parameter that returns the correct pattern? They could, technically—and you're welcome to propose that in a PR. That said, a few considerations: * I'm not sure there's a strong case for making such an `enum` public. Unless there's a clear user-facing need, we prefer to keep our API surface area minimal * The postfixes aren’t “compact” and “non-compact.” One follows `DateTimeFormatter` syntax (Java 8 time), and the other follows `SimpleDateFormat`/Commons Lang `FastDateFormat` conventions. So the distinction isn’t formatting style—the postfixes give the same result but using different APIs. GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601110 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) Yes, certainly right now I execute the following code: ```java private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern( FixedDateFormat.FixedFormat.DEFAULT.getPattern(), Locale.getDefault(Locale.Category.FORMAT) ) .withZone(ZoneId.systemDefault()); ``` and later on I'm use it as follows: ```java private static void printAlert(long contentLength, TemporalAccessor alertsLastModified, String translatedTitle, String translatedDescription, CharSequence output) { System.out.println("Translated Title: " + translatedTitle + System.lineSeparator() + "Translated Description: " + translatedDescription + System.lineSeparator() + "Content Length: " + contentLength + " bytes" + System.lineSeparator() + "Last Modified Date: " + DATE_TIME_FORMATTER.format(alertsLastModified) + System.lineSeparator() + "Current Date: " + DATE_TIME_FORMATTER.format(Instant.now()) + System.lineSeparator() + output); } ``` So at the end, the output is: > ``` > Translated Title: Rocket and Missile Attack > Translated Description: Enter the Protected Space > Content Length: 156 bytes > Last Modified Date: 2025-06-26 17:42:37,000 > Current Date: 2025-06-26 17:42:39,097 > Translated Areas and Districts: > Gaza Envelope: > 15 seconds: > Yated > ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601169 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 added a comment to the discussion: Getting current date-time format (or default format) Yes, certainly right now I execute the following code: ```java private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern( FixedDateFormat.FixedFormat.DEFAULT.getPattern(), Locale.getDefault(Locale.Category.FORMAT) ) .withZone(ZoneId.systemDefault()); ``` and later on I'm use it as follows: ```java private static void printAlert(long contentLength, TemporalAccessor alertsLastModified, String translatedTitle, String translatedDescription, CharSequence output) { System.out.println("Translated Title: " + translatedTitle + System.lineSeparator() + "Translated Description: " + translatedDescription + System.lineSeparator() + "Content Length: " + contentLength + " bytes" + System.lineSeparator() + "Last Modified Date: " + DATE_TIME_FORMATTER.format(alertsLastModified) + System.lineSeparator() + "Current Date: " + DATE_TIME_FORMATTER.format(Instant.now()) + System.lineSeparator() + output); } ``` So at the end, the output is: > ``` > Translated Title: Rocket and Missile Attack > Translated Description: Enter the Protected Space > Content Length: 156 bytes > Last Modified Date: 2025-06-26 17:42:37,000 > Current Date: 2025-06-26 17:42:39,097 > Translated Areas and Districts: > Gaza Envelope: > 15 seconds: > Yated > ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601169 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) in addition, why can't all the named patterns be an enum with 3 fields (base pattern, compact postfix and non-compact postfix) and a getter with `boolean compact` parameter that returns the correct pattern: ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "nn", "SS"), ABSOLUTE_MICROS("HH:mm:ss,", "nn", "SS"), ABSOLUTE_NANOS("HH:mm:ss,", "n", "S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "", ""), COMPACT("MMddHHmmssSSS", "", ""), DATE("dd MMM HH:mm:ss,SSS", "", ""), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "", ""), DEFAULT("-MM-dd HH:mm:ss,SSS", "", ""), DEFAULT_MICROS("-MM-dd HH:mm:ss,", "nn", "SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,", "n", "S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "", ""), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "", ""), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "", ""), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "", ""), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSS", "X", "x"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS", "XX", "xx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS", "XXX", "xxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "", ""), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.", "nn", "SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "", ""), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "", ""); private final String basePattern, compactPostfix, nonCompactPostfix; NamedPattern(String basePattern, String compactPostfix, String nonCompactPostfix) { this.basePattern = basePattern; this.compactPostfix = compactPostfix; this.nonCompactPostfix = nonCompactPostfix; } public String decodeNamedPattern(boolean isCompact) { return basePattern + (isCompact ? compactPostfix : nonCompactPostfix); } } ``` And inside `DatePatternConverter`: ```java static String decodeNamedPattern(final String pattern) { try { return NamedPattern.valueOf(pattern) .decodeNamedPattern(InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED); } catch (IllegalArgumentException e) { return pattern; } } ``` Or ```java public enum NamedPattern { ABSOLUTE("HH:mm:ss,SSS", "HH:mm:ss,SSS"), ABSOLUTE_MICROS("HH:mm:ss,nn", "HH:mm:ss,SS"), ABSOLUTE_NANOS("HH:mm:ss,n", "HH:mm:ss,S"), ABSOLUTE_PERIOD("HH:mm:ss.SSS", "HH:mm:ss.SSS"), COMPACT("MMddHHmmssSSS", "MMddHHmmssSSS"), DATE("dd MMM HH:mm:ss,SSS", "dd MMM HH:mm:ss,SSS"), DATE_PERIOD("dd MMM HH:mm:ss.SSS", "dd MMM HH:mm:ss.SSS"), DEFAULT("-MM-dd HH:mm:ss,SSS", "-MM-dd HH:mm:ss,SSS"), DEFAULT_MICROS("-MM-dd HH:mm:ss,nn", "-MM-dd HH:mm:ss,SS"), DEFAULT_NANOS("-MM-dd HH:mm:ss,n", "-MM-dd HH:mm:ss,S"), DEFAULT_PERIOD("-MM-dd HH:mm:ss.SSS", "-MM-dd HH:mm:ss.SSS"), ISO8601_BASIC("MMdd'T'HHmmss,SSS", "MMdd'T'HHmmss,SSS"), ISO8601_BASIC_PERIOD("MMdd'T'HHmmss.SSS", "MMdd'T'HHmmss.SSS"), ISO8601("-MM-dd'T'HH:mm:ss,SSS", "-MM-dd'T'HH:mm:ss,SSS"), ISO8601_OFFSET_DATE_TIME_HH("-MM-dd'T'HH:mm:ss,SSSX", "-MM-dd'T'HH:mm:ss,SSSx"), ISO8601_OFFSET_DATE_TIME_HHMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXX", "-MM-dd'T'HH:mm:ss,SSSxx"), ISO8601_OFFSET_DATE_TIME_HHCMM("-MM-dd'T'HH:mm:ss,SSS-MM-dd'T'HH:mm:ss,SSSXXX", "-MM-dd'T'HH:mm:ss,SSSxxx"), ISO8601_PERIOD("-MM-dd'T'HH:mm:ss.SSS", "-MM-dd'T'HH:mm:ss.SSS"), ISO8601_PERIOD_MICROS("-MM-dd'T'HH:mm:ss.nn", "-MM-dd'T'HH:mm:ss.SS"), US_MONTH_DAY_YEAR2_TIME("dd/MM/yy HH:mm:ss.SSS", "dd/MM/yy HH:mm:ss.SSS"), US_MONTH_DAY_YEAR4_TIME("dd/MM/ HH:mm:ss.SSS", "dd/MM/ HH:mm:ss.SSS"); private final String compactPattern, nonCompactPattern; NamedPattern2(String compactPattern, String nonCompactPattern) { this.compactPattern = compactPattern; this.nonCompactPattern = nonCompactPattern; } public String getCompactPattern() { return compactPattern; } public String getNonCompactPattern() { return nonCompactPattern; } } ``` And ```java static String decodeNamedPattern(final String pattern) { try { final NamedPattern namedPattern = NamedPattern.valueOf(pattern); return InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED ? namedPattern.getCompactPattern() : namedPattern.getNonCompactPattern(); } catch (IllegalArgumentException e) { return pattern; } } ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788
Re: [D] Getting current date-time format (or default format) [logging-log4j2]
GitHub user ashr123 edited a comment on the discussion: Getting current date-time format (or default format) Yes, certainly right now I execute the following code: ```java private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern( FixedDateFormat.FixedFormat.DEFAULT.getPattern(), Locale.getDefault(Locale.Category.FORMAT) ) .withZone(ZoneId.systemDefault()); ``` and later on I'm use it as follows: ```java private static void printAlert(long contentLength, TemporalAccessor alertsLastModified, String translatedTitle, String translatedDescription, CharSequence output) { System.out.println("Translated Title: " + translatedTitle + System.lineSeparator() + "Translated Description: " + translatedDescription + System.lineSeparator() + "Content Length: " + contentLength + " bytes" + System.lineSeparator() + "Last Modified Date: " + DATE_TIME_FORMATTER.format(alertsLastModified) + System.lineSeparator() + "Current Date: " + DATE_TIME_FORMATTER.format(Instant.now()) + System.lineSeparator() + output); } ``` So at the end, the output is: > ``` > Translated Title: Rocket and Missile Attack > Translated Description: Enter the Protected Space > Content Length: 156 bytes > Last Modified Date: 2025-06-26 17:42:37,000 > Current Date: 2025-06-26 17:42:39,097 > Translated Areas and Districts: > Gaza Envelope: > 15 seconds: > Yated > ``` Regarding the "compact" naming, I took it from the `decodeNamedPattern` method: ```java final boolean compat = InstantPatternFormatter.LEGACY_FORMATTERS_ENABLED; ... ``` GitHub link: https://github.com/apache/logging-log4j2/discussions/3788#discussioncomment-13601169 This is an automatically sent email for dev@logging.apache.org. To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org