Maven Plugin Logger Compatibility

假装没事ソ 提交于 2019-12-03 08:18:56

If you want to write a bridge class, look at SLF4J sources: http://www.slf4j.org/legacy.html#log4j-over-slf4j They are doing something quite similar in their log4j bridge.

Rafael Trestini

I had the same problem and solved writing a simple bridge across Maven Logger and Log4j:

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.maven.plugin.logging.Log;

public class MavenLoggerLog4jBridge extends AppenderSkeleton {
    private Log logger;

    public MavenLoggerLog4jBridge(Log logger) {
        this.logger = logger;
    }

    protected void append(LoggingEvent event) {
        int level = event.getLevel().toInt();
        String msg = event.getMessage().toString();
        if (level == Level.DEBUG_INT || level == Level.TRACE_INT) {
            this.logger.debug(msg);
        } else if (level == Level.INFO_INT) {
            this.logger.info(msg);
        } else if (level == Level.WARN_INT) {
            this.logger.warn(msg);
        } else if (level == Level.ERROR_INT || level == Level.FATAL_INT) {
            this.logger.error(msg);
        }
    }

    public void close() {
    }

    public boolean requiresLayout() {
        return false;
    }
}

And in my Mojo, I used the BasicConfigurator class of Log4j API, with an instance of this bridge:

public void execute() throws MojoExecutionException {
    org.apache.maven.plugin.logging.Log mavenLogger = getLog();
    BasicConfigurator.configure(new MavenLoggerLog4jBridge(mavenLogger));
}

I don't know if Maven infrastructure already have this bridge, I haven't tried to search something more "maven-like", but this solution worked fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!