KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > monitors > Log4JComponentMonitor


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Paul Hammant *
9  *****************************************************************************/

10
11 package org.picocontainer.gems.monitors;
12
13 import java.io.Serializable JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.Member JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17
18 import org.apache.log4j.LogManager;
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.Priority;
21 import org.picocontainer.monitors.AbstractComponentMonitor;
22 import org.picocontainer.monitors.DefaultComponentMonitor;
23 import org.picocontainer.ComponentMonitor;
24
25
26 /**
27  * A {@link org.picocontainer.ComponentMonitor} which writes to a Log4J {@link org.apache.log4j.Logger} instance.
28  * The Logger instance can either be injected or, if not set, the {@link LogManager LogManager}
29  * will be used to retrieve it at every invocation of the monitor.
30  *
31  * @author Paul Hammant
32  * @author Mauro Talevi
33  * @version $Revision: $
34  */

35 public class Log4JComponentMonitor extends AbstractComponentMonitor implements Serializable JavaDoc {
36
37     private Logger logger;
38     private final ComponentMonitor delegate;
39
40     /**
41      * Creates a Log4JComponentMonitor with no Logger instance set.
42      * The {@link LogManager LogManager} will be used to retrieve the Logger instance
43      * at every invocation of the monitor.
44      */

45     public Log4JComponentMonitor() {
46         delegate = new DefaultComponentMonitor();
47     }
48     
49     /**
50      * Creates a Log4JComponentMonitor with a given Logger instance class.
51      * The class name is used to retrieve the Logger instance.
52      *
53      * @param loggerClass the class of the Logger
54      */

55     public Log4JComponentMonitor(Class JavaDoc loggerClass) {
56         this(loggerClass.getName());
57     }
58
59     /**
60      * Creates a Log4JComponentMonitor with a given Logger instance name. It uses the
61      * {@link org.apache.log4j.LogManager LogManager} to create the Logger instance.
62      *
63      * @param loggerName the name of the Log
64      */

65     public Log4JComponentMonitor(String JavaDoc loggerName) {
66         this(LogManager.getLogger(loggerName));
67     }
68
69     /**
70      * Creates a Log4JComponentMonitor with a given Logger instance
71      *
72      * @param logger the Logger to write to
73      */

74     public Log4JComponentMonitor(Logger logger) {
75         this();
76         this.logger = logger;
77     }
78
79     /**
80      * Creates a Log4JComponentMonitor with a given Logger instance class.
81      * The class name is used to retrieve the Logger instance.
82      *
83      * @param loggerClass the class of the Logger
84      */

85     public Log4JComponentMonitor(Class JavaDoc loggerClass, ComponentMonitor delegate) {
86         this(loggerClass.getName(), delegate);
87     }
88
89     /**
90      * Creates a Log4JComponentMonitor with a given Logger instance name. It uses the
91      * {@link org.apache.log4j.LogManager LogManager} to create the Logger instance.
92      *
93      * @param loggerName the name of the Log
94      */

95     public Log4JComponentMonitor(String JavaDoc loggerName, ComponentMonitor delegate) {
96         this(LogManager.getLogger(loggerName), delegate);
97     }
98
99     /**
100      * Creates a Log4JComponentMonitor with a given Logger instance
101      *
102      * @param logger the Logger to write to
103      */

104     public Log4JComponentMonitor(Logger logger, ComponentMonitor delegate) {
105         this(delegate);
106         this.logger = logger;
107     }
108
109     public Log4JComponentMonitor(ComponentMonitor delegate) {
110         this.delegate = delegate;
111     }
112
113     public void instantiating(Constructor JavaDoc constructor) {
114         Logger logger = getLogger(constructor);
115         if (logger.isDebugEnabled()) {
116             logger.debug(format(INSTANTIATING, new Object JavaDoc[]{toString(constructor)}));
117         }
118         delegate.instantiating(constructor);
119     }
120
121     public void instantiated(Constructor JavaDoc constructor, long duration) {
122         Logger logger = getLogger(constructor);
123         if (logger.isDebugEnabled()) {
124             logger.debug(format(INSTANTIATED, new Object JavaDoc[]{toString(constructor), new Long JavaDoc(duration)}));
125         }
126         delegate.instantiated(constructor, duration);
127     }
128
129     public void instantiated(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] parameters, long duration) {
130         Logger logger = getLogger(constructor);
131         if (logger.isDebugEnabled()) {
132             logger.debug(format(INSTANTIATED2, new Object JavaDoc[]{toString(constructor), new Long JavaDoc(duration), instantiated.getClass().getName(), toString(parameters)}));
133         }
134         delegate.instantiated(constructor, instantiated, parameters, duration);
135     }
136
137     public void instantiationFailed(Constructor JavaDoc constructor, Exception JavaDoc cause) {
138         Logger logger = getLogger(constructor);
139         if (logger.isEnabledFor(Priority.WARN)) {
140             logger.warn(format(INSTANTIATION_FAILED, new Object JavaDoc[]{toString(constructor), cause.getMessage()}), cause);
141         }
142         delegate.instantiationFailed(constructor, cause);
143     }
144
145     public void invoking(Method JavaDoc method, Object JavaDoc instance) {
146         Logger logger = getLogger(method);
147         if (logger.isDebugEnabled()) {
148             logger.debug(format(INVOKING, new Object JavaDoc[]{toString(method), instance}));
149         }
150         delegate.invoking(method, instance);
151     }
152
153     public void invoked(Method JavaDoc method, Object JavaDoc instance, long duration) {
154         Logger logger = getLogger(method);
155         if (logger.isDebugEnabled()) {
156             logger.debug(format(INVOKED, new Object JavaDoc[]{toString(method), instance, new Long JavaDoc(duration)}));
157         }
158         delegate.invoked(method, instance, duration);
159     }
160
161     public void invocationFailed(Method JavaDoc method, Object JavaDoc instance, Exception JavaDoc cause) {
162         Logger logger = getLogger(method);
163         if (logger.isEnabledFor(Priority.WARN)) {
164             logger.warn(format(INVOCATION_FAILED, new Object JavaDoc[]{toString(method), instance, cause.getMessage()}), cause);
165         }
166         delegate.invocationFailed(method, instance, cause);
167     }
168
169     public void lifecycleInvocationFailed(Method JavaDoc method, Object JavaDoc instance, RuntimeException JavaDoc cause) {
170         Logger logger = getLogger(method);
171         if (logger.isEnabledFor(Priority.WARN)) {
172             logger.warn(format(LIFECYCLE_INVOCATION_FAILED, new Object JavaDoc[]{toString(method), instance, cause.getMessage()}), cause);
173         }
174         delegate.lifecycleInvocationFailed(method, instance, cause);
175     }
176
177     protected Logger getLogger(Member JavaDoc member) {
178         if ( logger != null ){
179             return logger;
180         }
181         return LogManager.getLogger(member.getDeclaringClass());
182     }
183
184 }
185
Popular Tags