KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > configuration > LogKitTargetAdapter


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.configuration;
7
8 import org.apache.avalon.framework.CascadingThrowable;
9 import org.apache.log.LogEvent;
10 import org.apache.log.Priority;
11 import org.apache.log.output.AbstractOutputTarget;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 /**
16  * An Avalon LogKit target that delegates to a Jakarta Commons <code>org.apache.commons.logging.Log</code>.
17  * Can be used to make Avalons internal logging go the same output as
18  * the Proxool internal logging.
19  * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:06 $
20  * @author Christian Nedregaard (christian_nedregaard@email.com)
21  * @author $Author: billhorsman $ (current maintainer)
22  * @since Proxool 0.6
23  */

24 public class LogKitTargetAdapter extends AbstractOutputTarget {
25     private boolean isOpen = true;
26
27     /**
28      * @see org.apache.log.output.AbstractOutputTarget#doProcessEvent(org.apache.log.LogEvent)
29      */

30     protected void doProcessEvent(LogEvent event) {
31         // we do a workaround for the fact that Avalon CascadingThrowables
32
// does not print the stacktrace of any nested exceptions.
33
// Also we 'upgrade' DEBUG LogEvents containing Throwables
34
// to WARN since Avalon seems to give some important exceptions
35
// DEBUG priority.
36
Throwable JavaDoc originalThrowable = event.getThrowable();
37         Throwable JavaDoc nestedThrowable = null;
38         if (originalThrowable != null && originalThrowable instanceof CascadingThrowable) {
39             nestedThrowable = ((CascadingThrowable) originalThrowable).getCause();
40         }
41         int priority = event.getPriority().getValue();
42         if (originalThrowable != null && event.getPriority().isLower(Priority.WARN)) {
43             priority = Priority.WARN.getValue();
44         }
45         Log logger = LogFactory.getLog(event.getCategory());
46         if (Priority.DEBUG.getValue() == priority) {
47             if (logger.isDebugEnabled()) {
48                 logger.debug(event.getMessage(), originalThrowable);
49             }
50             if (nestedThrowable != null) {
51                 logger.debug("... caused by:", nestedThrowable);
52             }
53         } else if (Priority.INFO.getValue() == priority) {
54             if (logger.isInfoEnabled()) {
55                 logger.info(event.getMessage(), originalThrowable);
56             }
57             if (nestedThrowable != null) {
58                 logger.info("... caused by:", nestedThrowable);
59             }
60         } else if (Priority.WARN.getValue() == priority) {
61             logger.warn(event.getMessage(), originalThrowable);
62             if (nestedThrowable != null) {
63                 logger.warn("... caused by:", nestedThrowable);
64             }
65         } else if (Priority.ERROR.getValue() == priority) {
66             logger.error(event.getMessage(), originalThrowable);
67             if (nestedThrowable != null) {
68                 logger.error("... caused by:", nestedThrowable);
69             }
70         } else if (Priority.FATAL_ERROR.getValue() == priority) {
71             logger.fatal(event.getMessage(), originalThrowable);
72             if (nestedThrowable != null) {
73                 logger.fatal("... caused by:", nestedThrowable);
74             }
75         } else {
76             logger.warn("Got log event of unknown priority: " + priority
77                     + ". Message: " + event.getMessage(), originalThrowable);
78         }
79     }
80
81     /**
82      * @see org.apache.log.output.AbstractOutputTarget#isOpen()
83      */

84     protected boolean isOpen() {
85         return this.isOpen;
86     }
87
88     /**
89      * @see org.apache.log.output.AbstractOutputTarget#open()
90      */

91     protected void open() {
92         this.isOpen = true;
93     }
94
95     /**
96      * @see org.apache.log.output.AbstractOutputTarget#close()
97      */

98     public void close() {
99         this.isOpen = false;
100     }
101
102 }
103
104 /*
105  Revision history:
106  $Log: LogKitTargetAdapter.java,v $
107  Revision 1.6 2006/01/18 14:40:06 billhorsman
108  Unbundled Jakarta's Commons Logging.
109
110  Revision 1.5 2003/03/04 10:24:41 billhorsman
111  removed try blocks around each test
112
113  Revision 1.4 2003/03/03 11:12:06 billhorsman
114  fixed licence
115
116  Revision 1.3 2003/03/01 15:27:25 billhorsman
117  checkstyle
118
119  Revision 1.2 2003/02/06 17:41:03 billhorsman
120  now uses imported logging
121
122  Revision 1.1 2002/12/23 02:39:43 chr32
123  Needed by the AvalonConfigurator tests.
124
125 */
Popular Tags