KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > lf5 > UsingSocketAppenders


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.lf5.UsingSocketAppenders;
18
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.PropertyConfigurator;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 /**
26  * This is another simple example of how to use the LogFactor5
27  * logging console.
28  *
29  * The LF5Appender is the primary class that enables logging to the
30  * LogFactor5 logging window. If the following line is added to a properties
31  * file, the LF5Appender will be appended to the root category when
32  * the properties file is loaded:
33  *
34  * log4j.appender.A1=org.apache.log4j.lf5.LF5Appender
35  *
36  * To make this example work, you must ensure that the example.properties file
37  * is in your classpath.You can then run the example at the command line.
38  *
39  * @author Brent Sprecher
40  */

41
42 // Contributed by ThoughtWorks Inc.
43

44 public class UsingSocketAppenders {
45     //--------------------------------------------------------------------------
46
// Constants:
47
//--------------------------------------------------------------------------
48

49     //--------------------------------------------------------------------------
50
// Protected Variables:
51
//--------------------------------------------------------------------------
52

53     //--------------------------------------------------------------------------
54
// Private Variables:
55
//--------------------------------------------------------------------------
56

57     private static Logger logger1 =
58             Logger.getLogger(UsingSocketAppenders.class);
59     private static Logger logger2 =
60             Logger.getLogger("TestClass.Subclass");
61     private static Logger logger3 =
62             Logger.getLogger("TestClass.Subclass.Subclass");
63     //--------------------------------------------------------------------------
64
// Constructors:
65
//--------------------------------------------------------------------------
66

67     //--------------------------------------------------------------------------
68
// Public Methods:
69
//--------------------------------------------------------------------------
70

71     public static void main(String JavaDoc argv[]) {
72         // Use a PropertyConfigurator to initialize from a property file.
73
String JavaDoc resource =
74                 "/examples/lf5/UsingSocketAppenders/socketclient.properties";
75         URL JavaDoc configFileResource =
76                 UsingSocketAppenders.class.getResource(resource);
77         PropertyConfigurator.configure(configFileResource);
78
79         // Add a bunch of logging statements ...
80
logger1.debug("Hello, my name is Homer Simpson.");
81         logger1.debug("Hello, my name is Lisa Simpson.");
82         logger2.debug("Hello, my name is Marge Simpson.");
83         logger2.debug("Hello, my name is Bart Simpson.");
84         logger3.debug("Hello, my name is Maggie Simpson.");
85
86         logger2.info("We are the Simpsons!");
87         logger2.info("Mmmmmm .... Chocolate.");
88         logger3.info("Homer likes chocolate");
89         logger3.info("Doh!");
90         logger3.info("We are the Simpsons!");
91
92         logger1.warn("Bart: I am through with working! Working is for chumps!" +
93                 "Homer: Son, I'm proud of you. I was twice your age before " +
94                 "I figured that out.");
95         logger1.warn("Mmm...forbidden donut.");
96         logger1.warn("D'oh! A deer! A female deer!");
97         logger1.warn("Truly, yours is a butt that won't quit." +
98                 "- Bart, writing as Woodrow to Ms. Krabappel.");
99
100         logger2.error("Dear Baby, Welcome to Dumpsville. Population: you.");
101         logger2.error("Dear Baby, Welcome to Dumpsville. Population: you.",
102                 new IOException JavaDoc("Dumpsville, USA"));
103         logger3.error("Mr. Hutz, are you aware you're not wearing pants?");
104         logger3.error("Mr. Hutz, are you aware you're not wearing pants?",
105                 new IllegalStateException JavaDoc("Error !!"));
106
107
108         logger3.fatal("Eep.");
109
110         logger3.fatal("Mmm...forbidden donut.",
111                 new SecurityException JavaDoc("Fatal Exception ... "));
112
113         logger3.fatal("D'oh! A deer! A female deer!");
114         logger2.fatal("Mmmmmm .... Chocolate.",
115                 new SecurityException JavaDoc("Fatal Exception"));
116
117         // Put the main thread is put to sleep for 5 seconds to allow the
118
// SocketServer to process all incoming messages before the Socket is
119
// closed. This is done to overcome some basic limitations with the
120
// way the SocketServer and SocketAppender classes manage sockets.
121
try {
122             Thread.currentThread().sleep(5000);
123         } catch (InterruptedException JavaDoc ie) {
124         }
125
126     }
127
128     //--------------------------------------------------------------------------
129
// Protected Methods:
130
//--------------------------------------------------------------------------
131

132     //--------------------------------------------------------------------------
133
// Private Methods:
134
//--------------------------------------------------------------------------
135

136     //--------------------------------------------------------------------------
137
// Nested Top-Level Classes or Interfaces:
138
//--------------------------------------------------------------------------
139

140 }
141
Popular Tags