View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.log;
17  
18  import org.codehaus.plexus.util.cli.StreamConsumer;
19  import org.kuali.common.util.Assert;
20  import org.slf4j.Logger;
21  
22  public final class LoggingStreamConsumer implements StreamConsumer {
23  
24  	private final Logger logger;
25  	private final LoggerLevel level;
26  
27  	public LoggingStreamConsumer(Logger logger) {
28  		this(logger, LoggerLevel.INFO);
29  	}
30  
31  	public LoggingStreamConsumer(Logger logger, LoggerLevel level) {
32  		Assert.noNulls(logger, level);
33  		this.logger = logger;
34  		this.level = level;
35  	}
36  
37  	@Override
38  	public void consumeLine(String line) {
39  		switch (level) {
40  		case TRACE:
41  			logger.trace(line);
42  			return;
43  		case DEBUG:
44  			logger.debug(line);
45  			return;
46  		case INFO:
47  			logger.info(line);
48  			return;
49  		case WARN:
50  			logger.warn(line);
51  			return;
52  		case ERROR:
53  			logger.error(line);
54  			return;
55  		default:
56  			throw new IllegalStateException("Logger level " + level + " is unknown");
57  		}
58  	}
59  
60  	public Logger getLogger() {
61  		return logger;
62  	}
63  
64  	public LoggerLevel getLevel() {
65  		return level;
66  	}
67  
68  }