如何从Simplelogger SL4FJ检索日志内容

时间:2019-07-18 17:38:06

标签: java maven log4j log4j2 slf4j

是否有一种方法可以检索记录器的内容,该记录器是通过引用从调用的类传递的。我有一个简单的示例程序设置,如下所示,

班级

public class Doubler {

    static SimpleLogger logger;

    public Doubler(SimpleLogger logger) {
        this.logger = logger;
    }


    public int calc(int i) {
        int result = 2 * i;

        logger.info("\ninput is: " + i);
        logger.info("\nresult is: " + result);
        logger.info("summary==>" + "i: " + i + "   " + "result: " + result);

        return result;
    }
}

集成测试(在Maven中配置为使用故障安全插件) 我知道这不是真正的集成测试,但是我要解决的问题是由与重定向System.out和故障安全插件冲突引起的,所以我正在设计一个使用它并使用Logger的示例而不是System.out。

public class DoublerIT {

    static SimpleLogger logger = SimpleLogger.getLogger(DoublerIT.class);
    Doubler undertest = new Doubler(logger);

    @Test
    public void shouldReturnDoubleTheInput() {

        int result = undertest.calc(2);

        if (logger != null && !logger.toString().isEmpty())
            System.out.println("out log>>>" + logger.toString());

        assertEquals(4, result);
    }

}

通过

运行测试时
  

mvn验证

它通过了,但是我想查看记录器的输出-请参见下面的屏幕快照中的循环日志输出,

enter image description here

有没有一种方法可以检索里面的东西, Logger.SimpleLogger@2f7c7260我假设它将是Doubler的内容,即

    input is: 2
    result is: 4
    summary==>i: 2 result: 4

我无法通过阅读api SimpleLogger Api doc

找到方法

我很不情愿地考虑使用SL4FJ接口的其他实现,我很不情愿地说,因为我们的系统已经将SimpleLogger内置到公共库中了。

POM.xml是

<?xml version="1.0" encoding="UTF-8"?>

<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>com.stackoverflow</groupId>
    <artifactId>logging-poc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>logging-poc</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>xxx.xxx.xxx</groupId>
            <artifactId>simpleloggerlib</artifactId>
            <version>1.13</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M3</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

0 个答案:

没有答案