
Author: seb Date: Thu Jan 4 18:48:33 2007 New Revision: 1157 Added: logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution1.xml logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution2.xml logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution3.xml logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution4.xml logback/trunk/logback-examples/src/main/java/chapter3/variables1.properties logback/trunk/logback-examples/src/main/java/chapter3/variables2.properties Modified: logback/trunk/logback-examples/src/main/java/chapter3/restricted.xml logback/trunk/logback-site/src/site/xdocTemplates/manual/joran.xml Log: On going work on chapter 3 Modified: logback/trunk/logback-examples/src/main/java/chapter3/restricted.xml ============================================================================== --- logback/trunk/logback-examples/src/main/java/chapter3/restricted.xml (original) +++ logback/trunk/logback-examples/src/main/java/chapter3/restricted.xml Thu Jan 4 18:48:33 2007 @@ -11,8 +11,7 @@ </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> - %date %level [%thread] %logger{10} [%file : %line] - %msg%n + %date %level [%thread] %logger{10} [%file : %line] %msg%n </Pattern> </layout> </appender> Added: logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution1.xml ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution1.xml Thu Jan 4 18:48:33 2007 @@ -0,0 +1,26 @@ +<configuration> + + <substitutionProperty name="user.home.dir" + value="/Users/seb" /> + + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${user.home.dir}/myApp.log</file> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration> \ No newline at end of file Added: logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution2.xml ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution2.xml Thu Jan 4 18:48:33 2007 @@ -0,0 +1,23 @@ +<configuration> + + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${user.home.dir}/myApp.log</file> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration> \ No newline at end of file Added: logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution3.xml ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution3.xml Thu Jan 4 18:48:33 2007 @@ -0,0 +1,25 @@ +<configuration> + + <substitutionProperty file="variables1.properties" /> + + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${user.home.dir}/myApp.log</file> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration> \ No newline at end of file Added: logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution4.xml ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variableSubstitution4.xml Thu Jan 4 18:48:33 2007 @@ -0,0 +1,25 @@ +<configuration> + + <substitutionProperty file="variables2.properties" /> + + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${destination}</file> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration> \ No newline at end of file Added: logback/trunk/logback-examples/src/main/java/chapter3/variables1.properties ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variables1.properties Thu Jan 4 18:48:33 2007 @@ -0,0 +1 @@ +user.home.dir=/Users/seb \ No newline at end of file Added: logback/trunk/logback-examples/src/main/java/chapter3/variables2.properties ============================================================================== --- (empty file) +++ logback/trunk/logback-examples/src/main/java/chapter3/variables2.properties Thu Jan 4 18:48:33 2007 @@ -0,0 +1,3 @@ +user.home.dir=/Users/seb +file=myApp.log +destination=${user.home.dir}/${file} \ No newline at end of file Modified: logback/trunk/logback-site/src/site/xdocTemplates/manual/joran.xml ============================================================================== --- logback/trunk/logback-site/src/site/xdocTemplates/manual/joran.xml (original) +++ logback/trunk/logback-site/src/site/xdocTemplates/manual/joran.xml Thu Jan 4 18:48:33 2007 @@ -1222,37 +1222,43 @@ substitution is similar to that of Unix shells. The string between an opening <em>${</em> and closing <em>}</em> is interpreted as a key. The value of the substituted variable can be defined as a system property -or in the configuration file itself. The value of the key -is first searched in the system properties, and if not found there, -it is then searched in the configuration file being parsed. +in the configuration file itself or in a separate file linked to the +configuration file. The value of the key +is first searched in configuration file or linked properties file, +and if not found there, it is then searched in system properties. The corresponding value replaces <em>${aKey}</em> sequence. For example, if <em>java.home</em> system property is set to <em>/home/xyz</em>, then every occurrence of the sequence <em>${java.home}</em> will be interpreted as <em>/home/xyz</em>. </p> -<em>Example 3.12: Variable substitution (logback-examples/src/main/java/chapter3/variableSubstitution.xml)</em> -<div class="source"><pre><configuration debug="true"> +<p> +The first example shows a declared property at the beginning of the +configuration file. It is then used further down the file to specify +the place to create the output file. +</p> + +<em>Example 3.12: Simple Variable substitution (logback-examples/src/main/java/chapter3/variableSubstitution1.xml)</em> +<div class="source"><pre><configuration> + + <b><substitutionProperty name="user.home.dir" value="/Users/seb" /></b> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> - <file><b>${filename}</b></file> - + <b><file>${user.home.dir}/myApp.log</file></b> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern> - <b>${filename}-%d{yyyy-MM-dd-HH-mm}</b> + myApp-%d{yyyy-MM-dd-HH}.log </FileNamePattern> </rollingPolicy> - <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> - %relative [%thread] %level %logger{20} %msg%n + %date %level [%thread] %logger{10} [%file : %line] %msg%n </Pattern> </layout> </appender> - <root> <level value="debug" /> <appender-ref ref="FILE" /> @@ -1260,26 +1266,125 @@ </configuration></pre></div> +<p> +The next example shows the use of a System property to achieve the same result. The +property is not declared anywhere, thus logback will look for it in the System properties. +</p> +<em>Example 3.13: System Variable substitution (logback-examples/src/main/java/chapter3/variableSubstitution2.xml)</em> +<div class="source"><pre><configuration> + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <b><file>${user.home.dir}/myApp.log</file></b> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration></pre></div> +<p> +When many variables are used, it is often more convenient to create +a separate file that will contain all the variables. Here is how one can +do such a setup. +</p> +<em>Example 3.14: Variable substitution using a separate file (logback-examples/src/main/java/chapter3/variableSubstitution3.xml)</em> +<div class="source"><pre><configuration> + <substitutionProperty file="variables1.properties" /> + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <b><file>${user.home.dir}/myApp.log</file></b> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration></pre></div> +<p> +This configuration file contains a link to another file called <em>variables.properties</em>. +The variables contained in that other file will be read and will thus be available to the +logback configuration file. Here is what the <em>variable.properties</em> file looks like. +</p> +<em>Example 3.15: Variable file (logback-examples/src/main/java/chapter3/variables1.properties)</em> +<div class="source"><pre>user.home.dir=/Users/seb</pre></div> +<p> +Nothing more is needed to declare the variable. +</p> +<p> +Recursive subsitution is also available. If the user wants to use variables to +specify not only the destination directory but also the file name, here is what she +would write in her <em>variables.properties</em> file. +</p> +<em>Example 3.16: Recursive use of variables (logback-examples/src/main/java/chapter3/variables2.properties)</em> +<div class="source"><pre>user.home.dir=/Users/seb +file=myApp.log +destination=${user.home.dir}/${file}</pre></div> + +<p> +In the configuration file, only the last variable, <em>${destination}</em> will +be used, as shown below: +</p> +<em>Example 3.17: Variable substitution using a separate file (logback-examples/src/main/java/chapter3/variableSubstitution4.xml)</em> +<div class="source"><pre><configuration> + <substitutionProperty file="variables1.properties" /> + <appender name="FILE" + class="ch.qos.logback.core.rolling.RollingFileAppender"> + <b><file>${destination}</file></b> + <rollingPolicy + class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <FileNamePattern> + myApp-%d{yyyy-MM-dd-HH}.log + </FileNamePattern> + </rollingPolicy> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern> + %date %level [%thread] %logger{10} [%file : %line] %msg%n + </Pattern> + </layout> + </appender> - - + <root> + <level value="debug" /> + <appender-ref ref="FILE" /> + </root> +</configuration></pre></div>