Coverage Report - org.codehaus.plexus.logging.console.ConsoleLoggerManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsoleLoggerManager
64 %
48/75
44 %
13/29
3,067
 
 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 org.codehaus.plexus.logging.AbstractLoggerManager;
 20  
 import org.codehaus.plexus.logging.Logger;
 21  
 import org.codehaus.plexus.logging.LoggerManager;
 22  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 23  
 
 24  
 import java.util.HashMap;
 25  
 import java.util.Iterator;
 26  
 import java.util.Locale;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * This is a simple logger manager that will only write the logging statements to the console.
 31  
  *
 32  
  * Sample configuration:
 33  
  * <pre>
 34  
  * <logging>
 35  
  *   <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
 36  
  *   <logger>
 37  
  *     <threshold>DEBUG</threshold>
 38  
  *   </logger>
 39  
  * </logging>
 40  
  * </pre>
 41  
  * 
 42  
  * @author Jason van Zyl
 43  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 44  
  * @version $Id$
 45  
  */
 46  
 public class ConsoleLoggerManager
 47  
     extends AbstractLoggerManager
 48  
     implements LoggerManager, Initializable
 49  
 {
 50  
     /**
 51  
      * Message of this level or higher will be logged. 
 52  
      * 
 53  
      * This field is set by the plexus container thus the name is 'threshold'. The field
 54  
      * currentThreshold contains the current setting of the threshold.
 55  
      */
 56  69
     private String threshold = "info";
 57  
 
 58  
     private int currentThreshold;
 59  
 
 60  
     private Map loggers;
 61  
 
 62  
     /** The number of active loggers in use. */
 63  
     private int loggerCount;
 64  
 
 65  69
     private boolean bootTimeLogger = false;
 66  
 
 67  
     public ConsoleLoggerManager()
 68  45
     {
 69  45
     }
 70  
 
 71  
     /**
 72  
      * This special constructor is called directly when the container is bootstrapping itself.
 73  
      */
 74  
     public ConsoleLoggerManager( String threshold )
 75  24
     {
 76  24
         this.threshold = threshold;
 77  
 
 78  24
         bootTimeLogger = true;
 79  
 
 80  24
         initialize();
 81  24
     }
 82  
 
 83  
     public void initialize()
 84  
     {
 85  69
         debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." );
 86  
 //        if ( !bootTimeLogger )
 87  
 //            new Throwable().printStackTrace(System.err);
 88  69
         currentThreshold = parseThreshold( threshold );
 89  
 
 90  69
         if ( currentThreshold == -1 )
 91  
         {
 92  0
             debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." );
 93  0
             currentThreshold = Logger.LEVEL_DEBUG;
 94  
         }
 95  
 
 96  69
         loggers = new HashMap();
 97  69
     }
 98  
 
 99  
     public void setThreshold( int currentThreshold )
 100  
     {
 101  9
         this.currentThreshold = currentThreshold;
 102  9
     }
 103  
 
 104  
     public void setThresholds( int currentThreshold )
 105  
     {
 106  1
         this.currentThreshold = currentThreshold;
 107  
 
 108  1
         for (Object o : loggers.values()) {
 109  1
             Logger logger = (Logger) o;
 110  1
             logger.setThreshold(currentThreshold);
 111  1
         }
 112  1
     }
 113  
 
 114  
     /**
 115  
      * @return Returns the threshold.
 116  
      */
 117  
     public int getThreshold()
 118  
     {
 119  83
         return currentThreshold;
 120  
     }
 121  
 
 122  
     // new stuff
 123  
 
 124  
     public void setThreshold( String role, String roleHint, int threshold ) {
 125  
         ConsoleLogger logger;
 126  
         String name;
 127  
 
 128  0
         name = toMapKey( role, roleHint );
 129  0
         logger = (ConsoleLogger)loggers.get( name );
 130  
 
 131  0
         if(logger == null) {
 132  0
             debug( "Trying to set the threshold of a unknown logger '" + name + "'." );
 133  0
             return; // nothing to do
 134  
         }
 135  
 
 136  0
         logger.setThreshold( threshold );
 137  0
     }
 138  
 
 139  
     public int getThreshold( String role, String roleHint ) {
 140  
         ConsoleLogger logger;
 141  
         String name;
 142  
 
 143  0
         name = toMapKey( role, roleHint );
 144  0
         logger = (ConsoleLogger)loggers.get( name );
 145  
 
 146  0
         if(logger == null) {
 147  0
             debug( "Trying to get the threshold of a unknown logger '" + name + "'." );
 148  0
             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
 149  
         }
 150  
 
 151  0
         return logger.getThreshold();
 152  
     }
 153  
 
 154  
     public Logger createLogger(int threshold, String name)
 155  
     {
 156  83
         return new ConsoleLogger( threshold, name );
 157  
     }
 158  
 
 159  
     public Logger getLoggerForComponent( String role, String roleHint )
 160  
     {
 161  
         Logger logger;
 162  
         String name;
 163  
 
 164  93
         name = toMapKey( role, roleHint );
 165  93
         logger = (Logger)loggers.get( name );
 166  
 
 167  93
         if ( logger != null )
 168  10
             return logger;
 169  
 
 170  83
         debug( "Creating logger '" + name + "' " + this.hashCode() + "." );
 171  83
         logger = createLogger( getThreshold(), name );
 172  83
         loggers.put( name, logger );
 173  
 
 174  83
         return logger;
 175  
     }
 176  
 
 177  
     public void returnComponentLogger( String role, String roleHint )
 178  
     {
 179  
         Object obj;
 180  
         String name;
 181  
 
 182  7
         name = toMapKey( role, roleHint );
 183  7
         obj = loggers.remove( name );
 184  
 
 185  7
         if ( obj == null )
 186  
         {
 187  2
             debug( "There was no such logger '" + name + "' " + this.hashCode() + ".");
 188  
         }
 189  
         else
 190  
         {
 191  5
             debug( "Removed logger '" + name + "' " + this.hashCode() + ".");
 192  
         }
 193  7
     }
 194  
 
 195  
     public int getActiveLoggerCount()
 196  
     {
 197  7
         return loggers.size();
 198  
     }
 199  
 
 200  
     private int parseThreshold( String text )
 201  
     {
 202  69
         text = text.trim().toLowerCase( Locale.ENGLISH );
 203  
 
 204  69
         if ( text.equals( "debug" ) )
 205  
         {
 206  0
             return ConsoleLogger.LEVEL_DEBUG;
 207  
         }
 208  69
         else if ( text.equals( "info" ) )
 209  
         {
 210  25
             return ConsoleLogger.LEVEL_INFO;
 211  
         }
 212  44
         else if ( text.equals( "warn" ) )
 213  
         {
 214  0
             return ConsoleLogger.LEVEL_WARN;
 215  
         }
 216  44
         else if ( text.equals( "error" ) )
 217  
         {
 218  0
             return ConsoleLogger.LEVEL_ERROR;
 219  
         }
 220  44
         else if ( text.equals( "fatal" ) )
 221  
         {
 222  44
             return ConsoleLogger.LEVEL_FATAL;
 223  
         }
 224  
 
 225  0
         return -1;
 226  
     }
 227  
 
 228  
     private String decodeLogLevel( int logLevel )
 229  
     {
 230  0
         switch(logLevel) {
 231  0
         case ConsoleLogger.LEVEL_DEBUG: return "debug";
 232  0
         case ConsoleLogger.LEVEL_INFO: return "info";
 233  0
         case ConsoleLogger.LEVEL_WARN: return "warn";
 234  0
         case ConsoleLogger.LEVEL_ERROR: return "error";
 235  0
         case ConsoleLogger.LEVEL_FATAL: return "fatal";
 236  0
         case ConsoleLogger.LEVEL_DISABLED: return "disabled";
 237  0
         default: return "unknown";
 238  
         }
 239  
     }
 240  
 
 241  
     /**
 242  
      * Remove this method and all references when this code is verified.
 243  
      *
 244  
      * @param msg
 245  
      */
 246  
     private void debug( String msg )
 247  
     {
 248  
 //        if ( !bootTimeLogger )
 249  
 //            System.out.println( "[Console] " + msg );
 250  159
     }
 251  
 }