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, Object arg0, Object arg1)
71      {
72          if (_debug)
73          {
74              try
75              {
76                  _debugMAA.invoke(_logger,msg,arg0,arg1);
77              }
78              catch (Exception e)
79              {
80                  e.printStackTrace();
81              }
82          }
83      }
84  
85      public Logger getLogger(String name)
86      {
87          try
88          {
89              Object logger=_getLoggerN.invoke(_logger,name);
90              return new LoggerLog(logger);
91          }
92          catch (Exception e)
93          {
94              e.printStackTrace();
95          }
96          return this;
97      }
98  
99      public void info(String msg, Object arg0, Object arg1)
100     {
101         try
102         {
103             _infoMAA.invoke(_logger,msg,arg0,arg1);
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace();
108         }
109     }
110 
111     public boolean isDebugEnabled()
112     {
113         return _debug;
114     }
115 
116     public void setDebugEnabled(boolean enabled)
117     {
118         try
119         {
120             _setDebugEnabledE.invoke(_logger,enabled);
121             _debug=enabled;
122         }
123         catch (Exception e)
124         {
125             e.printStackTrace();
126         }
127         
128     }
129 
130     public void warn(String msg, Object arg0, Object arg1)
131     {
132         try
133         {
134             _warnMAA.invoke(_logger,msg,arg0,arg1);
135         }
136         catch (Exception e)
137         {
138             e.printStackTrace();
139         }
140     }
141 
142     public void warn(String msg, Throwable th)
143     {
144         try
145         {
146             _warnMT.invoke(_logger,msg,th);
147         }
148         catch (Exception e)
149         {
150             e.printStackTrace();
151         }
152     }
153 
154 }