Skip to content

Commit

Permalink
Fix DateConverter to confirm w/ISO8601 (LOGBACK-262)
Browse files Browse the repository at this point in the history
ISO8601 requires the letter "T" as the delimiter between the date and
time fields, but we were incorrectly using a space.
  • Loading branch information
tony19 committed Jun 16, 2014
1 parent c4382b3 commit 12e2917
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
6 changes: 6 additions & 0 deletions logback-classic/pom.xml
Expand Up @@ -142,6 +142,12 @@
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -16,7 +16,7 @@
import ch.qos.logback.core.util.CoreTestConstants;

public class ClassicTestConstants {
final static public String ISO_REGEX = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}";
final static public String ISO_REGEX = "\\d{4}-\\d{2}-\\d{2}T?\\d{2}:\\d{2}:\\d{2}(,\\d{3})?(Z|(\\+-\\d\\d:\\d\\d))?";

This comment has been minimized.

Copy link
@tony19

tony19 Jun 17, 2014

Author Contributor

Yes, indeed.

//pool-1-thread-47
final static public String NAKED_MAIN_REGEX = "([mM]ain|pool-\\d-)([Tt]hread)?(-\\d{1,3})?";

Expand Down
@@ -0,0 +1,82 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2014, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.TimeZone;

import org.junit.Test;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;

/**
* Tests the {@link DateConverter}
*/
public class DateConverterTest {

private final Calendar CAL = getCalendar(2014,1,1,14,28,30,456);
private final String DATE_ISO801_UTC_STR = "2014-01-01T14:28:30,456";

This comment has been minimized.

Copy link
@tony19

tony19 Jun 17, 2014

Author Contributor

Currently, we don't include the time zone, so there wouldn't be a "Z". However, there is discussion about adding the time zone to the format, and if that passes, I'll take care of it here.

private final String DATE_ISO801_AWST_STR = "2014-01-01T22:28:30,456"; // AWST = UTC+8:00
private final long DATE_MS = CAL.getTimeInMillis();
private ILoggingEvent EVENT = getLogEvent();

@Test
public void convertsCustomDateTimeFormat() {
final String dateFormat = "(MM/dd/yy HH:mm)";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
assertEquals(sdf.format(CAL.getTime()), getConverter(dateFormat).convert(EVENT));
}

@Test
public void convertsIso8601WithUtcTzOption() {
assertEquals(DATE_ISO801_UTC_STR, getConverter(CoreConstants.ISO8601_STR, "UTC").convert(EVENT));
}

@Test
public void convertsIso8601WithTzOption() {
assertEquals(DATE_ISO801_AWST_STR, getConverter(CoreConstants.ISO8601_STR, "Australia/Perth").convert(EVENT));
}

private ILoggingEvent getLogEvent() {
ILoggingEvent event = mock(ILoggingEvent.class);
when(event.getTimeStamp()).thenReturn(DATE_MS);
return event;
}

private Calendar getCalendar(int year, int month, int dayOfMonth, int hour, int minute, int second, int millisecond) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, millisecond);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
return cal;
}

private DateConverter getConverter(String... options) {
DateConverter converter = new DateConverter();
converter.setOptionList(Arrays.asList(options));
converter.start();
return converter;
}
}
Expand Up @@ -50,7 +50,7 @@ public class CoreConstants {
public static final String PATTERN_RULE_REGISTRY = "PATTERN_RULE_REGISTRY";

public static final String ISO8601_STR = "ISO8601";
public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
public static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss,SSS";
public static final String DAILY_DATE_PATTERN = "yyyy-MM-dd";

/**
Expand Down
8 changes: 4 additions & 4 deletions logback-site/src/site/pages/manual/layouts.html
Expand Up @@ -561,23 +561,23 @@ <h2 class="doAnchor" name="ClassicPatternLayout">PatternLayout</h2>
</tr>
<tr>
<td>%d</td>
<td>2006-10-20 14:06:49,812</td>
<td>2006-10-20T14:06:49,812</td>
</tr>
<tr>
<td>%date</td>
<td>2006-10-20 14:06:49,812</td>
<td>2006-10-20T14:06:49,812</td>
</tr>
<tr>
<td>%date{ISO8601}</td>
<td>2006-10-20 14:06:49,812</td>
<td>2006-10-20T14:06:49,812</td>
</tr>
<tr>
<td>%date{HH:mm:ss.SSS}</td>
<td>14:06:49.812</td>
</tr>
<tr>
<td>%date{dd&nbsp;MMM&nbsp;yyyy;HH:mm:ss.SSS}</td>
<td>20 oct. 2006;14:06:49.812 </td>
<td>20 Oct 2006;14:06:49.812 </td>
</tr>
</table>

Expand Down

0 comments on commit 12e2917

Please sign in to comment.