KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > MyPatternLayout


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package examples;
18
19 import org.apache.log4j.*;
20 import org.apache.log4j.helpers.PatternParser;
21
22 /**
23
24   Example showing how to extend PatternLayout to recognize additional
25   conversion characters.
26   
27   <p>In this case MyPatternLayout recognizes %# conversion pattern. It
28   outputs the value of an internal counter which is also incremented
29   at each call.
30
31   <p>See <a HREF=doc-files/MyPatternLayout.java><b>source</b></a> code
32   for more details.
33
34   @see MyPatternParser
35   @see org.apache.log4j.PatternLayout
36   @author Anders Kristensen
37 */

38 public class MyPatternLayout extends PatternLayout {
39   public
40   MyPatternLayout() {
41     this(DEFAULT_CONVERSION_PATTERN);
42   }
43
44   public
45   MyPatternLayout(String JavaDoc pattern) {
46     super(pattern);
47   }
48     
49   public
50   PatternParser createPatternParser(String JavaDoc pattern) {
51     return new MyPatternParser(
52       pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern);
53   }
54   
55   public
56   static void main(String JavaDoc[] args) {
57     Layout layout = new MyPatternLayout("[counter=%.10#] - %m%n");
58     Logger logger = Logger.getLogger("some.cat");
59     logger.addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
60     logger.debug("Hello, log");
61     logger.info("Hello again...");
62   }
63 }
64
Popular Tags