View Javadoc
1   package org.codehaus.plexus.logging;
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.personality.plexus.lifecycle.phase.Initializable;
24  
25  /**
26   * Base class for all LoggerManagers which use cache of Loggers.
27   *
28   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
29   */
30  public abstract class BaseLoggerManager extends AbstractLoggerManager implements Initializable {
31      private Map loggerCache = new HashMap();
32  
33      private String threshold = "info";
34  
35      private int currentThreshold;
36  
37      public void initialize() {
38          currentThreshold = parseThreshold(threshold);
39  
40          if (currentThreshold == -1) {
41              currentThreshold = Logger.LEVEL_DEBUG;
42          }
43      }
44  
45      protected int parseThreshold(String text) {
46          text = text.trim().toLowerCase(Locale.ENGLISH);
47  
48          if (text.equals("debug")) {
49              return Logger.LEVEL_DEBUG;
50          } else if (text.equals("info")) {
51              return Logger.LEVEL_INFO;
52          } else if (text.equals("warn")) {
53              return Logger.LEVEL_WARN;
54          } else if (text.equals("error")) {
55              return Logger.LEVEL_ERROR;
56          } else if (text.equals("fatal")) {
57              return Logger.LEVEL_FATAL;
58          }
59  
60          return -1;
61      }
62  
63      /**
64       * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
65       * <p>This is usually only set once while the logger manager is configured.</p>
66       *
67       * @param currentThreshold The new threshold.
68       */
69      public void setThreshold(int currentThreshold) {
70          this.currentThreshold = currentThreshold;
71      }
72  
73      /**
74       * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
75       * <p>This is usually only set once while the logger manager is configured.</p>
76       *
77       * @param currentThreshold The new threshold.
78       */
79      public void setThresholds(int currentThreshold) {
80          this.currentThreshold = currentThreshold;
81  
82          for (Object o : loggerCache.values()) {
83              Logger logger = (Logger) o;
84              logger.setThreshold(currentThreshold);
85          }
86      }
87  
88      /**
89       * Returns the current threshold for all new loggers.
90       *
91       * @return Returns the current threshold for all new loggers.
92       */
93      public int getThreshold() {
94          return currentThreshold;
95      }
96  
97      public void setThreshold(String role, String roleHint, int threshold) {
98          AbstractLogger logger;
99  
100         String key = toMapKey(role, roleHint);
101 
102         logger = (AbstractLogger) loggerCache.get(key);
103 
104         if (logger == null) {
105             return; // nothing to do
106         }
107 
108         logger.setThreshold(threshold);
109     }
110 
111     public int getThreshold(String role, String roleHint) {
112         AbstractLogger logger;
113 
114         String key = toMapKey(role, roleHint);
115 
116         logger = (AbstractLogger) loggerCache.get(key);
117 
118         if (logger == null) {
119             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
120         }
121 
122         return logger.getThreshold();
123     }
124 
125     public Logger getLoggerForComponent(String role, String roleHint) {
126         Logger logger;
127 
128         String key = toMapKey(role, roleHint);
129 
130         logger = (Logger) loggerCache.get(key);
131 
132         if (logger != null) {
133             return logger;
134         }
135 
136         logger = createLogger(key);
137 
138         loggerCache.put(key, logger);
139 
140         return logger;
141     }
142 
143     protected abstract Logger createLogger(String key);
144 
145     public void returnComponentLogger(String role, String roleHint) {
146         Object obj;
147 
148         String key = toMapKey(role, roleHint);
149 
150         obj = loggerCache.remove(key);
151 
152         if (obj == null) {
153             // TODO: use a logger!
154             System.err.println("There was no such logger '" + key + "' " + hashCode() + ".");
155         }
156     }
157 
158     public int getActiveLoggerCount() {
159         return loggerCache.size();
160     }
161 
162     public String getThresholdAsString() {
163         return threshold;
164     }
165 }