[logback-dev] svn commit: r2382 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/pattern/parser main/java/ch/qos/logback/core/rolling main/java/ch/qos/logback/core/rolling/helper test/java/ch/qos/logback/core/rolling/helper
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Thu Jul 30 18:57:15 CEST 2009
Author: ceki
Date: Thu Jul 30 18:57:14 2009
New Revision: 2382
Added:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicy.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/MonoTypedConverter.java
Modified:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/TokenStream.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/IntegerTokenConverter.java
logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java
Log:
Further preparatory work on time and size based rolling - related to LBCORE-90
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/TokenStream.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/TokenStream.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/TokenStream.java Thu Jul 30 18:57:14 2009
@@ -1,11 +1,11 @@
-/**
- * LOGBack: the reliable, fast and flexible logging library for Java.
- *
- * Copyright (C) 1999-2005, QOS.ch, LOGBack.com
- *
- * This library is free software, you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation.
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
*/
package ch.qos.logback.core.pattern.parser;
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/DefaultTimeBasedFileNamingAndTriggeringPolicy.java Thu Jul 30 18:57:14 2009
@@ -3,78 +3,17 @@
import java.io.File;
import java.util.Date;
-import ch.qos.logback.core.rolling.helper.DateTokenConverter;
-import ch.qos.logback.core.rolling.helper.RollingCalendar;
-import ch.qos.logback.core.spi.ContextAwareBase;
-
-public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends ContextAwareBase
- implements TimeBasedFileNamingAndTriggeringPolicy<E> {
-
- private TimeBasedRollingPolicy<E> tbrp;
- private String elapsedPeriodsFileName;
- private RollingCalendar rc;
- private long currentTime;
- private long nextCheck;
- // indicate whether the time has been forced or not
- private boolean isTimeForced = false;
- private Date dateInCurrentPeriod = null;
- boolean started = false;
+public class DefaultTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
- public boolean isStarted() {
- return started;
- }
-
- public void start() {
-
- DateTokenConverter dtc = tbrp.fileNamePattern.getDateTokenConverter();
- if (dtc == null) {
- throw new IllegalStateException("FileNamePattern ["
- + tbrp.fileNamePattern.getPattern()
- + "] does not contain a valid DateToken");
- }
- rc = new RollingCalendar();
- rc.init(dtc.getDatePattern());
- addInfo("The date pattern is '" + dtc.getDatePattern()
- + "' from file name pattern '" + tbrp.fileNamePattern.getPattern()
- + "'.");
- rc.printPeriodicity(this);
-
- // dateInCurrentPeriod can be set by test classes
- // if it has not been set, we set it here
- if (dateInCurrentPeriod == null) {
- dateInCurrentPeriod = new Date();
- updateDateInCurrentPeriod(getCurrentTime());
- }
- computeNextCheck();
- }
-
- public void stop() {
- started = false;
+ @Override
+ public void start() {
+ super.start();
+
+ started = true;
}
- private void computeNextCheck() {
- nextCheck = rc.getNextTriggeringMillis(dateInCurrentPeriod);
- }
-
- // allow Test classes to act on the dateInCurrentPeriod field to simulate old
- // log files needing rollover
- public void setDateInCurrentPeriod(Date _dateInCurrentPeriod) {
- this.dateInCurrentPeriod = _dateInCurrentPeriod;
- }
-
- public Date getDateInCurrentPeriod() {
- return dateInCurrentPeriod;
- }
-
- public String getElapsedPeriodsFileName() {
- return elapsedPeriodsFileName;
- }
-
- public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
- return tbrp.fileNamePatternWCS.convertDate(dateInCurrentPeriod);
- }
public boolean isTriggeringEvent(File activeFile, final E event) {
long time = getCurrentTime();
@@ -90,32 +29,4 @@
return false;
}
}
-
- private void updateDateInCurrentPeriod(long now) {
- dateInCurrentPeriod.setTime(now);
- }
-
- public void setCurrentTime(long timeInMillis) {
- currentTime = timeInMillis;
- isTimeForced = true;
- }
-
- public long getCurrentTime() {
- // if time is forced return the time set by user
- if (isTimeForced) {
- return currentTime;
- } else {
- return System.currentTimeMillis();
- }
- }
-
- public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
- this.tbrp = _tbrp;
-
- }
-
- public RollingCalendar getRollingCalendar() {
- return rc;
- }
-
}
Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicy.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicy.java Thu Jul 30 18:57:14 2009
@@ -0,0 +1,38 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.rolling;
+
+import java.io.File;
+import java.util.Date;
+
+public class SizeAndTimeBasedFileNamingAndTriggeringPolicy<E> extends TimeBasedFileNamingAndTriggeringPolicyBase<E> {
+
+
+ @Override
+ public void start() {
+ super.start();
+ started = true;
+ }
+
+ public boolean isTriggeringEvent(File activeFile, final E event) {
+ long time = getCurrentTime();
+
+ if (time >= nextCheck) {
+ Date dateInElapsedPeriod = dateInCurrentPeriod;
+ elapsedPeriodsFileName = tbrp.fileNamePatternWCS
+ .convertDate(dateInElapsedPeriod);
+ updateDateInCurrentPeriod(time);
+ computeNextCheck();
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java Thu Jul 30 18:57:14 2009
@@ -0,0 +1,113 @@
+package ch.qos.logback.core.rolling;
+
+import java.util.Date;
+
+import ch.qos.logback.core.rolling.helper.DateTokenConverter;
+import ch.qos.logback.core.rolling.helper.RollingCalendar;
+import ch.qos.logback.core.spi.ContextAwareBase;
+
+abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends ContextAwareBase
+ implements TimeBasedFileNamingAndTriggeringPolicy<E> {
+
+ protected TimeBasedRollingPolicy<E> tbrp;
+
+
+ protected String elapsedPeriodsFileName;
+ protected RollingCalendar rc;
+
+
+ protected long currentTime;
+ //indicate whether the time has been forced or not
+ protected boolean isTimeForced = false;
+ protected Date dateInCurrentPeriod = null;
+
+
+ protected long nextCheck;
+ protected boolean started = false;
+
+
+ public boolean isStarted() {
+ return started;
+ }
+
+ public void start() {
+
+ DateTokenConverter dtc = tbrp.fileNamePattern.getDateTokenConverter();
+
+ if (dtc == null) {
+ throw new IllegalStateException("FileNamePattern ["
+ + tbrp.fileNamePattern.getPattern()
+ + "] does not contain a valid DateToken");
+ }
+
+ rc = new RollingCalendar();
+ rc.init(dtc.getDatePattern());
+ addInfo("The date pattern is '" + dtc.getDatePattern()
+ + "' from file name pattern '" + tbrp.fileNamePattern.getPattern()
+ + "'.");
+ rc.printPeriodicity(this);
+
+ // dateInCurrentPeriod can be set by test classes
+ // if it has not been set, we set it here
+ if (dateInCurrentPeriod == null) {
+ dateInCurrentPeriod = new Date();
+ updateDateInCurrentPeriod(getCurrentTime());
+ }
+ computeNextCheck();
+ }
+
+ public void stop() {
+ started = false;
+ }
+
+ protected void computeNextCheck() {
+ nextCheck = rc.getNextTriggeringMillis(dateInCurrentPeriod);
+ }
+
+ // allow Test classes to act on the dateInCurrentPeriod field to simulate old
+ // log files needing rollover
+ public void setDateInCurrentPeriod(Date _dateInCurrentPeriod) {
+ this.dateInCurrentPeriod = _dateInCurrentPeriod;
+ }
+
+ public Date getDateInCurrentPeriod() {
+ return dateInCurrentPeriod;
+ }
+
+ public String getElapsedPeriodsFileName() {
+ return elapsedPeriodsFileName;
+ }
+
+ public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
+ return tbrp.fileNamePatternWCS.convertDate(dateInCurrentPeriod);
+ }
+
+
+ protected void updateDateInCurrentPeriod(long now) {
+ dateInCurrentPeriod.setTime(now);
+ }
+
+ public void setCurrentTime(long timeInMillis) {
+ currentTime = timeInMillis;
+ isTimeForced = true;
+ }
+
+ public long getCurrentTime() {
+ // if time is forced return the time set by user
+ if (isTimeForced) {
+ return currentTime;
+ } else {
+ return System.currentTimeMillis();
+ }
+ }
+
+ public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
+ this.tbrp = _tbrp;
+
+ }
+
+ public RollingCalendar getRollingCalendar() {
+ return rc;
+ }
+
+}
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java Thu Jul 30 18:57:14 2009
@@ -15,13 +15,17 @@
import ch.qos.logback.core.pattern.DynamicConverter;
-
/**
- *
+ *
* @author Ceki Gücü
*/
-public class DateTokenConverter extends DynamicConverter {
-
+public class DateTokenConverter extends DynamicConverter implements MonoTypedConverter {
+
+ /**
+ * The conversion word/character with which this converter is registered.
+ */
+ public final static String CONVERTER_KEY = "d";
+
String datePattern;
SimpleDateFormat sdf;
@@ -30,25 +34,27 @@
public void start() {
this.datePattern = getFirstOption();
- if(this.datePattern == null) {
- this.datePattern = "yyyy-MM-dd";;
+ if (this.datePattern == null) {
+ this.datePattern = "yyyy-MM-dd";
+ ;
}
sdf = new SimpleDateFormat(datePattern);
}
-
+
public String convert(Date date) {
return sdf.format(date);
}
-
+
public String convert(Object o) {
- if(o == null) {
+ if (o == null) {
throw new IllegalArgumentException("Null argument forbidden");
}
- if(o instanceof Date) {
+ if (o instanceof Date) {
return convert((Date) o);
- }
+ }
throw new IllegalArgumentException("Cannot convert "+o+" of type"+o.getClass().getName());
}
+
/**
* Return the date pattern.
*/
@@ -56,11 +62,14 @@
return datePattern;
}
+ public boolean isApplicable(Object o) {
+ return (o instanceof Date);
+ }
+
/**
* Set the date pattern.
*/
- //public void setDatePattern(String string) {
- // datePattern = string;
- //}
-
+ // public void setDatePattern(String string) {
+ // datePattern = string;
+ // }
}
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java Thu Jul 30 18:57:14 2009
@@ -1,7 +1,7 @@
/**
- * LOGBack: the reliable, fast and flexible logging library for Java.
+ * Logback: the generic, reliable, fast and flexible logging framework.
*
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 2000-2009, QOS.ch
*
* This library is free software, you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
@@ -12,6 +12,7 @@
import java.util.Date;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import ch.qos.logback.core.Context;
@@ -23,12 +24,10 @@
import ch.qos.logback.core.pattern.util.AlmostAsIsEscapeUtil;
import ch.qos.logback.core.spi.ContextAwareBase;
-
/**
- *
- * After parsing file name patterns, given a number or a date, instances of this class
- * can be used to compute a file name according to the file name pattern and the given
- * integer or date.
+ * After parsing file name patterns, given a number or a date, instances of this
+ * class can be used to compute a file name according to the file name pattern
+ * and the given integer or date.
*
* @author Ceki Gülcü
*
@@ -37,8 +36,8 @@
static final Map<String, String> CONVERTER_MAP = new HashMap<String, String>();
static {
- CONVERTER_MAP.put("i", IntegerTokenConverter.class.getName());
- CONVERTER_MAP.put("d", DateTokenConverter.class.getName());
+ CONVERTER_MAP.put(IntegerTokenConverter.CONVERTER_KEY, IntegerTokenConverter.class.getName());
+ CONVERTER_MAP.put(DateTokenConverter.CONVERTER_KEY, DateTokenConverter.class.getName());
}
String pattern;
@@ -94,9 +93,29 @@
return null;
}
+
+ public String convertList(List<Object> objectList) {
+ StringBuilder buf = new StringBuilder();
+ Converter<Object> c = headTokenConverter;
+ while (c != null) {
+ if(c instanceof MonoTypedConverter) {
+ MonoTypedConverter monoTyped = (MonoTypedConverter) c;
+ for(Object o: objectList) {
+ if(monoTyped.isApplicable(o)) {
+ buf.append(c.convert(o));
+ }
+ }
+ } else {
+ buf.append(c.convert(objectList));
+ }
+ c = c.getNext();
+ }
+ return buf.toString();
+ }
+
public String convert(Object o) {
+ StringBuilder buf = new StringBuilder();
Converter<Object> p = headTokenConverter;
- StringBuffer buf = new StringBuffer();
while (p != null) {
buf.append(p.convert(o));
p = p.getNext();
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/IntegerTokenConverter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/IntegerTokenConverter.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/IntegerTokenConverter.java Thu Jul 30 18:57:14 2009
@@ -1,7 +1,7 @@
/**
- * LOGBack: the reliable, fast and flexible logging library for Java.
+ * Logback: the generic, reliable, fast and flexible logging framework.
*
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 2000-2009, QOS.ch
*
* This library is free software, you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
@@ -17,8 +17,10 @@
*
* @author Ceki Gulcu
*/
-public class IntegerTokenConverter extends DynamicConverter {
+public class IntegerTokenConverter extends DynamicConverter implements MonoTypedConverter {
+ public final static String CONVERTER_KEY = "i";
+
public IntegerTokenConverter() {
}
@@ -33,7 +35,11 @@
if(o instanceof Integer) {
Integer i = (Integer) o;
return convert(i.intValue());
- }
+ }
throw new IllegalArgumentException("Cannot convert "+o+" of type"+o.getClass().getName());
}
+
+ public boolean isApplicable(Object o) {
+ return (o instanceof Integer);
+ }
}
Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/MonoTypedConverter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/MonoTypedConverter.java Thu Jul 30 18:57:14 2009
@@ -0,0 +1,20 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2009, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.rolling.helper;
+
+/**
+ * Converters which can deal only with one type should implement this interface.
+ *
+ * @author Ceki G&ulcu;lcü
+ *
+ */
+public interface MonoTypedConverter {
+ boolean isApplicable(Object o);
+}
Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java (original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/FileNamePatternTest.java Thu Jul 30 18:57:14 2009
@@ -9,13 +9,14 @@
*/
package ch.qos.logback.core.rolling.helper;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
import java.util.Calendar;
+import java.util.List;
import org.junit.Test;
-
import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
@@ -64,9 +65,7 @@
@Test
// test ways for dealing with flowing i converter, as in "foo%ix"
- public void testFlowingI() {
- // System.out.println("Testing [foo%ibar%i]");
-
+ public void flowingI() {
{
FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
assertEquals("foo3bar3", pp.convertInt(3));
@@ -78,7 +77,7 @@
}
@Test
- public void testDate() {
+ public void date() {
Calendar cal = Calendar.getInstance();
cal.set(2003, 4, 20, 17, 55);
@@ -95,10 +94,26 @@
}
@Test
- public void testWithBackslash() {
+ public void withBackslash() {
FileNamePattern pp = new FileNamePattern("c:\\foo\\bar.%i", context);
assertEquals("c:\\foo\\bar.3", pp.convertInt(3));
}
+ @Test
+ public void objectListConverter() {
+ List<Object> oList = new ArrayList<Object>();
+
+ Calendar cal = Calendar.getInstance();
+ cal.set(2003, 4, 20, 17, 55);
+
+ oList.add(cal.getTime());
+ oList.add(79);
+
+
+ FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
+
+ assertEquals("foo-2003.05.20-79.txt", fnp.convertList(oList));
+ }
+
}
More information about the logback-dev
mailing list