Loglog Log Maven Compatibility

I am writing a Maven plugin (Mojo) that imports the Jar library used by other projects. At least one of the classes in this library uses Apache Log4j for logging, but Log4j will not be configured correctly by the logger that Maven provides Mojo.

Is there any easy way to transition between them? Unfortunately, org.apache.log4j.Logger and org.apache.maven.logging.Log do not have a common superinterface or superclass, so I cannot just have a function like setLog() . Any suggestions are welcome; I'm currently planning to either just ignore it, or write a bridge class that can use either.

+4
source share
2 answers

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

+2
source

I had the same problem and decided to write a simple bridge through 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 LogCj BasicConfigurator API class 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 the Maven infrastructure already has this bridge, I didn’t try to look for something more “maven-like”, but this solution worked fine.

+10
source

All Articles