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: 
`yyyy-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", "yyyy-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
   <Properties>
     <Property name="myapp.date.format" value="yyyy-MM-dd HH:mm:ss,SSS"/>
   </Properties>
   ```
5. **Reference the system property in your `PatternLayout`**:

   ```xml
   <PatternLayout pattern="%d{${sys:myapp.date.format}} [%t] %-5level %logger - 
%msg%n"/>
   ```

  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

Reply via email to