View Javadoc
1   package org.codehaus.plexus.logging.console;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  import java.util.Locale;
21  import java.util.Map;
22  
23  import org.codehaus.plexus.logging.AbstractLoggerManager;
24  import org.codehaus.plexus.logging.Logger;
25  import org.codehaus.plexus.logging.LoggerManager;
26  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
27  
28  /**
29   * This is a simple logger manager that will only write the logging statements to the console.
30   *
31   * Sample configuration:
32   * <pre>{@code
33   * <logging>
34   *   <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
35   *   <logger>
36   *     <threshold>DEBUG</threshold>
37   *   </logger>
38   * </logging>
39   * }</pre>
40   *
41   * @author Jason van Zyl
42   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
43   */
44  public class ConsoleLoggerManager extends AbstractLoggerManager implements LoggerManager, Initializable {
45      /**
46       * Message of this level or higher will be logged.
47       *
48       * This field is set by the plexus container thus the name is 'threshold'. The field
49       * currentThreshold contains the current setting of the threshold.
50       */
51      private String threshold = "info";
52  
53      private int currentThreshold;
54  
55      private Map loggers;
56  
57      /** The number of active loggers in use. */
58      private int loggerCount;
59  
60      private boolean bootTimeLogger = false;
61  
62      public ConsoleLoggerManager() {}
63  
64      /**
65       * This special constructor is called directly when the container is bootstrapping itself.
66       * @param threshold The threshold.
67       */
68      public ConsoleLoggerManager(String threshold) {
69          this.threshold = threshold;
70  
71          bootTimeLogger = true;
72  
73          initialize();
74      }
75  
76      public void initialize() {
77          debug("Initializing ConsoleLoggerManager: " + this.hashCode() + ".");
78          //        if ( !bootTimeLogger )
79          //            new Throwable().printStackTrace(System.err);
80          currentThreshold = parseThreshold(threshold);
81  
82          if (currentThreshold == -1) {
83              debug("Could not parse the threshold level: '" + threshold + "', setting to debug.");
84              currentThreshold = Logger.LEVEL_DEBUG;
85          }
86  
87          loggers = new HashMap();
88      }
89  
90      public void setThreshold(int currentThreshold) {
91          this.currentThreshold = currentThreshold;
92      }
93  
94      public void setThresholds(int currentThreshold) {
95          this.currentThreshold = currentThreshold;
96  
97          for (Object o : loggers.values()) {
98              Logger logger = (Logger) o;
99              logger.setThreshold(currentThreshold);
100         }
101     }
102 
103     /**
104      * @return Returns the threshold.
105      */
106     public int getThreshold() {
107         return currentThreshold;
108     }
109 
110     // new stuff
111 
112     public void setThreshold(String role, String roleHint, int threshold) {
113         ConsoleLogger logger;
114         String name;
115 
116         name = toMapKey(role, roleHint);
117         logger = (ConsoleLogger) loggers.get(name);
118 
119         if (logger == null) {
120             debug("Trying to set the threshold of a unknown logger '" + name + "'.");
121             return; // nothing to do
122         }
123 
124         logger.setThreshold(threshold);
125     }
126 
127     public int getThreshold(String role, String roleHint) {
128         ConsoleLogger logger;
129         String name;
130 
131         name = toMapKey(role, roleHint);
132         logger = (ConsoleLogger) loggers.get(name);
133 
134         if (logger == null) {
135             debug("Trying to get the threshold of a unknown logger '" + name + "'.");
136             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
137         }
138 
139         return logger.getThreshold();
140     }
141 
142     public Logger createLogger(int threshold, String name) {
143         return new ConsoleLogger(threshold, name);
144     }
145 
146     public Logger getLoggerForComponent(String role, String roleHint) {
147         Logger logger;
148         String name;
149 
150         name = toMapKey(role, roleHint);
151         logger = (Logger) loggers.get(name);
152 
153         if (logger != null) return logger;
154 
155         debug("Creating logger '" + name + "' " + this.hashCode() + ".");
156         logger = createLogger(getThreshold(), name);
157         loggers.put(name, logger);
158 
159         return logger;
160     }
161 
162     public void returnComponentLogger(String role, String roleHint) {
163         Object obj;
164         String name;
165 
166         name = toMapKey(role, roleHint);
167         obj = loggers.remove(name);
168 
169         if (obj == null) {
170             debug("There was no such logger '" + name + "' " + this.hashCode() + ".");
171         } else {
172             debug("Removed logger '" + name + "' " + this.hashCode() + ".");
173         }
174     }
175 
176     public int getActiveLoggerCount() {
177         return loggers.size();
178     }
179 
180     private int parseThreshold(String text) {
181         text = text.trim().toLowerCase(Locale.ENGLISH);
182 
183         if (text.equals("debug")) {
184             return ConsoleLogger.LEVEL_DEBUG;
185         } else if (text.equals("info")) {
186             return ConsoleLogger.LEVEL_INFO;
187         } else if (text.equals("warn")) {
188             return ConsoleLogger.LEVEL_WARN;
189         } else if (text.equals("error")) {
190             return ConsoleLogger.LEVEL_ERROR;
191         } else if (text.equals("fatal")) {
192             return ConsoleLogger.LEVEL_FATAL;
193         }
194 
195         return -1;
196     }
197 
198     private String decodeLogLevel(int logLevel) {
199         switch (logLevel) {
200             case ConsoleLogger.LEVEL_DEBUG:
201                 return "debug";
202             case ConsoleLogger.LEVEL_INFO:
203                 return "info";
204             case ConsoleLogger.LEVEL_WARN:
205                 return "warn";
206             case ConsoleLogger.LEVEL_ERROR:
207                 return "error";
208             case ConsoleLogger.LEVEL_FATAL:
209                 return "fatal";
210             case ConsoleLogger.LEVEL_DISABLED:
211                 return "disabled";
212             default:
213                 return "unknown";
214         }
215     }
216 
217     /**
218      * Remove this method and all references when this code is verified.
219      *
220      * @param msg
221      */
222     private void debug(String msg) {
223         //        if ( !bootTimeLogger )
224         //            System.out.println( "[Console] " + msg );
225     }
226 }