]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/log/LoggerLog.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / log / LoggerLog.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util.log;
20
21 import java.lang.reflect.Method;
22
23 /**
24  *
25  */
26 public class LoggerLog extends AbstractLogger
27 {
28     private final Object _logger;
29     private final Method _debugMT;
30     private final Method _debugMAA;
31     private final Method _infoMT;
32     private final Method _infoMAA;
33     private final Method _warnMT;
34     private final Method _warnMAA;
35     private final Method _setDebugEnabledE;
36     private final Method _getLoggerN;
37     private final Method _getName;
38     private volatile boolean _debug;
39
40     public LoggerLog(Object logger)
41     {
42         try
43         {
44             _logger = logger;
45             Class<?> lc = logger.getClass();
46             _debugMT = lc.getMethod("debug", new Class[]{String.class, Throwable.class});
47             _debugMAA = lc.getMethod("debug", new Class[]{String.class, Object[].class});
48             _infoMT = lc.getMethod("info", new Class[]{String.class, Throwable.class});
49             _infoMAA = lc.getMethod("info", new Class[]{String.class, Object[].class});
50             _warnMT = lc.getMethod("warn", new Class[]{String.class, Throwable.class});
51             _warnMAA = lc.getMethod("warn", new Class[]{String.class, Object[].class});
52             Method _isDebugEnabled = lc.getMethod("isDebugEnabled");
53             _setDebugEnabledE = lc.getMethod("setDebugEnabled", new Class[]{Boolean.TYPE});
54             _getLoggerN = lc.getMethod("getLogger", new Class[]{String.class});
55             _getName = lc.getMethod("getName");
56
57             _debug = (Boolean)_isDebugEnabled.invoke(_logger);
58         }
59         catch(Exception x)
60         {
61             throw new IllegalStateException(x);
62         }
63     }
64
65     public String getName()
66     {
67         try
68         {
69             return (String)_getName.invoke(_logger);
70         }
71         catch (Exception e)
72         {
73             e.printStackTrace();
74             return null;
75         }
76     }
77
78     public void warn(String msg, Object... args)
79     {
80         try
81         {
82             _warnMAA.invoke(_logger, args);
83         }
84         catch (Exception e)
85         {
86             e.printStackTrace();
87         }
88     }
89
90     public void warn(Throwable thrown)
91     {
92         warn("", thrown);
93     }
94
95     public void warn(String msg, Throwable thrown)
96     {
97         try
98         {
99             _warnMT.invoke(_logger, msg, thrown);
100         }
101         catch (Exception e)
102         {
103             e.printStackTrace();
104         }
105     }
106
107     public void info(String msg, Object... args)
108     {
109         try
110         {
111             _infoMAA.invoke(_logger, args);
112         }
113         catch (Exception e)
114         {
115             e.printStackTrace();
116         }
117     }
118
119     public void info(Throwable thrown)
120     {
121         info("", thrown);
122     }
123
124     public void info(String msg, Throwable thrown)
125     {
126         try
127         {
128             _infoMT.invoke(_logger, msg, thrown);
129         }
130         catch (Exception e)
131         {
132             e.printStackTrace();
133         }
134     }
135
136     public boolean isDebugEnabled()
137     {
138         return _debug;
139     }
140
141     public void setDebugEnabled(boolean enabled)
142     {
143         try
144         {
145             _setDebugEnabledE.invoke(_logger, enabled);
146             _debug = enabled;
147         }
148         catch (Exception e)
149         {
150             e.printStackTrace();
151         }
152     }
153
154     
155     public void debug(String msg, Object... args)
156     {
157         if (!_debug)
158             return;
159
160         try
161         {
162             _debugMAA.invoke(_logger, args);
163         }
164         catch (Exception e)
165         {
166             e.printStackTrace();
167         }
168     }
169
170     public void debug(Throwable thrown)
171     {
172         debug("", thrown);
173     }
174
175     public void debug(String msg, Throwable th)
176     {
177         if (!_debug)
178             return;
179
180         try
181         {
182             _debugMT.invoke(_logger, msg, th);
183         }
184         catch (Exception e)
185         {
186             e.printStackTrace();
187         }
188     }
189
190     public void debug(String msg, long value)
191     {
192         if (!_debug)
193             return;
194
195         try
196         {
197             _debugMAA.invoke(_logger, new Object[]{new Long(value)});
198         }
199         catch (Exception e)
200         {
201             e.printStackTrace();
202         }
203     }
204     
205     public void ignore(Throwable ignored)
206     {
207         if (Log.isIgnored())
208         {
209             warn(Log.IGNORED, ignored);
210         }
211     }
212
213     /**
214      * Create a Child Logger of this Logger.
215      */
216     protected Logger newLogger(String fullname)
217     {
218         try
219         {
220             Object logger=_getLoggerN.invoke(_logger, fullname);
221             return new LoggerLog(logger);
222         }
223         catch (Exception e)
224         {
225             e.printStackTrace();
226             return this;
227         }
228     }
229 }