001    package org.apache.maven.cli;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Locale;
023    
024    import org.codehaus.plexus.logging.AbstractLoggerManager;
025    import org.codehaus.plexus.logging.Logger;
026    import org.codehaus.plexus.logging.LoggerManager;
027    import org.codehaus.plexus.logging.console.ConsoleLogger;
028    import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
029    
030    /**
031     * This is a simple logger manager that will only write the logging statements to the console.
032     * <p/>
033     * Sample configuration:
034     * <pre>
035     * <logging>
036     *   <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
037     *   <logger>
038     *     <threshold>DEBUG</threshold>
039     *   </logger>
040     * </logging>
041     * </pre>
042     *
043     * @author Jason van Zyl
044     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
045     */
046    public class MavenLoggerManager
047        extends AbstractLoggerManager
048        implements LoggerManager, Initializable
049    {
050        /**
051         * Message of this level or higher will be logged.
052         * <p/>
053         * This field is set by the plexus container thus the name is 'threshold'. The field
054         * currentThreshold contains the current setting of the threshold.
055         */
056        private String threshold = "info";
057    
058        private int currentThreshold;
059    
060        private Logger logger;
061    
062        public MavenLoggerManager( Logger logger )
063        {
064            this.logger = logger;
065        }
066    
067        public void initialize()
068        {
069            debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." );
070    
071            currentThreshold = parseThreshold( threshold );
072    
073            if ( currentThreshold == -1 )
074            {
075                debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." );
076                currentThreshold = Logger.LEVEL_DEBUG;
077            }
078        }
079    
080        public void setThreshold( int currentThreshold )
081        {
082            this.currentThreshold = currentThreshold;
083        }
084    
085        public void setThresholds( int currentThreshold )
086        {
087            this.currentThreshold = currentThreshold;
088    
089            logger.setThreshold( currentThreshold );
090        }
091    
092        /** @return Returns the threshold. */
093        public int getThreshold()
094        {
095            return currentThreshold;
096        }
097    
098        public void setThreshold( String role,
099                                  String roleHint,
100                                  int threshold )
101        {
102        }
103    
104        public int getThreshold( String role,
105                                 String roleHint )
106        {
107            return currentThreshold;
108        }
109    
110        public Logger getLoggerForComponent( String role,
111                                             String roleHint )
112        {
113            return logger;
114        }
115    
116        public void returnComponentLogger( String role,
117                                           String roleHint )
118        {
119        }
120    
121        public int getActiveLoggerCount()
122        {
123            return 1;
124        }
125    
126        private int parseThreshold( String text )
127        {
128            text = text.trim().toLowerCase( Locale.ENGLISH );
129    
130            if ( text.equals( "debug" ) )
131            {
132                return ConsoleLogger.LEVEL_DEBUG;
133            }
134            else if ( text.equals( "info" ) )
135            {
136                return ConsoleLogger.LEVEL_INFO;
137            }
138            else if ( text.equals( "warn" ) )
139            {
140                return ConsoleLogger.LEVEL_WARN;
141            }
142            else if ( text.equals( "error" ) )
143            {
144                return ConsoleLogger.LEVEL_ERROR;
145            }
146            else if ( text.equals( "fatal" ) )
147            {
148                return ConsoleLogger.LEVEL_FATAL;
149            }
150    
151            return -1;
152        }
153    
154        /**
155         * Remove this method and all references when this code is verified.
156         *
157         * @param msg
158         */
159        private void debug( String msg )
160        {
161        }
162    }