001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util;
017    
018    import org.codehaus.plexus.util.cli.StreamConsumer;
019    import org.slf4j.Logger;
020    
021    public class LoggingStreamConsumer implements StreamConsumer {
022    
023            Logger logger;
024            LoggerLevel level;
025    
026            public LoggingStreamConsumer() {
027                    this(null);
028            }
029    
030            public LoggingStreamConsumer(Logger logger) {
031                    this(logger, LoggerLevel.INFO);
032            }
033    
034            public LoggingStreamConsumer(Logger logger, LoggerLevel level) {
035                    super();
036                    this.logger = logger;
037                    this.level = level;
038            }
039    
040            @Override
041            public void consumeLine(String line) {
042                    switch (level) {
043                    case TRACE:
044                            logger.trace(line);
045                            return;
046                    case DEBUG:
047                            logger.debug(line);
048                            return;
049                    case INFO:
050                            logger.info(line);
051                            return;
052                    case WARN:
053                            logger.warn(line);
054                            return;
055                    case ERROR:
056                            logger.error(line);
057                            return;
058                    default:
059                            throw new IllegalStateException("Logger level " + level + " is unknown");
060                    }
061            }
062    
063            public Logger getLogger() {
064                    return logger;
065            }
066    
067            public void setLogger(Logger logger) {
068                    this.logger = logger;
069            }
070    
071            public LoggerLevel getLevel() {
072                    return level;
073            }
074    
075            public void setLevel(LoggerLevel level) {
076                    this.level = level;
077            }
078    
079    }