View Javadoc

1   // ========================================================================
2   // Copyright 2004-2005 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  import java.lang.reflect.Method;
17  
18  import org.mortbay.util.Loader;
19  
20  
21  
22  /*-----------------------------------------------------------------------*/
23  /** Logging.
24   * This class provides a static logging interface.  If an instance of the 
25   * org.slf4j.Logger class is found on the classpath, the static log methods
26   * are directed to a slf4j logger for "org.mortbay.log".   Otherwise the logs
27   * are directed to stderr.
28   * 
29   * If the system property VERBOSE is set, then ignored exceptions are logged in detail.
30   * 
31   */
32  public class Log 
33  {    
34      private static final String[] __nestedEx =
35          {"getTargetException","getTargetError","getException","getRootCause"};
36      /*-------------------------------------------------------------------*/
37      private static final Class[] __noArgs=new Class[0];
38      public final static String EXCEPTION= "EXCEPTION ";
39      public final static String IGNORED= "IGNORED";
40      public final static String IGNORED_FMT= "IGNORED: {}";
41      public final static String NOT_IMPLEMENTED= "NOT IMPLEMENTED ";
42      
43      private static String logClass=System.getProperty("org.mortbay.log.class","org.mortbay.log.Slf4jLog");
44      private static boolean verbose = System.getProperty("VERBOSE",null)!=null;
45      private static boolean ignored = System.getProperty("IGNORED",null)!=null;
46      private static Logger log;
47     
48      static
49      {
50          Class log_class=null;
51          try
52          {
53              log_class=Loader.loadClass(Log.class, logClass);
54              log=(Logger) log_class.newInstance();
55          }
56          catch(Exception e)
57          {
58              log_class=StdErrLog.class;
59              log=new StdErrLog();
60              if(verbose)
61                  e.printStackTrace();
62          }
63          
64          log.info("Logging to {} via {}",log,log_class.getName());
65      }
66      
67      public static void setLog(Logger log)
68      {
69          Log.log=log;
70      }
71      
72      public static Logger getLog()
73      {
74          return log;
75      }
76      
77      
78      public static void debug(Throwable th)
79      {
80          if (log==null || !isDebugEnabled())
81              return;
82          log.debug(EXCEPTION,th);
83          unwind(th);
84      }
85  
86      public static void debug(String msg)
87      {
88          if (log==null)
89              return;
90          log.debug(msg,null,null);
91      }
92      
93      public static void debug(String msg,Object arg)
94      {
95          if (log==null) 
96              return;
97          log.debug(msg,arg,null);
98      }
99      
100     public static void debug(String msg,Object arg0, Object arg1)
101     {
102         if (log==null)
103             return;
104         log.debug(msg,arg0,arg1);
105     }
106     
107     /* ------------------------------------------------------------ */
108     /**
109      * Ignore an exception unless trace is enabled.
110      * This works around the problem that log4j does not support the trace level.
111      */
112     public static void ignore(Throwable th)
113     {
114         if (log==null)
115             return;
116 	if (ignored)
117 	{
118             log.warn(IGNORED,th);
119             unwind(th);
120 	}
121         else if (verbose)
122         {
123             log.debug(IGNORED,th);
124             unwind(th);
125         }
126     }
127     
128     public static void info(String msg)
129     {
130         if (log==null)
131             return;
132         log.info(msg,null,null);
133     }
134     
135     public static void info(String msg,Object arg)
136     {
137         if (log==null)
138             return;
139         log.info(msg,arg,null);
140     }
141     
142     public static void info(String msg,Object arg0, Object arg1)
143     {
144         if (log==null)
145             return;
146         log.info(msg,arg0,arg1);
147     }
148     
149     public static boolean isDebugEnabled()
150     {
151         if (log==null)
152             return false;
153         return log.isDebugEnabled();
154     }
155     
156     public static void warn(String msg)
157     {
158         if (log==null)
159             return;
160         log.warn(msg,null,null);
161     }
162     
163     public static void warn(String msg,Object arg)
164     {
165         if (log==null)
166             return;
167         log.warn(msg,arg,null);        
168     }
169     
170     public static void warn(String msg,Object arg0, Object arg1)
171     {
172         if (log==null)
173             return;
174         log.warn(msg,arg0,arg1);        
175     }
176     
177     public static void warn(String msg, Throwable th)
178     {
179         if (log==null)
180             return;
181         log.warn(msg,th);
182         unwind(th);
183     }
184 
185     public static void warn(Throwable th)
186     {
187         if (log==null)
188             return;
189         log.warn(EXCEPTION,th);
190         unwind(th);
191     }
192 
193     /** Obtain a named Logger.
194      * Obtain a named Logger or the default Logger if null is passed.
195      */
196     public static Logger getLogger(String name)
197     {
198         if (log==null)
199             return log;
200         if (name==null)
201           return log;
202         return log.getLogger(name);
203     }
204 
205     private static void unwind(Throwable th)
206     {
207         if (th==null)
208             return;
209         for (int i=0;i<__nestedEx.length;i++)
210         {
211             try
212             {
213                 Method get_target = th.getClass().getMethod(__nestedEx[i],__noArgs);
214                 Throwable th2=(Throwable)get_target.invoke(th,(Object[])null);
215                 if (th2!=null && th2!=th)
216                     warn("Nested in "+th+":",th2);
217             }
218             catch(Exception ignore){}
219         }
220     }
221     
222 
223 }
224