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;
17  
18  import org.codehaus.plexus.util.cli.StreamConsumer;
19  import org.slf4j.Logger;
20  
21  public class LoggingStreamConsumer implements StreamConsumer {
22  
23  	Logger logger;
24  	LoggerLevel level;
25  
26  	public LoggingStreamConsumer() {
27  		this(null);
28  	}
29  
30  	public LoggingStreamConsumer(Logger logger) {
31  		this(logger, LoggerLevel.INFO);
32  	}
33  
34  	public LoggingStreamConsumer(Logger logger, LoggerLevel level) {
35  		super();
36  		this.logger = logger;
37  		this.level = level;
38  	}
39  
40  	@Override
41  	public void consumeLine(String line) {
42  		switch (level) {
43  		case TRACE:
44  			logger.trace(line);
45  			return;
46  		case DEBUG:
47  			logger.debug(line);
48  			return;
49  		case INFO:
50  			logger.info(line);
51  			return;
52  		case WARN:
53  			logger.warn(line);
54  			return;
55  		case ERROR:
56  			logger.error(line);
57  			return;
58  		default:
59  			throw new IllegalStateException("Logger level " + level + " is unknown");
60  		}
61  	}
62  
63  	public Logger getLogger() {
64  		return logger;
65  	}
66  
67  	public void setLogger(Logger logger) {
68  		this.logger = logger;
69  	}
70  
71  	public LoggerLevel getLevel() {
72  		return level;
73  	}
74  
75  	public void setLevel(LoggerLevel level) {
76  		this.level = level;
77  	}
78  
79  }