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.LoggingEvent;
27  
28  
29  /***
30   * Class designed to stress, and/or test the Chainsaw GUI by sending it
31   * lots of Logging Events.
32   *
33   * @author Scott Deboy <sdeboy@apache.org>
34   *
35   */
36  public class Generator extends Receiver implements Runnable {
37    private static final Logger logger1 =
38      Logger.getLogger("com.mycompany.mycomponentA");
39    private static final Logger logger2 =
40      Logger.getLogger("com.mycompany.mycomponentB");
41    private static final Logger logger3 =
42      Logger.getLogger("com.someothercompany.corecomponent");
43    private final String baseString_;
44    private Thread thread;
45    private boolean shutdown;
46  
47    public Generator(String name) {
48      setName(name);
49      baseString_ = name;
50    }
51  
52    private LoggingEvent createEvent(
53      Level level, Logger logger, String msg, Throwable t) {
54      LoggingEvent e = new LoggingEvent(
55          logger.getClass().getName(), logger, level, msg, t);
56      e.setProperty(Constants.APPLICATION_KEY, getName());
57      e.setProperty(Constants.HOSTNAME_KEY, "localhost");
58  
59      return e;
60    }
61  
62    public void run() {
63      NDC.push(baseString_);
64      MDC.put("some string", "some value" + baseString_);
65  
66      int i = 0;
67  
68      while (!shutdown) {
69       doPost(createEvent(Level.TRACE, logger1, "tracemsg" + i++, new Exception("someexception-" + baseString_)));
70       doPost(
71          createEvent(
72            Level.DEBUG, logger1,
73            "debugmsg " + i++
74            + " 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",
75            new Exception("someexception-" + baseString_)));
76       doPost(createEvent(Level.INFO, logger1, "infomsg " + i++, new Exception("someexception-" + baseString_)));
77       doPost(createEvent(Level.WARN, logger1, "warnmsg " + i++, new Exception("someexception-" + baseString_)));
78       doPost(createEvent(Level.ERROR, logger1, "errormsg " + i++, new Exception("someexception-" + baseString_)));
79       doPost(createEvent(Level.FATAL, logger1, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
80       doPost(createEvent(Level.TRACE, logger2, "tracemsg" + i++, new Exception("someexception-" + baseString_)));
81       doPost(
82          createEvent(
83            Level.DEBUG, logger2,
84            "debugmsg " + i++
85            + " 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",
86            new Exception("someexception-" + baseString_)));
87       doPost(createEvent(Level.INFO, logger2, "infomsg " + i++, new Exception("someexception-" + baseString_)));
88       doPost(createEvent(Level.WARN, logger2, "warnmsg " + i++, new Exception("someexception-" + baseString_)));
89       doPost(createEvent(Level.ERROR, logger2, "errormsg " + i++, new Exception("someexception-" + baseString_)));
90       doPost(createEvent(Level.FATAL, logger2, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
91       doPost(createEvent(Level.TRACE, logger3, "tracemsg" + i++, new Exception("someexception-" + baseString_)));
92       doPost(
93          createEvent(
94            Level.DEBUG, logger3,
95            "debugmsg " + i++
96            + " 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",
97            new Exception("someexception-" + baseString_)));
98       doPost(createEvent(Level.INFO, logger3, "infomsg " + i++, new Exception("someexception-" + baseString_)));
99       doPost(createEvent(Level.WARN, logger3, "warnmsg " + i++, new Exception("someexception-" + baseString_)));
100      doPost(createEvent(Level.ERROR, logger3, "errormsg " + i++, new Exception("someexception-" + baseString_)));
101      doPost(createEvent(Level.FATAL, logger3, "fatalmsg " + i++, new Exception("someexception-" + baseString_)));
102      
103      
104      
105      
106      
107      
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 }