View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 Internet2
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    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  /*
17   * @author mchyzer
18   * $Id: GrouperClientLog.java,v 1.1 2008-12-04 07:51:39 mchyzer Exp $
19   */
20  package edu.internet2.middleware.grouperInstaller.util;
21  
22  import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.logging.Log;
23  
24  
25  /**
26   *
27   */
28  public class GrouperInstallerLog implements Log {
29  
30    /** debug to console */
31    private static ThreadLocal<Boolean> debugToConsole = new ThreadLocal<Boolean>();
32  
33    /**
34     * if we should debug to console
35     * @param theDebugToConsole
36     */
37    public static void assignDebugToConsole(boolean theDebugToConsole) {
38      debugToConsole.set(theDebugToConsole);
39    }
40    
41    /**
42     * if we should debug to console
43     * @return if debug to console
44     */
45    public static boolean debugToConsole() {
46      Boolean debugToConsoleBoolean = debugToConsole.get();
47      return GrouperInstallerUtils.defaultIfNull(debugToConsoleBoolean, false);
48    }
49    
50    /**
51     * debug to console if needed
52     * @param prefix 
53     * @param message
54     * @param t
55     */
56    private static void debugToConsoleIfNeeded(String prefix, Object message, Throwable t) {
57      if (debugToConsole()) {
58        System.err.print(prefix);
59        System.err.println(message);
60        if (t != null) {
61          t.printStackTrace();
62        }
63      }
64    }
65    
66    /** wrap this log */
67    private Log enclosedLog;
68    
69    /**
70     * wrap a logger
71     * @param theLog
72     */
73    public GrouperInstallerLog(Log theLog) {
74      this.enclosedLog = theLog;
75    }
76    
77    /**
78     * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#debug(java.lang.Object)
79     */
80    public void debug(Object message) {
81      debugToConsoleIfNeeded("DEBUG: ", message, null);
82      this.enclosedLog.debug(message);
83    }
84  
85    /**
86     * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
87     */
88    public void debug(Object message, Throwable t) {
89      debugToConsoleIfNeeded("DEBUG: ", message, t);
90      this.enclosedLog.debug(message, t);
91    }
92  
93    /**
94     * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#error(java.lang.Object)
95     */
96    public void error(Object message) {
97      debugToConsoleIfNeeded("ERROR: ", message, null);
98      this.enclosedLog.error(message);
99    }
100 
101   /**
102    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
103    */
104   public void error(Object message, Throwable t) {
105     debugToConsoleIfNeeded("ERROR: ", message, t);
106     this.enclosedLog.error(message, t);
107   }
108 
109   /**
110    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#fatal(java.lang.Object)
111    */
112   public void fatal(Object message) {
113     debugToConsoleIfNeeded("FATAL: ", message, null);
114     this.enclosedLog.fatal(message);
115   }
116 
117   /**
118    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
119    */
120   public void fatal(Object message, Throwable t) {
121     debugToConsoleIfNeeded("FATAL: ", message, t);
122     this.enclosedLog.fatal(message, t);
123   }
124 
125   /**
126    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#info(java.lang.Object)
127    */
128   public void info(Object message) {
129     debugToConsoleIfNeeded("INFO: ", message, null);
130     this.enclosedLog.info(message);
131   }
132 
133   /**
134    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
135    */
136   public void info(Object message, Throwable t) {
137     debugToConsoleIfNeeded("INFO: ", message, t);
138     this.enclosedLog.info(message, t);
139   }
140 
141   /**
142    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isDebugEnabled()
143    */
144   public boolean isDebugEnabled() {
145     return this.enclosedLog.isDebugEnabled();
146   }
147 
148   /**
149    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isErrorEnabled()
150    */
151   public boolean isErrorEnabled() {
152     return this.enclosedLog.isErrorEnabled();
153   }
154 
155   /**
156    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isFatalEnabled()
157    */
158   public boolean isFatalEnabled() {
159     return this.enclosedLog.isFatalEnabled();
160   }
161 
162   /**
163    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isInfoEnabled()
164    */
165   public boolean isInfoEnabled() {
166     return this.enclosedLog.isInfoEnabled();
167   }
168 
169   /**
170    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isTraceEnabled()
171    */
172   public boolean isTraceEnabled() {
173     return this.enclosedLog.isTraceEnabled();
174   }
175 
176   /**
177    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#isWarnEnabled()
178    */
179   public boolean isWarnEnabled() {
180     return this.enclosedLog.isWarnEnabled();
181   }
182 
183   /**
184    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#trace(java.lang.Object)
185    */
186   public void trace(Object message) {
187     debugToConsoleIfNeeded("TRACE: ", message, null);
188     this.enclosedLog.trace(message);
189   }
190 
191   /**
192    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
193    */
194   public void trace(Object message, Throwable t) {
195     debugToConsoleIfNeeded("TRACE: ", message, t);
196     this.enclosedLog.trace(message, t);
197   }
198 
199   /**
200    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#warn(java.lang.Object)
201    */
202   public void warn(Object message) {
203     debugToConsoleIfNeeded("WARN: ", message, null);
204     this.enclosedLog.warn(message);
205   }
206 
207   /**
208    * @see edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
209    */
210   public void warn(Object message, Throwable t) {
211     debugToConsoleIfNeeded("WARN: ", message, t);
212     this.enclosedLog.warn(message, t);
213   }
214 
215 }