View Javadoc

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