Skip to content

Commit

Permalink
fix LOGBACK-1571 and LOGBACK-1421
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 17, 2021
1 parent 23bfa90 commit 5fa9345
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 151 deletions.
Expand Up @@ -13,8 +13,8 @@
*/
package ch.qos.logback.access.pattern;

import java.time.ZoneId;
import java.util.List;
import java.util.TimeZone;

import ch.qos.logback.access.spi.IAccessEvent;
import ch.qos.logback.core.CoreConstants;
Expand All @@ -23,10 +23,10 @@
public class DateConverter extends AccessConverter {

CachingDateFormatter cachingDateFormatter = null;

@Override
public void start() {

String datePattern = getFirstOption();
if (datePattern == null) {
datePattern = CoreConstants.CLF_DATE_PATTERN;
Expand All @@ -35,23 +35,26 @@ public void start() {
if (datePattern.equals(CoreConstants.ISO8601_STR)) {
datePattern = CoreConstants.ISO8601_PATTERN;
}
ZoneId zoneId = null;
List<String> optionList = getOptionList();

// if the option list contains a TZ option, then set it.
if (optionList != null && optionList.size() > 1) {
String zoneIdString = (String) optionList.get(1);
zoneId = ZoneId.of(zoneIdString);
}

try {
cachingDateFormatter = new CachingDateFormatter(datePattern);
cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId);
// maximumCacheValidity = CachedDateFormat.getMaximumCacheValidity(pattern);
} catch (IllegalArgumentException e) {
addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
addWarn("Defaulting to " + CoreConstants.CLF_DATE_PATTERN);
cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN);
cachingDateFormatter = new CachingDateFormatter(CoreConstants.CLF_DATE_PATTERN, zoneId);
}

List<String> optionList = getOptionList();

// if the option list contains a TZ option, then set it.
if (optionList != null && optionList.size() > 1) {
TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
cachingDateFormatter.setTimeZone(tz);
}

}

@Override
Expand Down
Expand Up @@ -13,8 +13,8 @@
*/
package ch.qos.logback.classic.pattern;

import java.time.ZoneId;
import java.util.List;
import java.util.TimeZone;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
Expand All @@ -37,23 +37,24 @@ public void start() {
datePattern = CoreConstants.ISO8601_PATTERN;
}

List<String> optionList = getOptionList();
ZoneId zoneId = null;
// if the option list contains a TZ option, then set it.
if (optionList != null && optionList.size() > 1) {
String zoneIdString = (String) optionList.get(1);
zoneId = ZoneId.of(zoneIdString);
}

try {
cachingDateFormatter = new CachingDateFormatter(datePattern);
// maximumCacheValidity =
// CachedDateFormat.getMaximumCacheValidity(pattern);
cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId);
} catch (IllegalArgumentException e) {
addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
// default to the ISO8601 format
cachingDateFormatter = new CachingDateFormatter(CoreConstants.ISO8601_PATTERN);
cachingDateFormatter = new CachingDateFormatter(CoreConstants.ISO8601_PATTERN, zoneId);
}

List<String> optionList = getOptionList();

// if the option list contains a TZ option, then set it.
if (optionList != null && optionList.size() > 1) {
TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
cachingDateFormatter.setTimeZone(tz);
}


super.start();
}

Expand Down
Expand Up @@ -18,48 +18,53 @@

public class ConverterUtil {

/**
* Start converters in the chain of converters.
*
* @param head
*/
public static <E> void startConverters(Converter<E> head) {
Converter<E> c = head;
while (c != null) {
// CompositeConverter is a subclass of DynamicConverter
if (c instanceof CompositeConverter) {
CompositeConverter<E> cc = (CompositeConverter<E>) c;
Converter<E> childConverter = cc.childConverter;
startConverters(childConverter);
cc.start();
} else if (c instanceof DynamicConverter) {
DynamicConverter<E> dc = (DynamicConverter<E>) c;
dc.start();
}
c = c.getNext();
}
}
/**
* Start converters in the chain of converters.
*
* @param head
*/
public static <E> void startConverters(Converter<E> head) {
Converter<E> c = head;
while (c != null) {
// CompositeConverter is a subclass of DynamicConverter
if (c instanceof CompositeConverter) {
CompositeConverter<E> cc = (CompositeConverter<E>) c;
Converter<E> childConverter = cc.childConverter;
startConverters(childConverter);
cc.start();
} else if (c instanceof DynamicConverter) {
DynamicConverter<E> dc = (DynamicConverter<E>) c;
dc.start();
}
c = c.getNext();
}
}

public static <E> Converter<E> findTail(Converter<E> head) {
Converter<E> p = head;
while (p != null) {
Converter<E> next = p.getNext();
if (next == null) {
break;
} else {
p = next;
}
}
return p;
}
public static <E> Converter<E> findTail(Converter<E> head) {
Converter<E> p = head;
while (p != null) {
Converter<E> next = p.getNext();
if (next == null) {
break;
} else {
p = next;
}
}
return p;
}

public static <E> void setContextForConverters(Context context, Converter<E> head) {
Converter<E> c = head;
while (c != null) {
if (c instanceof ContextAware) {
((ContextAware) c).setContext(context);
}
c = c.getNext();
}
}
public static <E> void setContextForConverters(Context context, Converter<E> head) {
Converter<E> c = head;
while (c != null) {
if (c instanceof ContextAware) {
((ContextAware) c).setContext(context);
}
if (c instanceof CompositeConverter) {
CompositeConverter<E> cc = (CompositeConverter<E>) c;
Converter<E> childConverter = cc.childConverter;
setContextForConverters(context, childConverter);
}
c = c.getNext();
}
}
}
Expand Up @@ -39,14 +39,17 @@ abstract public class DynamicConverter<E> extends FormattingConverter<E> impleme
* components, the trivial implementation found in this abstract class will be
* sufficient.
*/
@Override
public void start() {
started = true;
}

