log4j2的松弛追加器

时间:2018-09-17 10:02:02

标签: java maven log4j2 slack

在Java 8项目中,我试图使用tobias的log4j2的Slack追加器。 tobias项目是: tobias wrapper for slack

我试图通过配置程序,log4j2.xml文件或程序化配置来配置附加程序,而这两种方式都无法使它正常工作。

我的log4j4.xml文件是:

<configuration monitorInterval="30">
<appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout
                pattern="%-5p - [%t]"/>
    </Console>

    <Slack name="Slack" channel="configurouter-alerts">
        <PatternLayout pattern="%-5p - [%t]"/>
    </Slack>

</appenders>

<loggers>
    <root level="info">
        <appender-ref ref="Slack" level="info"/>
        <appender-ref ref="Console"/>
    </root>
</loggers>
</configuration>

我的Maven依赖项是:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>slackappender</groupId>
<artifactId>slackappender</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>be.olsson</groupId>
        <artifactId>slack-appender</artifactId>
        <version>1.2.0</version>
    </dependency>

</dependencies>

</project>

我收到此错误日志:

2018-09-17 12:07:38,815 main ERROR Unable to invoke factory method in class be.olsson.slackappender.SlackAppender for element Slack: java.lang.IllegalArgumentException: Didn't find any working be.olsson.slackappender.client.Client implementation called null java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:136)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:958)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:898)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:890)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:513)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:237)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:249)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:122)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:43)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at LoggingUtils.lambda$startLogging$1(LoggingUtils.java:70)
    at LoggingUtils.startLogging(LoggingUtils.java:25)
    at LoggingUtils.startLogging(LoggingUtils.java:70)
    at Main.main(Main.java:9)
Caused by: java.lang.IllegalArgumentException: Didn't find any working be.olsson.slackappender.client.Client implementation called null
    at be.olsson.slackappender.SlackAppender.findClientImpl(SlackAppender.java:326)
    at be.olsson.slackappender.SlackAppender.createAppender(SlackAppender.java:310)
    ... 28 more

2018-09-17 12:07:38,822 main ERROR Null object returned for Slack in appenders.
2018-09-17 12:07:38,826 main ERROR Unable to locate appender "Slack" for logger config "root"
2018-09-17T12:07:38,938 [main] [INFO ] [flow=] [phase=] [uid=] [url=] [] Main - logging started
2018-09-17T12:07:38,940 [main] [INFO ] [flow=] [phase=] [uid=] [url=] [] Main - Info logger.
2018-09-17T12:07:38,940 [main] [ERROR] [flow=] [phase=] [uid=] [url=] [] Main - Warning logger.

Process finished with exit code 0

有人将这个tobias包装器与行家一起使用并且知道头可以提供帮助吗?

2 个答案:

答案 0 :(得分:1)

未使用此附加程序,但是正如我在it's github repobuild.gradle文件中所看到的那样,您需要手动将HTTP客户端实现添加到运行时类路径中。这些是作者在项目中使用的库:

dependencies {
    provided 'com.squareup.okhttp:okhttp:2.5.0'
    provided 'com.squareup.okhttp3:okhttp:3.5.0'
...

答案 1 :(得分:0)

谢谢,这解决了问题! 我在pom中添加了依赖项:

 <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.11.0</version>
        </dependency>