Joran and variable substitution

I was impressed by slf4j and logback and decided to use Joran in one of my programs. While trying to use variable substitution I failed, the variable ${os.name} never gets replaced. Is variable substitution only available in logback and not in joran? A superficial look at the source makes me think so.

Hi Marge, Variable substitution is done by Joran actions on an individual basis. Search for usages of the subst() method of the InterpretationContext class. HTH, On 30.12.2010 10:24, Marge wrote:
I was impressed by slf4j and logback and decided to use Joran in one of my programs. While trying to use variable substitution I failed, the variable ${os.name} never gets replaced. Is variable substitution only available in logback and not in joran? A superficial look at the source makes me think so.

Thanks for the hint, this lead me in the right direction ! I found out that a simple context.subst in e.g. the begin part of the action is sufficient. To make things easier for me I created a small helper class: public final class VariableHelper { final static Logger log = LoggerFactory.getLogger(VariableHelper.class); private InterpretationContext context = null; private Attributes attrs = null; private VariableHelper( InterpretationContext context, Attributes attrs ) { this.context = context; this.attrs = attrs; } public static String replace(final InterpretationContext ic, final String str) { String ret = null; log.debug("before subst '{}'", str); ret = ic.subst(str); log.debug("after subst: '{}'", ret); return ret; } public static String replace(final InterpretationContext ic, final Attributes attrs, final String attrName) { return replace(ic, attrs.getValue(attrName)); } public String replace( String attrName ) { return replace( context, attrs, attrName ); } public static VariableHelper createInstance( final InterpretationContext context, final Attributes attrs ) { return new VariableHelper( context, attrs ); } } This class can be used like this: public class FooBarAction extends Action { public void begin(InterpretationContext context, String arg1, Attributes attributes) throws ActionException { VariableHelper vh = VariableHelper.createInstance(context, attributes); String foo = vh.replace("foo"); String bar = vh.replace("sbar"); ... This is a replacement for: String foo = attributes.getValue( "foo" ); foo = context.subst("foo" ); Perhaps this is of help for somebody. -------- Original-Nachricht --------
Datum: Thu, 30 Dec 2010 13:35:37 +0100 Von: Ceki Gulcu <ceki@qos.ch> An: logback users list <logback-user@qos.ch> Betreff: Re: [logback-user] Joran and variable substitution
Hi Marge,
Variable substitution is done by Joran actions on an individual basis. Search for usages of the subst() method of the InterpretationContext class.
-- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail

If all you are looking for is variable replacement then you could look at commons-lang's StrSubstitutor class. Commons Configuration's ConfigurationInterpolator provides an example of how it can be used to resolve variables from multiple StrLookup sources, including SLF4J's MDCLookup. Ralph On Dec 30, 2010, at 1:18 PM, Oliver Geishuettner wrote:
Thanks for the hint, this lead me in the right direction ! I found out that a simple context.subst in e.g. the begin part of the action is sufficient. To make things easier for me I created a small helper class:
public final class VariableHelper { final static Logger log = LoggerFactory.getLogger(VariableHelper.class);
private InterpretationContext context = null; private Attributes attrs = null;
private VariableHelper( InterpretationContext context, Attributes attrs ) { this.context = context; this.attrs = attrs; }
public static String replace(final InterpretationContext ic, final String str) { String ret = null; log.debug("before subst '{}'", str); ret = ic.subst(str); log.debug("after subst: '{}'", ret);
return ret; }
public static String replace(final InterpretationContext ic, final Attributes attrs, final String attrName) { return replace(ic, attrs.getValue(attrName)); }
public String replace( String attrName ) { return replace( context, attrs, attrName ); }
public static VariableHelper createInstance( final InterpretationContext context, final Attributes attrs ) { return new VariableHelper( context, attrs ); } }
This class can be used like this:
public class FooBarAction extends Action { public void begin(InterpretationContext context, String arg1, Attributes attributes) throws ActionException { VariableHelper vh = VariableHelper.createInstance(context, attributes);
String foo = vh.replace("foo"); String bar = vh.replace("sbar"); ...
This is a replacement for:
String foo = attributes.getValue( "foo" ); foo = context.subst("foo" );
Perhaps this is of help for somebody. -------- Original-Nachricht --------
Datum: Thu, 30 Dec 2010 13:35:37 +0100 Von: Ceki Gulcu <ceki@qos.ch> An: logback users list <logback-user@qos.ch> Betreff: Re: [logback-user] Joran and variable substitution
Hi Marge,
Variable substitution is done by Joran actions on an individual basis. Search for usages of the subst() method of the InterpretationContext class.
-- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
participants (4)
-
Ceki Gulcu
-
Marge
-
Oliver Geishuettner
-
Ralph Goers