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 org.apache.log4j.Level;
21  import org.apache.log4j.Logger;
22  import org.apache.log4j.MDC;
23  import org.apache.log4j.NDC;
24  import org.apache.log4j.helpers.Constants;
25  import org.apache.log4j.plugins.Receiver;
26  import org.apache.log4j.spi.LocationInfo;
27  import org.apache.log4j.spi.LoggingEvent;
28  import org.apache.log4j.spi.ThrowableInformation;
29  
30  import java.util.HashMap;
31  import java.util.Map;
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  public class Generator extends Receiver implements Runnable {
41      private static final Logger logger1 =
42          Logger.getLogger("com.mycompany.mycomponentA");
43      private static final Logger logger2 =
44          Logger.getLogger("com.mycompany.mycomponentB");
45      private static final Logger logger3 =
46          Logger.getLogger("com.someothercompany.corecomponent");
47      private final String baseString_;
48      private Thread thread;
49      private boolean shutdown;
50  
51      public Generator(String name) {
52          setName(name);
53          baseString_ = name;
54      }
55  
56      private LoggingEvent createEvent(
57          Level level, Logger logger, String msg, Throwable t) {
58          ThrowableInformation ti = new ThrowableInformation(t);
59          Map<String, String> properties = new HashMap<>();
60          properties.put(Constants.APPLICATION_KEY, getName());
61          properties.put(Constants.HOSTNAME_KEY, "localhost");
62          LocationInfo li = new LocationInfo("file", logger.getClass().getName(), "method", "123");
63          LoggingEvent e = new LoggingEvent(
64              logger.getClass().getName(), logger, System.currentTimeMillis(), level, msg, "Thread=1", ti, "NDC value", li, properties);
65          return e;
66      }
67  
68      public void run() {
69          NDC.push(baseString_);
70          MDC.put("some string", "some value" + baseString_);
71  
72          int i = 0;
73  
74          while (!shutdown) {
75              doPost(createEvent(Level.TRACE, logger1, "tracemsg" + i++, null));
76              doPost(
77                  createEvent(
78                      Level.DEBUG, logger1,
79                      "debugmsg " + i++
80                          + " 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",
81                      null));
82              doPost(createEvent(Level.INFO, logger1, "infomsg " + i++, null));
83              doPost(createEvent(Level.WARN, logger1, "warnmsg " + i++, null));
84              doPost(createEvent(Level.ERROR, logger1, "errormsg " + i++, null));
85              doPost(createEvent(Level.FATAL, logger1, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
86              doPost(createEvent(Level.TRACE, logger2, "tracemsg" + i++, null));
87              doPost(
88                  createEvent(
89                      Level.DEBUG, logger2,
90                      "debugmsg " + i++
91                          + " 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",
92                      null));
93              doPost(createEvent(Level.INFO, logger2, "infomsg " + i++, null));
94              doPost(createEvent(Level.WARN, logger2, "warnmsg " + i++, null));
95              doPost(createEvent(Level.ERROR, logger2, "errormsg " + i++, null));
96              doPost(createEvent(Level.FATAL, logger2, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
97              doPost(createEvent(Level.TRACE, logger3, "tracemsg" + i++, null));
98              doPost(
99                  createEvent(
100                     Level.DEBUG, logger3,
101                     "debugmsg " + i++
102                         + " 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",
103                     null));
104             doPost(createEvent(Level.INFO, logger3, "infomsg " + i++, null));
105             doPost(createEvent(Level.WARN, logger3, "warnmsg " + i++, null));
106             doPost(createEvent(Level.ERROR, logger3, "errormsg " + i++, null));
107             doPost(createEvent(Level.FATAL, logger3, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
108 
109             try {
110                 Thread.sleep(1000);
111             } catch (InterruptedException ie) {
112             }
113         }
114     }
115 
116     /* (non-Javadoc)
117      * @see org.apache.log4j.plugins.Plugin#shutdown()
118      */
119     public void shutdown() {
120         shutdown = true;
121     }
122 
123     /* (non-Javadoc)
124      * @see org.apache.log4j.spi.OptionHandler#activateOptions()
125      */
126     public void activateOptions() {
127         thread = new Thread(this);
128         thread.setPriority(Thread.MIN_PRIORITY);
129         thread.start();
130     }
131 }