@Override
public void stop() {
started = false;
}

@Override
public boolean isStarted() {
return started;
}
Expand All @@ -73,38 +76,47 @@ protected List<String> getOptionList() {
return optionList;
}

@Override
public void setContext(Context context) {
cab.setContext(context);
}

@Override
public Context getContext() {
return cab.getContext();
}

@Override
public void addStatus(Status status) {
cab.addStatus(status);
}

@Override
public void addInfo(String msg) {
cab.addInfo(msg);
}

@Override
public void addInfo(String msg, Throwable ex) {
cab.addInfo(msg, ex);
}

@Override
public void addWarn(String msg) {
cab.addWarn(msg);
}

@Override
public void addWarn(String msg, Throwable ex) {
cab.addWarn(msg, ex);
}

@Override
public void addError(String msg) {
cab.addError(msg);
}

@Override
public void addError(String msg, Throwable ex) {
cab.addError(msg, ex);
}
Expand Down
Expand Up @@ -35,6 +35,7 @@ class Token {

static Token EOF_TOKEN = new Token(EOF, "EOF");
static Token RIGHT_PARENTHESIS_TOKEN = new Token(RIGHT_PARENTHESIS);
// BARE as in naked. Used for formatting purposes
static Token BARE_COMPOSITE_KEYWORD_TOKEN = new Token(COMPOSITE_KEYWORD, "BARE");
static Token PERCENT_TOKEN = new Token(PERCENT);

Expand Down
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.rolling.helper.ArchiveRemover;
Expand Down Expand Up @@ -52,8 +53,9 @@ public void start() {
throw new IllegalStateException("FileNamePattern [" + tbrp.fileNamePattern.getPattern() + "] does not contain a valid DateToken");
}

if (dtc.getTimeZone() != null) {
rc = new RollingCalendar(dtc.getDatePattern(), dtc.getTimeZone(), Locale.getDefault());
if (dtc.getZoneId() != null) {
TimeZone tz = TimeZone.getTimeZone(dtc.getZoneId());
rc = new RollingCalendar(dtc.getDatePattern(), tz, Locale.getDefault());
} else {
rc = new RollingCalendar(dtc.getDatePattern());
}
Expand Down
Expand Up @@ -13,9 +13,9 @@
*/
package ch.qos.logback.core.rolling.helper;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.pattern.DynamicConverter;
Expand All @@ -37,7 +37,7 @@ public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTy
public static final String DEFAULT_DATE_PATTERN = CoreConstants.DAILY_DATE_PATTERN;

private String datePattern;
private TimeZone timeZone;
private ZoneId zoneId;
private CachingDateFormatter cdf;
// is this token converter primary or auxiliary? Only the primary converter
// determines the rolling period
Expand All @@ -56,15 +56,12 @@ public void start() {
if (AUXILIARY_TOKEN.equalsIgnoreCase(option)) {
primary = false;
} else {
timeZone = TimeZone.getTimeZone(option);
zoneId = ZoneId.of(option);
}
}
}

cdf = new CachingDateFormatter(datePattern);
if (timeZone != null) {
cdf.setTimeZone(timeZone);
}
cdf = new CachingDateFormatter(datePattern, zoneId);
}

public String convert(Date date) {
Expand All @@ -88,8 +85,8 @@ public String getDatePattern() {
return datePattern;
}

public TimeZone getTimeZone() {
return timeZone;
public ZoneId getZoneId() {
return zoneId;
}

public boolean isApplicable(Object o) {
Expand Down

0 comments on commit 5fa9345

Please sign in to comment.