1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.log;
16  
17  import java.lang.reflect.Method;
18  
19  public class LoggerLog implements Logger
20  {
21      boolean _debug;
22      Object _logger;
23      Method _debugMT;
24      Method _debugMAA;
25      Method _infoMAA;
26      Method _warnMT;
27      Method _warnMAA;
28      Method _isDebugEnabled;
29      Method _setDebugEnabledE;
30      Method _getLoggerN;
31      
32      public LoggerLog(Object logger)
33      {
34          try
35          {
36              _logger=logger;
37              Class<?> lc=logger.getClass();
38              _debugMT=lc.getMethod("debug",new Class[]{String.class,Throwable.class});
39              _debugMAA=lc.getMethod("debug",new Class[]{String.class,Object.class,Object.class});
40              _infoMAA=lc.getMethod("info",new Class[]{String.class,Object.class,Object.class});
41              _warnMT=lc.getMethod("warn",new Class[]{String.class,Throwable.class});
42              _warnMAA=lc.getMethod("warn",new Class[]{String.class,Object.class,Object.class});
43              _isDebugEnabled=lc.getMethod("isDebugEnabled",new Class[]{});
44              _setDebugEnabledE=lc.getMethod("setDebugEnabled",new Class[]{Boolean.TYPE});
45              _getLoggerN=lc.getMethod("getLogger",new Class[]{String.class});
46              
47              _debug=((Boolean)_isDebugEnabled.invoke(_logger,(Object[])null)).booleanValue();
48          }
49          catch(Exception e)
50          {
51              e.printStackTrace();
52              throw new IllegalStateException(e);
53          }
54      }
55      
56      public void debug(String msg, Throwable th)
57      {
58          if (_debug)
59          {
60              try
61              {
62                  _debugMT.invoke(_logger,msg,th);
63              }
64              catch (Exception e)
65              {
66                  e.printStackTrace();
67              }
68          }
69      }
70  
71      public void debug(String msg, Object arg0, Object arg1)
72      {
73          if (_debug)
74          {
75              try
76              {
77                  _debugMAA.invoke(_logger,msg,arg0,arg1);
78              }
79              catch (Exception e)
80              {
81                  e.printStackTrace();
82              }
83          }
84      }
85  
86      public Logger getLogger(String name)
87      {
88          try
89          {
90              Object logger=_getLoggerN.invoke(_logger,name);
91              return new LoggerLog(logger);
92          }
93          catch (Exception e)
94          {
95              e.printStackTrace();
96          }
97          return this;
98      }
99  
100     public void info(String msg, Object arg0, Object arg1)
101     {
102         try
103         {
104             _infoMAA.invoke(_logger,msg,arg0,arg1);
105         }
106         catch (Exception e)
107         {
108             e.printStackTrace();
109         }
110     }
111 
112     public boolean isDebugEnabled()
113     {
114         return _debug;
115     }
116 
117     public void setDebugEnabled(boolean enabled)
118     {
119         try
120         {
121             _setDebugEnabledE.invoke(_logger,enabled);
122             _debug=enabled;
123         }
124         catch (Exception e)
125         {
126             e.printStackTrace();
127         }
128         
129     }
130 
131     public void warn(String msg, Object arg0, Object arg1)
132     {
133         try
134         {
135             _warnMAA.invoke(_logger,msg,arg0,arg1);
136         }
137         catch (Exception e)
138         {
139             e.printStackTrace();
140         }
141     }
142 
143     public void warn(String msg, Throwable th)
144     {
145         try
146         {
147             _warnMT.invoke(_logger,msg,th);
148         }
149         catch (Exception e)
150         {
151             e.printStackTrace();
152         }
153     }
154 
155 }