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      public void debug(String msg, Throwable th)
56      {
57          if (_debug)
58          {
59              try
60              {
61                  _debugMT.invoke(_logger,msg,th);
62              }
63              catch (Exception e)
64              {
65                  e.printStackTrace();
66              }
67          }
68      }
69  
70      public void debug(String msg)
71      {
72          if (_debug)
73          {
74              try
75              {
76                  _debugMAA.invoke(_logger,msg,null,null);
77              }
78              catch (Exception e)
79              {
80                  e.printStackTrace();
81              }
82          }
83      }
84  
85      public void debug(String msg, Object arg0, Object arg1)
86      {
87          if (_debug)
88          {
89              try
90              {
91                  _debugMAA.invoke(_logger,msg,arg0,arg1);
92              }
93              catch (Exception e)
94              {
95                  e.printStackTrace();
96              }
97          }
98      }
99  
100     public Logger getLogger(String name)
101     {
102         try
103         {
104             Object logger=_getLoggerN.invoke(_logger,name);
105             return new LoggerLog(logger);
106         }
107         catch (Exception e)
108         {
109             e.printStackTrace();
110         }
111         return this;
112     }
113 
114     public void info(String msg)
115     {
116         try
117         {
118             _infoMAA.invoke(_logger,msg,null,null);
119         }
120         catch (Exception e)
121         {
122             e.printStackTrace();
123         }
124     }
125 
126     public void info(String msg, Object arg0, Object arg1)
127     {
128         try
129         {
130             _infoMAA.invoke(_logger,msg,arg0,arg1);
131         }
132         catch (Exception e)
133         {
134             e.printStackTrace();
135         }
136     }
137 
138     public boolean isDebugEnabled()
139     {
140         return _debug;
141     }
142 
143     public void setDebugEnabled(boolean enabled)
144     {
145         try
146         {
147             _setDebugEnabledE.invoke(_logger,enabled);
148             _debug=enabled;
149         }
150         catch (Exception e)
151         {
152             e.printStackTrace();
153         }
154         
155     }
156 
157     public void warn(String msg)
158     {
159         try
160         {
161             _warnMAA.invoke(_logger,msg,null,null);
162         }
163         catch (Exception e)
164         {
165             e.printStackTrace();
166         }
167     }
168 
169     public void warn(String msg, Object arg0, Object arg1)
170     {
171         try
172         {
173             _warnMAA.invoke(_logger,msg,arg0,arg1);
174         }
175         catch (Exception e)
176         {
177             e.printStackTrace();
178         }
179     }
180 
181     public void warn(String msg, Throwable th)
182     {
183         try
184         {
185             _warnMT.invoke(_logger,msg,th);
186         }
187         catch (Exception e)
188         {
189             e.printStackTrace();
190         }
191     }
192 
193 }