Skip to content

Commit

Permalink
add STRICT shortcut for ISO8601 date pattern, fixes LOGBACK-262
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 14, 2024
1 parent 94b67db commit 1b7fe94
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class DateConverter extends ClassicConverter {
public void start() {

String datePattern = getFirstOption();

if (datePattern == null) {
datePattern = CoreConstants.ISO8601_PATTERN;
}
Expand All @@ -38,6 +39,10 @@ public void start() {
datePattern = CoreConstants.ISO8601_PATTERN;
}

if (datePattern.equals(CoreConstants.STRICT_STR)) {
datePattern = CoreConstants.STRICT_ISO8601_PATTERN;
}

List<String> optionList = getOptionList();
ZoneId zoneId = null;
// if the option list contains a TZ option, then set it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
*/
package ch.qos.logback.classic.pattern;

import ch.qos.logback.classic.ClassicConstants;
import ch.qos.logback.classic.ClassicTestConstants;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.*;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
Expand All @@ -31,21 +27,20 @@
import org.junit.jupiter.api.Test;
import org.slf4j.MarkerFactory;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

public class ConverterTest {

LoggerContext loggerContext = new LoggerContext();
LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
Logger logger = loggerContext.getLogger(ConverterTest.class);
LoggingEvent le;
List<String> optionList = new ArrayList<String>();
//List<String> optionList = new ArrayList<String>();

// The LoggingEvent is massaged with an FCQN of FormattingConverter. This
// forces the returned caller information to match the caller stack for
Expand Down Expand Up @@ -77,7 +72,7 @@ public void testLineOfCaller() {
StringBuilder buf = new StringBuilder();
converter.write(buf, le);
// the number below should be the line number of the previous line
assertEquals("78", buf.toString());
assertEquals("73", buf.toString());
}
}

Expand Down Expand Up @@ -134,8 +129,7 @@ public void testException() {

{
DynamicConverter<ILoggingEvent> converter = new ThrowableProxyConverter();
this.optionList.add("3");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("3"));
StringBuilder buf = new StringBuilder();
converter.write(buf, le);
}
Expand All @@ -152,8 +146,7 @@ public void testLogger() {

{
ClassicConverter converter = new LoggerConverter();
this.optionList.add("20");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("20"));
converter.start();
StringBuilder buf = new StringBuilder();
converter.write(buf, le);
Expand All @@ -162,9 +155,7 @@ public void testLogger() {

{
DynamicConverter<ILoggingEvent> converter = new LoggerConverter();
this.optionList.clear();
this.optionList.add("0");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("0"));
converter.start();
StringBuilder buf = new StringBuilder();
converter.write(buf, le);
Expand All @@ -175,8 +166,7 @@ public void testLogger() {
@Test
public void testVeryLongLoggerName() {
ClassicConverter converter = new LoggerConverter();
this.optionList.add("5");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("5"));
converter.start();
StringBuilder buf = new StringBuilder();

Expand Down Expand Up @@ -239,9 +229,7 @@ public void testCallerData() {

{
DynamicConverter<ILoggingEvent> converter = new CallerDataConverter();
this.optionList.add("2");
this.optionList.add("XXX");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("2", "XXX"));
converter.start();

StringBuilder buf = new StringBuilder();
Expand All @@ -255,11 +243,7 @@ public void testCallerData() {

{
DynamicConverter<ILoggingEvent> converter = new CallerDataConverter();
this.optionList.clear();
this.optionList.add("2");
this.optionList.add("XXX");
this.optionList.add("*");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("2", "XXX", "*"));
converter.start();

StringBuilder buf = new StringBuilder();
Expand All @@ -272,11 +256,7 @@ public void testCallerData() {
}
{
DynamicConverter<ILoggingEvent> converter = new CallerDataConverter();
this.optionList.clear();
this.optionList.add("2");
this.optionList.add("XXX");
this.optionList.add("+");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("2", "XXX", "*"));
converter.start();

StringBuilder buf = new StringBuilder();
Expand All @@ -290,11 +270,7 @@ public void testCallerData() {

{
DynamicConverter<ILoggingEvent> converter = new CallerDataConverter();
this.optionList.clear();
this.optionList.add("2");
this.optionList.add("XXX");
this.optionList.add("*");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("2", "XXX", "*"));
converter.start();

StringBuilder buf = new StringBuilder();
Expand All @@ -307,16 +283,10 @@ public void testCallerData() {

{
DynamicConverter<ILoggingEvent> converter = new CallerDataConverter();
this.optionList.clear();


boolean jdk18 = EnvUtil.isJDK18OrHigher();
// jdk 18EA creates a different stack trace
if(jdk18) {
this.optionList.add("2..3");
} else {
this.optionList.add("4..5");
}
converter.setOptionList(this.optionList);
converter.setOptionList(jdk18 ? List.of("2..3") : List.of("4..5"));
converter.start();

StringBuilder buf = new StringBuilder();
Expand Down Expand Up @@ -352,9 +322,7 @@ public void testRelativeTime() throws Exception {
@Test
public void testSyslogStart() throws Exception {
DynamicConverter<ILoggingEvent> converter = new SyslogStartConverter();
this.optionList.clear();
this.optionList.add("MAIL");
converter.setOptionList(this.optionList);
converter.setOptionList(List.of("MAIL"));
converter.start();

ILoggingEvent event = makeLoggingEvent(null);
Expand All @@ -371,9 +339,7 @@ public void testMDCConverter() throws Exception {
logbackMDCAdapter.clear();
logbackMDCAdapter.put("someKey", "someValue");
MDCConverter converter = new MDCConverter();
this.optionList.clear();
this.optionList.add("someKey");
converter.setOptionList(optionList);
converter.setOptionList(List.of("someKey"));
converter.start();

ILoggingEvent event = makeLoggingEvent(null);
Expand Down Expand Up @@ -401,9 +367,7 @@ public void contextNameConverter() {
public void contextProperty() {
PropertyConverter converter = new PropertyConverter();
converter.setContext(loggerContext);
List<String> ol = new ArrayList<String>();
ol.add("k");
converter.setOptionList(ol);
converter.setOptionList(List.of("k"));
converter.start();
loggerContext.setName("aValue");
loggerContext.putProperty("k", "v");
Expand All @@ -427,4 +391,30 @@ public void testSequenceNumber() {
assertEquals("123", converter.convert(event));
StatusPrinter.print(loggerContext);
}

@Test
void dateConverterTest() {
dateConverterChecker(List.of("STRICT", "GMT"), "2024-08-14T15:29:25,956");
dateConverterChecker(List.of("ISO8601", "GMT"), "2024-08-14 15:29:25,956");
dateConverterChecker(List.of("ISO8601", "UTC"), "2024-08-14 15:29:25,956");
dateConverterChecker(List.of("yyyy-MM-EE", "UTC", "fr-CH"), "2024-08-mer.");

}

void dateConverterChecker(List<String> options, String expected) {
DateConverter dateConverter = new DateConverter();
dateConverter.setOptionList(options) ;
dateConverter.setContext(loggerContext);
dateConverter.start();

assertTrue(dateConverter.isStarted());
LoggingEvent event = makeLoggingEvent(null);

// 2024-08-14T1Z:29:25,956 GMT
long millis = 1_723_649_365_956L; //System.currentTimeMillis();
Instant now = Instant.ofEpochMilli(millis);
event.setInstant(now);
String result = dateConverter.convert(event);
assertEquals(expected, result);
}
}
13 changes: 13 additions & 0 deletions logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public class CoreConstants {
public static final String ISO8601_STR = "ISO8601";
public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";

/**
* Keyword for setting a strict ISO8601 pattern which includes 'T' between the date and the time
*
* @since 1.5.7
*/
public static final String STRICT_STR = "STRICT";
/**
* Strict ISO8601 pattern which includes 'T' between the date and the time
* @since 15.7
*/
public static final String STRICT_ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss,SSS";


public static final String FILE_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HHmm";
public static final String DAILY_DATE_PATTERN = "yyyy-MM-dd";

Expand Down

0 comments on commit 1b7fe94

Please sign in to comment.