View Javadoc

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