Skip to content

Commit b294a12

Browse files
committed
check optionList in start()
Signed-off-by: ceki <ceki@qos.ch>
1 parent b65040a commit b294a12

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/pattern/EpochConverter.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Logback: the reliable, generic, fast and flexible logging framework.
3-
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
3+
* Copyright (C) 1999-2026, QOS.ch. All rights reserved.
44
*
55
* This program and the accompanying materials are dual-licensed under
66
* either the terms of the Eclipse Public License v1.0 as published by
@@ -15,14 +15,36 @@
1515

1616
import ch.qos.logback.classic.spi.ILoggingEvent;
1717

18+
/**
19+
* The EpochConverter class extends the ClassicConverter to handle the conversion of logging event
20+
* timestamps into epoch time. This class allows control over whether the output epoch time is
21+
* represented in milliseconds (default) or seconds.
22+
*
23+
* @since 1.5.25
24+
*/
1825
public class EpochConverter extends ClassicConverter {
26+
27+
// assume output in milliseconds by default
28+
boolean inUnitsOfSeconds = false;
29+
1930
@Override
20-
public String convert(ILoggingEvent event) {
21-
String millisOrSeconds = getFirstOption();
22-
if ("seconds".equals(millisOrSeconds) ) {
23-
return "" + (event.getTimeStamp() / 1000L);
31+
public void start() {
32+
String millisOrSecondsStr = getFirstOption();
33+
if ("seconds".equalsIgnoreCase(millisOrSecondsStr)) {
34+
inUnitsOfSeconds = true;
2435
}
2536

26-
return "" + event.getTimeStamp();
37+
super.start();
38+
39+
}
40+
41+
@Override
42+
public String convert(ILoggingEvent event) {
43+
44+
if(inUnitsOfSeconds) {
45+
return Long.toString(event.getTimeStamp() / 1000);
46+
} else {
47+
return Long.toString(event.getTimeStamp());
48+
}
2749
}
2850
}

logback-classic/src/test/java/ch/qos/logback/classic/pattern/EpochConverterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class EpochConverterTest {
2727
@Test
2828
public void withDefaultConfiguration() {
2929
ec.setOptionList(null);
30+
ec.start();
3031
LoggingEvent le = new LoggingEvent();
3132
Instant instant = Instant.parse("2026-01-15T10:15:30Z");
3233
instant = instant.plusMillis(321);
@@ -39,6 +40,7 @@ public void withDefaultConfiguration() {
3940
@Test
4041
public void withSecondsConfiguration() {
4142
ec.setOptionList(Collections.singletonList("seconds"));
43+
ec.start();
4244
LoggingEvent le = new LoggingEvent();
4345
Instant instant = Instant.parse("2026-01-15T10:15:30Z");
4446
instant = instant.plusMillis(321); // millis should be ignored
@@ -51,6 +53,7 @@ public void withSecondsConfiguration() {
5153
@Test
5254
public void withUnknownNonsenseConfiguration() {
5355
ec.setOptionList(Collections.singletonList("nonsense"));
56+
ec.start();
5457
LoggingEvent le = new LoggingEvent();
5558
Instant instant = Instant.parse("2026-01-15T10:15:30Z");
5659
instant = instant.plusMillis(321);

0 commit comments

Comments
 (0)