1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.log4j;
22  
23  import org.apache.log4j.spi.LoggingEvent;
24  import org.apache.log4j.AppenderSkeleton;
25  
26  import java.io.ObjectOutputStream;
27  import java.io.ByteArrayOutputStream;
28  
29  /***
30   * A bogus appender which calls the format method of its layout object but does not write the result
31   * anywhere.
32   *  
33   */
34  public class NullAppender extends AppenderSkeleton {
35  
36      static long eventCnt;
37      
38      public NullAppender() {
39      }
40  
41      public void close() {
42          System.out.println("Close. evenCtn=" + eventCnt);
43      }
44  
45      public void append(LoggingEvent event) {
46          eventCnt ++;
47          if ((eventCnt % 2000) == 0) {
48              Object obj = event.getMessage();
49              String message = obj.toString();
50              String ctn = "Cnt=" + eventCnt + " " + message;
51              if (obj instanceof ProfilerEvent) {
52                  printSize(ctn + " ProfilerEvent", obj);
53              } else  if (obj instanceof ProfilerEventExt) {
54                  printSize(ctn + " ProfilerEventExt", obj);
55              } else {
56                  printSize(ctn + " LoggingEvent", event);
57              }
58          }
59      }
60      
61      public void printSize(String name, Object obj) {
62          ObjectOutputStream out = null;
63  		try {
64  			ByteArrayOutputStream baos = new ByteArrayOutputStream();
65  			out = new ObjectOutputStream(baos);
66  			out.writeObject(obj);
67  			System.out.println(name + " size=" + baos.size());
68  		} catch (java.io.NotSerializableException ex) {
69  			System.out.println(name + " data \"" + ex.getMessage() + "\" is not serializable!");
70  		} catch (java.io.IOException e) {
71  			System.out.println("Serialization error!");
72  		} finally {
73  			if (out != null) {
74  				try {
75  					out.close();
76  				} catch (Exception ignore) {
77  				}
78  			}
79  		}
80      }
81  
82      public boolean requiresLayout() {
83          return false;
84      }
85  }