Java tutorial
/* * Copyright 2013 Andrew Everitt, Andrew Heckford, Daniele Masato * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package uk.org.raje.maven.plugin.msbuild; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import org.apache.maven.plugin.logging.Log; /** * This class catches log messages generated by the standard Java logging framework and relays them onto a Maven logger. */ final class LoggingHandler extends Handler { /** * Create a new log handler that catches messages generated by a standard Java logger. * @param name the name of the standard Java logger from which to catch messages */ public LoggingHandler(String name) { javaLogger = Logger.getLogger(name); javaLogger.addHandler(this); //Stop the propagation of log messages to the logger's parent (which could be the console). In this way we // ensure that only Maven prints out log messages and prevent duplication by the Java logging framework. javaLogger.setUseParentHandlers(false); } /** * Set the Maven logger that will output the messages generated by the Java logger specified in {@link * LoggingHandler#LoggingHandler(name)}. * @param log the Maven logger that will output the messages generated by the Java logger */ public void setLog(Log log) { this.mavenLogger = log; //Match the log level of the Java logger with that set in the Maven logger if (log.isDebugEnabled()) { javaLogger.setLevel(Level.FINE); } else if (log.isInfoEnabled()) { javaLogger.setLevel(Level.INFO); } else if (log.isWarnEnabled()) { javaLogger.setLevel(Level.WARNING); } else { javaLogger.setLevel(Level.SEVERE); } } @Override public void publish(LogRecord record) { int level = record.getLevel().intValue(); //Cast a log message from the Java logger into the corresponding message for the Maven logger if (level <= Level.FINE.intValue()) { mavenLogger.debug(record.getMessage()); } else if (level <= Level.INFO.intValue()) { mavenLogger.info(record.getMessage()); } else if (level <= Level.WARNING.intValue()) { mavenLogger.warn(record.getMessage()); } else { mavenLogger.error(record.getMessage()); } } @Override public void flush() { } @Override public void close() { } private Log mavenLogger; private Logger javaLogger; }