Maven 插件记录器兼容性

发布于 2024-09-28 04:24:05 字数 295 浏览 1 评论 0原文

我正在编写一个 Maven 插件 (Mojo),它导入其他项目使用的 Jar 库。该库中至少有一个类使用 Apache Log4j 进行日志记录,但 Maven 向 Mojo 提供的记录器不会正确配置 Log4j。

有什么简单的方法可以在这些之间建立桥梁吗?不幸的是, org.apache.log4j.Logger 和 org.apache.maven.logging.Log 不共享通用的超接口或超类,所以我不能简单地拥有一个setLog() 类型函数。欢迎任何建议;目前我计划要么忽略它,要么编写一个可以使用其中任何一个的桥类。

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

Is there any simple way to bridge between these? Unfortunately, org.apache.log4j.Logger and org.apache.maven.logging.Log do not share a common superinterface or superclass, so I can't simply have a setLog() type function. Any suggestions would be welcome; currently I am planning to either just ignore it, or write a bridge class that can use either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-10-05 04:24:05

我遇到了同样的问题,并解决了在 Maven Logger 和 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;
    }
}

在我的 Mojo 中,我使用了 Log4j API 的 BasicConfigurator 类,以及该桥的一个实例:

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

我不知道如果 Maven 基础设施已经有这个桥,我还没有尝试搜索更“类似 Maven”的东西,但这个解决方案工作得很好。

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.

五里雾 2024-10-05 04:24:05

如果您想编写桥接类,请查看 SLF4J 源代码: http://www .slf4j.org/legacy.html#log4j-over-slf4j
他们在 log4j 桥中做了一些非常相似的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文