View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.chainsaw;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.log4j.Level;
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.MDC;
26  import org.apache.log4j.NDC;
27  import org.apache.log4j.helpers.Constants;
28  import org.apache.log4j.plugins.Receiver;
29  import org.apache.log4j.spi.LocationInfo;
30  import org.apache.log4j.spi.LoggingEvent;
31  import org.apache.log4j.spi.ThrowableInformation;
32  
33  
34  /**
35   * Class designed to stress, and/or test the Chainsaw GUI by sending it
36   * lots of Logging Events.
37   *
38   * @author Scott Deboy <sdeboy@apache.org>
39   *
40   */
41  public class Generator extends Receiver implements Runnable {
42    private static final Logger logger1 =
43      Logger.getLogger("com.mycompany.mycomponentA");
44    private static final Logger logger2 =
45      Logger.getLogger("com.mycompany.mycomponentB");
46    private static final Logger logger3 =
47      Logger.getLogger("com.someothercompany.corecomponent");
48    private final String baseString_;
49    private Thread thread;
50    private boolean shutdown;
51  
52    public Generator(String name) {
53      setName(name);
54      baseString_ = name;
55    }
56  
57    private LoggingEvent createEvent(
58      Level level, Logger logger, String msg, Throwable t) {
59        ThrowableInformation ti = new ThrowableInformation(t);
60        Map properties = new HashMap();
61        properties.put(Constants.APPLICATION_KEY, getName());
62        properties.put(Constants.HOSTNAME_KEY, "localhost");
63        LocationInfo li = new LocationInfo("file", logger.getClass().getName(), "method", "123");
64      LoggingEvent e = new LoggingEvent(
65          logger.getClass().getName(), logger, System.currentTimeMillis(), level, msg, "Thread=1", ti, "NDC value", li, properties);
66      return e;
67    }
68  
69    public void run() {
70      NDC.push(baseString_);
71      MDC.put("some string", "some value" + baseString_);
72  
73      int i = 0;
74  
75      while (!shutdown) {
76       doPost(createEvent(Level.TRACE, logger1, "tracemsg" + i++, null));
77       doPost(
78          createEvent(
79            Level.DEBUG, logger1,
80            "debugmsg " + i++
81            + " g dg sdfa sadf sdf safd fsda asfd sdfa sdaf asfd asdf fasd fasd adfs fasd adfs fads afds afds afsd afsd afsd afsd afsd fasd asfd asfd afsd fasd afsd",
82            null));
83       doPost(createEvent(Level.INFO, logger1, "infomsg " + i++, null));
84       doPost(createEvent(Level.WARN, logger1, "warnmsg " + i++, null));
85       doPost(createEvent(Level.ERROR, logger1, "errormsg " + i++, null));
86       doPost(createEvent(Level.FATAL, logger1, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
87       doPost(createEvent(Level.TRACE, logger2, "tracemsg" + i++, null));
88       doPost(
89          createEvent(
90            Level.DEBUG, logger2,
91            "debugmsg " + i++
92            + " g dg sdfa sadf sdf safd fsda asfd sdfa sdaf asfd asdf fasd fasd adfs fasd adfs fads afds afds afsd afsd afsd afsd afsd fasd asfd asfd afsd fasd afsd",
93            null));
94       doPost(createEvent(Level.INFO, logger2, "infomsg " + i++, null));
95       doPost(createEvent(Level.WARN, logger2, "warnmsg " + i++, null));
96       doPost(createEvent(Level.ERROR, logger2, "errormsg " + i++, null));
97       doPost(createEvent(Level.FATAL, logger2, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
98       doPost(createEvent(Level.TRACE, logger3, "tracemsg" + i++, null));
99       doPost(
100         createEvent(
101           Level.DEBUG, logger3,
102           "debugmsg " + i++
103           + " g dg sdfa sadf sdf safd fsda asfd sdfa sdaf asfd asdf fasd fasd adfs fasd adfs fads afds afds afsd afsd afsd afsd afsd fasd asfd asfd afsd fasd afsd",
104           null));
105      doPost(createEvent(Level.INFO, logger3, "infomsg " + i++, null));
106      doPost(createEvent(Level.WARN, logger3, "warnmsg " + i++, null));
107      doPost(createEvent(Level.ERROR, logger3, "errormsg " + i++, null));
108      doPost(createEvent(Level.FATAL, logger3, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
109 
110       try {
111         Thread.sleep(1000);
112       } catch (InterruptedException ie) {
113       }
114     }
115   }
116 
117   /* (non-Javadoc)
118    * @see org.apache.log4j.plugins.Plugin#shutdown()
119    */
120   public void shutdown() {
121     shutdown = true;
122   }
123 
124   /* (non-Javadoc)
125    * @see org.apache.log4j.spi.OptionHandler#activateOptions()
126    */
127   public void activateOptions() {
128     thread = new Thread(this);
129     thread.setPriority(Thread.MIN_PRIORITY);
130     thread.start();
131   }
132 }