root/trunk/src/java/org/jcoderz/commons/tracing/JulTracer.java

Revision 1628, 7.6 kB (checked in by amandel, 2 years ago)

Add work in progress tracing components.
- Extended TracingProxy? that allows different Traces to be used
- TracingInjector? that allows aspect like injection of the tracing aspect.

Please note that this is still work in progress.

  • Property svn:mime-type set to text/plain
Line 
1/*
2 * $Id: ArraysUtil.java 1011 2008-06-16 17:57:36Z amandel $
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.tracing;
34
35import java.util.logging.Level;
36import java.util.logging.Logger;
37
38/**
39 * A Tracer implementation that used {@link java.util.logging}
40 * entering exiting logger as tracing sink.
41 * See {@link Logger#entering(String, String)} for an example.
42 *
43 * @author Andreas Mandel
44 */
45public class JulTracer
46    implements Tracer
47{
48    /** The backing java logger. */
49    private final Logger mLogger;
50    /** Name of the class where this logger belongs to. */
51    private final String mClassName;
52   
53   
54    /**
55     * Create a new instance of this Tracer.
56     * The underlying java logger is instantly created.
57     * @param tracerName the name of the tracer to be created, should be
58     *     equal to the name of the class that uses the tracer.
59     */
60    private JulTracer (String tracerName)
61    {
62        mLogger = Logger.getLogger(tracerName);
63        mClassName = tracerName;
64    }
65
66    /**
67     * Factory for a tracer for classes with the given name.
68     * @param className the name of the class that uses this tracer.
69     * @return a tracer for the given class that dumps trace output to
70     *      java logging.
71     */
72    public static JulTracer getTracer (String className)
73    {
74        return new JulTracer(className);
75    }
76
77
78    /**
79     * Returns true, if the level of the underlying java logger is
80     * {@link Level#FINER} or below.
81     * @see org.jcoderz.commons.tracing.Tracer#isTracing()
82     */
83    public boolean isTracing ()
84    {
85        return mLogger.isLoggable(Level.FINER);
86    }
87
88    /**
89     * Returns always true.
90     * @see org.jcoderz.commons.tracing.Tracer#isTracingArguments()
91     */
92    public boolean isTracingArguments ()
93    {
94        return true;
95    }
96
97    /**
98     * Returns the Name of the class where this tracer belongs to.
99     * @return the Name of the class where this tracer belongs to.
100     */
101    public String getClassName ()
102    {
103        return mClassName;
104    }
105
106    /**
107     * Delegates to {@link Logger#entering(String, String)}.
108     * The className is expected to be the same as the tracer name and
109     * is silently ignored.
110     * @see Tracer#entering(java.lang.String, java.lang.String)
111     */
112    public TracingToken entering (String className, String methodName)
113    {
114        final TracingToken result;
115        if (isTracing())
116        {
117            mLogger.entering(getClassName(), methodName);
118            result = new JulTracingToken(methodName);
119        }
120        else
121        {
122            result = null;
123        }
124        return result;
125    }
126
127    /**
128     * Delegates to {@link Logger#entering(String, String, Object)}.
129     * The className is expected to be the same as the tracer name and
130     * is silently ignored.
131     * @see Tracer#entering(String, String, Object)
132     */
133    public TracingToken entering (String className, String methodName,
134        Object argument)
135    {
136        final TracingToken result;
137        if (isTracing())
138        {
139            mLogger.entering(getClassName(), methodName, argument);
140            result = new JulTracingToken(methodName);
141        }
142        else
143        {
144            result = null;
145        }
146        return result;
147    }
148
149    /**
150     * Delegates to {@link Logger#entering(String, String, Object)}.
151     * The className is expected to be the same as the tracer name and
152     * is silently ignored.
153     * @see Tracer#entering(String, String, Object[])
154     */
155    public TracingToken entering (String className, String methodName,
156        Object[] arguments)
157    {
158        final TracingToken result;
159        if (isTracing())
160        {
161            mLogger.entering(getClassName(), methodName, arguments);
162            result = new JulTracingToken(methodName);
163        }
164        else
165        {
166            result = null;
167        }
168        return result;
169    }
170
171    /**
172     * Delegates to {@link Logger#exiting(String, String)}.
173     * If the token is not a {@link JulTracingToken} the call is
174     * silently ignored.
175     * @see Tracer#exiting(TracingToken)
176     */
177    public void exiting (TracingToken token)
178    {
179        if (token instanceof JulTracingToken)
180        {
181            final JulTracingToken julTracingToken = (JulTracingToken) token;
182            mLogger.exiting(getClassName(), julTracingToken.getMethodName());
183        }
184    }
185
186    /**
187     * Delegates to {@link Logger#exiting(String, String)}.
188     * If the token is not a {@link JulTracingToken} the call is
189     * silently ignored.
190     * @see Tracer#exiting(TracingToken, java.lang.Object)
191     */
192    public void exiting (TracingToken token, Object result)
193    {
194        if (token instanceof JulTracingToken)
195        {
196            final JulTracingToken julTracingToken = (JulTracingToken) token;
197            mLogger.exiting(
198                getClassName(), julTracingToken.getMethodName(), result);
199        }
200    }
201
202    /**
203     * Delegates to {@link Logger#throwing(String, String, Throwable)}.
204     * If the token is not a {@link JulTracingToken} the call is
205     * silently ignored.
206     * @see Tracer#throwing(TracingToken, java.lang.Throwable)
207     */
208    public void throwing (TracingToken token, Throwable thrown)
209    {
210        if (token instanceof JulTracingToken)
211        {
212            final JulTracingToken julTracingToken = (JulTracingToken) token;
213            mLogger.throwing(getClassName(), julTracingToken.getMethodName(),
214                thrown);
215        }
216    }
217   
218    /**
219     * Stores context information for the {@link JulTracer}.
220     * @see TracingToken
221     * @author Andreas Mandel
222     */
223    private class JulTracingToken implements TracingToken
224    {
225        private final String mMethodName;
226       
227        public JulTracingToken (String methodName)
228        {
229            mMethodName = methodName;
230        }
231
232        public String getMethodName ()
233        {
234            return mMethodName;
235        }
236       
237    }
238}
Note: See TracBrowser for help on using the browser.