KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > lf5 > InitUsingMultipleAppenders


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 package examples.lf5.InitUsingMultipleAppenders;
17
18 import org.apache.log4j.Logger;
19 import org.apache.log4j.PropertyConfigurator;
20
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 /**
25  * This example shows how to use LogFactor5 with other Log4J appenders
26  * (In this case the RollingFileAppender).
27  *
28  * The following lines can be added to the log4j.properties file or a
29  * standard Java properties file.
30  *
31  * # Two appenders are registered with the root of the Category tree.
32  *
33  * log4j.rootCategory=, A1, R
34  *
35  * # A1 is set to be a LF5Appender which outputs to a swing
36  * # logging console.
37  *
38  * log4j.appender.A1=org.apache.log4j.lf5.LF5Appender
39  *
40  * # R is the RollingFileAppender that outputs to a rolling log
41  * # file called rolling_log_file.log.
42  *
43  * log4j.appender.R=org.apache.log4j.RollingFileAppender
44  * log4j.appender.R.File=rolling_log_file.log
45  *
46  * log4j.appender.R.layout=org.apache.log4j.PatternLayout
47  * log4j.appender.R.layout.ConversionPattern=Date - %d{DATE}%nPriority
48  * - %p%nThread - %t%nCategory - %c%nLocation - %l%nMessage - %m%n%n
49  * log4j.appender.R.MaxFileSize=100KB
50  * log4j.appender.R.MaxBackupIndex=1
51  *
52  * To make this example work, either run the InitUsingMultipleAppenders.bat
53  * file located in the examples folder or run it at the command line. If you
54  * are running the example at the command line, you must ensure that the
55  * example.properties file is in your classpath.
56  *
57  * @author Brent Sprecher
58  * @author Brad Marlborough
59  */

60
61 // Contributed by ThoughtWorks Inc.
62

63 public class InitUsingMultipleAppenders {
64
65     //--------------------------------------------------------------------------
66
// Constants:
67
//--------------------------------------------------------------------------
68

69     //--------------------------------------------------------------------------
70
// Protected Variables:
71
//--------------------------------------------------------------------------
72

73     //--------------------------------------------------------------------------
74
// Private Variables:
75
//--------------------------------------------------------------------------
76

77     private static Logger logger =
78             Logger.getLogger(InitUsingMultipleAppenders.class);
79
80     //--------------------------------------------------------------------------
81
// Constructors:
82
//--------------------------------------------------------------------------
83

84     //--------------------------------------------------------------------------
85
// Public Methods:
86
//--------------------------------------------------------------------------
87

88     public static void main(String JavaDoc argv[]) {
89         // Use a PropertyConfigurator to initialize from a property file.
90
String JavaDoc resource =
91                 "/examples/lf5/InitUsingMultipleAppenders/example.properties";
92         URL JavaDoc configFileResource =
93                 InitUsingMultipleAppenders.class.getResource(resource);
94         PropertyConfigurator.configure(configFileResource);
95
96         // Add a bunch of logging statements ...
97
logger.debug("Hello, my name is Homer Simpson.");
98         logger.debug("Hello, my name is Lisa Simpson.");
99         logger.debug("Hello, my name is Marge Simpson.");
100         logger.debug("Hello, my name is Bart Simpson.");
101         logger.debug("Hello, my name is Maggie Simpson.");
102
103         logger.info("We are the Simpsons!");
104         logger.info("Mmmmmm .... Chocolate.");
105         logger.info("Homer likes chocolate");
106         logger.info("Doh!");
107         logger.info("We are the Simpsons!");
108
109         logger.warn("Bart: I am through with working! Working is for chumps!" +
110                 "Homer: Son, I'm proud of you. I was twice your age before " +
111                 "I figured that out.");
112         logger.warn("Mmm...forbidden donut.");
113         logger.warn("D'oh! A deer! A female deer!");
114         logger.warn("Truly, yours is a butt that won't quit." +
115                 "- Bart, writing as Woodrow to Ms. Krabappel.");
116
117         logger.error("Dear Baby, Welcome to Dumpsville. Population: you.");
118         logger.error("Dear Baby, Welcome to Dumpsville. Population: you.",
119                 new IOException JavaDoc("Dumpsville, USA"));
120         logger.error("Mr. Hutz, are you aware you're not wearing pants?");
121         logger.error("Mr. Hutz, are you aware you're not wearing pants?",
122                 new IllegalStateException JavaDoc("Error !!"));
123
124
125         logger.fatal("Eep.");
126         logger.fatal("Mmm...forbidden donut.",
127                 new SecurityException JavaDoc("Fatal Exception"));
128         logger.fatal("D'oh! A deer! A female deer!");
129         logger.fatal("Mmmmmm .... Chocolate.",
130                 new SecurityException JavaDoc("Fatal Exception"));
131     }
132
133     //--------------------------------------------------------------------------
134
// Protected Methods:
135
//--------------------------------------------------------------------------
136

137     //--------------------------------------------------------------------------
138
// Private Methods:
139
//--------------------------------------------------------------------------
140

141     //--------------------------------------------------------------------------
142
// Nested Top-Level Classes or Interfaces:
143
//--------------------------------------------------------------------------
144

145 }
146
Popular Tags