[logback-dev] svn commit: r1077 - in logback/trunk: logback-examples logback-examples/src/main/java/chapter6 logback-site/src/site/xdocTemplates/manual

noreply.seb at qos.ch noreply.seb at qos.ch
Wed Dec 13 14:26:33 CET 2006


Author: seb
Date: Wed Dec 13 14:26:33 2006
New Revision: 1077

Added:
   logback/trunk/logback-examples/src/main/java/chapter6/sampleTurboFilterConfig.xml
Modified:
   logback/trunk/logback-examples/pom.xml
   logback/trunk/logback-examples/src/main/java/chapter6/SampleTurboFilter.java
   logback/trunk/logback-site/src/site/xdocTemplates/manual/filters.xml

Log:
improved examples

Modified: logback/trunk/logback-examples/pom.xml
==============================================================================
--- logback/trunk/logback-examples/pom.xml	(original)
+++ logback/trunk/logback-examples/pom.xml	Wed Dec 13 14:26:33 2006
@@ -36,11 +36,6 @@
 			<groupId>ch.qos.logback</groupId>
 			<artifactId>logback-core</artifactId>
 		</dependency>
-
-		<dependency>
-			<groupId>ch.qos.logback</groupId>
-			<artifactId>logback-core</artifactId>
-		</dependency>
 			
 		<dependency>
 			<groupId>ch.qos.logback</groupId>

Modified: logback/trunk/logback-examples/src/main/java/chapter6/SampleTurboFilter.java
==============================================================================
--- logback/trunk/logback-examples/src/main/java/chapter6/SampleTurboFilter.java	(original)
+++ logback/trunk/logback-examples/src/main/java/chapter6/SampleTurboFilter.java	Wed Dec 13 14:26:33 2006
@@ -10,16 +10,33 @@
 
 public class SampleTurboFilter extends TurboFilter {
 
-  Marker sampleMarker = MarkerFactory.getMarker("sample");
+  String marker;
+  Marker acceptedMarker;
 
   @Override
   public FilterReply decide(Marker marker, Logger logger, Level level,
       String format, Object[] params, Throwable t) {
 
-    if ((sampleMarker.equals(marker))) {
+    if ((acceptedMarker.equals(marker))) {
       return FilterReply.ACCEPT;
     } else {
       return FilterReply.NEUTRAL;
     }
   }
+
+  public String getMarker() {
+    return marker;
+  }
+
+  public void setMarker(String markerStr) {
+    this.marker = markerStr;
+  }
+
+  @Override
+  public void start() {
+    if (marker != null && marker.trim().length() > 0) {
+      acceptedMarker = MarkerFactory.getMarker(marker);
+      super.start(); 
+    }
+  }
 }

Added: logback/trunk/logback-examples/src/main/java/chapter6/sampleTurboFilterConfig.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter6/sampleTurboFilterConfig.xml	Wed Dec 13 14:26:33 2006
@@ -0,0 +1,19 @@
+<configuration>
+
+	<turboFilter class="chapter6.SampleTurboFilter">
+		<Marker>sample</Marker>
+	</turboFilter>
+
+	<appender name="STDOUT"
+		class="ch.qos.logback.core.ConsoleAppender">
+		<layout class="ch.qos.logback.classic.PatternLayout">
+			<pattern>
+				%-4relative [%thread] %-5level %logger - %msg%n
+			</pattern>
+		</layout>
+	</appender>
+
+	<root>
+		<appender-ref ref="STDOUT" />
+	</root>
+</configuration>
\ No newline at end of file

Modified: logback/trunk/logback-site/src/site/xdocTemplates/manual/filters.xml
==============================================================================
--- logback/trunk/logback-site/src/site/xdocTemplates/manual/filters.xml	(original)
+++ logback/trunk/logback-site/src/site/xdocTemplates/manual/filters.xml	Wed Dec 13 14:26:33 2006
@@ -439,7 +439,8 @@
       To create your own <code>TurboFilter</code> component, just extend the 
       <code>TurboFilter</code> abstract class. Like previously, when implementing
       a custumized filter object, developing a custom <code>TurboFilter</code> only
-      ask that one implement the <code>decide()</code> method, as shown below:
+      ask that one implement the <code>decide()</code> method. In the next example, we
+      create a slightly more complex filter:
     </p>
     
 <em>Example 6.5: Basic custom <code>TurboFilter</code> (<a href="../xref/chapter6/SampleTurboFilter.html">logback-examples/src/main/java/chapter6/SampleTurboFilter.java</a>)</em>		
@@ -455,16 +456,37 @@
 
 public class SampleTurboFilter extends TurboFilter {
 
+  String marker;
+  Marker acceptedMarker;
+
   @Override
   public FilterReply decide(Marker marker, Logger logger, Level level,
       String format, Object[] params, Throwable t) {
-    if ((MarkerFactory.getMarker("sample").equals(marker))) {
+
+    if ((acceptedMarker.equals(marker))) {
       return FilterReply.ACCEPT;
     } else {
       return FilterReply.NEUTRAL;
     }
   }
-}</pre></div>
+
+  public String getMarker() {
+    return marker;
+  }
+
+  public void setMarker(String markerStr) {
+    this.marker = markerStr;
+  }
+
+  @Override
+  public void start() {
+    if (marker != null &amp;&amp; marker.trim().length() > 0) {
+      acceptedMarker = MarkerFactory.getMarker(marker);
+      super.start(); 
+    }
+  }
+}
+</pre></div>
 
 		<p>
 			The <code>TurboFilter</code> above accepts events that contain a specific marker.
@@ -473,12 +495,35 @@
 		</p>
 		
 		<p>
-			Like any other logback component, adding an option only requires implementing the
-			getter and setter methods in the corresponding class. Once done, the option can be specified
-			by adding nested elements inside the <em>TurboFilter</em> element.
+			To allow more flexibility, the marker that will be tested can be specified
+			in the configuration file. Hence the getter and setter methods. We also implemented
+			the <code>start()</code> method, to check that the option has been specified during the
+			configuration process.
 		</p>
-    
-    
+		
+		<p>
+			Here is a sample configuration that makes use of the newly created <code>TurboFilter</code>.
+		</p>
+		
+<em>Example 6.5: Basic custom <code>TurboFilter</code> configuration (logback-examples/src/main/java/chapter6/sampleTurboFilterConfig.xml)</em>		
+<div class="source"><pre>&lt;configuration>
+	<b>&lt;turboFilter class="chapter6.SampleTurboFilter">
+		&lt;Marker>sample&lt;/Marker>
+	&lt;/turboFilter></b>
+
+	&lt;appender name="STDOUT"
+		class="ch.qos.logback.core.ConsoleAppender">
+		&lt;layout class="ch.qos.logback.classic.PatternLayout">
+			&lt;pattern>
+				%-4relative [%thread] %-5level %logger - %msg%n
+			&lt;/pattern>
+		&lt;/layout>
+	&lt;/appender>
+
+	&lt;root>
+		&lt;appender-ref ref="STDOUT" />
+	&lt;/root>
+&lt;/configuration></pre></div>   
     
     
     <h2>Logback Access</h2>



More information about the logback-dev mailing list