Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ch.qos.logback.classic.sift;

import java.util.List;
import java.util.Map;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.joran.event.SaxEvent;
Expand All @@ -23,14 +24,16 @@
public class AppenderFactory extends AppenderFactoryBase<ILoggingEvent>{

String key;
Map<String, String> parentpropertyMap;

AppenderFactory(List<SaxEvent> eventList, String key) {
AppenderFactory(List<SaxEvent> eventList, String key, Map<String, String> parentpropertyMap) {
super(eventList);
this.key = key;
this.parentpropertyMap = parentpropertyMap;
}

public SiftingJoranConfiguratorBase<ILoggingEvent> getSiftingJoranConfigurator(String discriminatingValue) {
return new SiftingJoranConfigurator(key, discriminatingValue);
return new SiftingJoranConfigurator(key, discriminatingValue, parentpropertyMap);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;

Expand All @@ -40,8 +41,9 @@ public void end(InterpretationContext ic, String name) throws ActionException {
Object o = ic.peekObject();
if (o instanceof SiftingAppender) {
SiftingAppender sa = (SiftingAppender) o;
Map<String, String> propertyMap = ic.getCopyOfPropertyMap();
AppenderFactory appenderFactory = new AppenderFactory(seList, sa
.getDiscriminatorKey());
.getDiscriminatorKey(), propertyMap);
sa.setAppenderFactory(appenderFactory);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public class SiftingJoranConfigurator extends SiftingJoranConfiguratorBase<ILog

String key;
String value;
Map<String, String> parentpropertyMap;

SiftingJoranConfigurator(String key, String value) {
SiftingJoranConfigurator(String key, String value, Map<String, String> parentpropertyMap) {
this.key = key;
this.value = value;
this.parentpropertyMap = parentpropertyMap;
}

@Override
Expand All @@ -61,6 +63,7 @@ protected void buildInterpreter() {
omap.put(ActionConst.APPENDER_BAG, new HashMap());
omap.put(ActionConst.FILTER_CHAIN_BAG, new HashMap());
Map<String, String> propertiesMap = new HashMap<String, String>();
propertiesMap.putAll(parentpropertyMap);
propertiesMap.put(key, value);
interpreter.setInterpretationContextPropertiesMap(propertiesMap);
}
Expand Down
7 changes: 7 additions & 0 deletions logback-classic/src/test/input/joran/timestamp-local.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<configuration>
<timestamp key="testTimestamp" datePattern="yyyy-MM"/>
<property scope="system"
name="ch.qos.logback.classic.joran.JoranConfiguratorTest.timestampLocal"
value="today is ${testTimestamp}"/>
</configuration>

2 changes: 1 addition & 1 deletion logback-classic/src/test/input/joran/timestamp.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<configuration>
<timestamp key="testTimestamp" datePattern="yyyy-MM"/>
<timestamp key="testTimestamp" datePattern="yyyy-MM" scope="context"/>
</configuration>

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import ch.qos.logback.classic.jul.JULHelper;
Expand Down Expand Up @@ -323,6 +325,32 @@ public void timestamp() throws JoranException, IOException,
assertEquals("expected \"" + expected + "\" but got " + r, expected, r);
}

@Test
public void timestampLocal() throws JoranException, IOException,
InterruptedException {

String sysProp = "ch.qos.logback.classic.joran.JoranConfiguratorTest.timestampLocal";
System.setProperty(sysProp, "");

String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX
+ "timestamp-local.xml";
configure(configFileAsStr);

// It's hard to test the local variable has been set, as it's not
// visible from here. But instead we test that it's not set in the
// context. And check that a system property has been replaced with the
// contents of the local variable

String r = loggerContext.getProperty("testTimestamp");
assertNull(r);

String exprected = "today is " + new SimpleDateFormat("yyyy-MM").format(new Date());
String sysPropValue = System.getProperty(sysProp);
assertEquals(exprected, sysPropValue);


}

@Test
public void encoderCharset() throws JoranException, IOException,
InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ch.qos.logback.core.util.CachingDateFormatter;
import org.xml.sax.Attributes;

import ch.qos.logback.core.joran.action.ActionUtil.Scope;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.util.OptionHelper;
Expand Down Expand Up @@ -64,12 +65,16 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)
if (inError)
return;

String scopeStr = attributes.getValue(SCOPE_ATTRIBUTE);

Scope scope = ActionUtil.stringToScope(scopeStr);

CachingDateFormatter sdf = new CachingDateFormatter(datePatternStr);
String val = sdf.format(timeReference);

addInfo("Adding property to the context with key=\"" + keyStr
+ "\" and value=\"" + val + "\" to the context");
context.putProperty(keyStr, val);
+ "\" and value=\"" + val + "\" to the " + scope + " scope");
ActionUtil.setProperty(ec, keyStr, val, scope);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -62,6 +61,10 @@ public DefaultNestedComponentRegistry getDefaultNestedComponentRegistry() {
return defaultNestedComponentRegistry;
}

public Map<String, String> getCopyOfPropertyMap() {
return new HashMap<String, String>(propertiesMap);
}

void setPropertiesMap(Map<String, String> propertiesMap) {
this.propertiesMap = propertiesMap;
}
Expand Down Expand Up @@ -129,9 +132,7 @@ public void addSubstitutionProperties(Properties props) {
if (props == null) {
return;
}
Iterator i = props.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
for (String key : props.stringPropertyNames()) {
addSubstitutionProperty(key, props.getProperty(key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package ch.qos.logback.core.sift;

import java.util.ArrayList;
import java.util.List;

import ch.qos.logback.core.Appender;
Expand All @@ -26,13 +25,12 @@ public abstract class AppenderFactoryBase<E> {
final List<SaxEvent> eventList;

protected AppenderFactoryBase(List<SaxEvent> eventList) {
this.eventList = new ArrayList<SaxEvent>(eventList);
removeSiftElement();
this.eventList = removeSiftElement(eventList);

}

void removeSiftElement() {
eventList.remove(0);
eventList.remove(eventList.size() - 1);
List<SaxEvent> removeSiftElement(List<SaxEvent> eventList) {
return eventList.subList(1, eventList.size() - 1);
}

public abstract SiftingJoranConfiguratorBase<E> getSiftingJoranConfigurator(String k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ NOPAppender<E> buildNOPAppender(String discriminatingValue) {
/**
* @since 0.9.19
*/
public AppenderTracker getAppenderTracker() {
public AppenderTracker<E> getAppenderTracker() {
return appenderTracker;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void addImplicitRules(Interpreter interpreter) {

int errorEmmissionCount = 0;

protected void oneAndOnlyOneCheck(Map appenderMap) {
protected void oneAndOnlyOneCheck(Map<?, ?> appenderMap) {
String errMsg = null;
if (appenderMap.size() == 0) {
errorEmmissionCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
*/
package ch.qos.logback.core.spi;

import java.util.Map;

public interface PropertyContainer {

String getProperty(String key);

Map<String, String> getCopyOfPropertyMap();
}