问题
Is there a way to retrieve the contents of the logger when its been passed by reference from a called Class. I have a simple example program set-up as follows,
Class
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;
}
}
Integration-Test (configured in Maven to use the failsafe plugin) I know this isn't really an integration test but the problem that i am trying to solve is caused by a conflict with re-directing System.out and the fail-safe plugin so I am contriving an example to use it and using a Logger instead of 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);
}
}
When I run the test via
mvn verify
it passes but I want to see the output from the logger - see the circle log output in the screenshot below,
Is there a way that I can retrieve what is inside, Logger.SimpleLogger@2f7c7260 I am assuming it will be the contents from Doubler i.e.
input is: 2 result is: 4 summary==>i: 2 result: 4
I cant find a way from reading the api, SimpleLogger Api doc
I could, reluctantly, consider using a different implementation of the SL4FJ interface, I say reluctantly because our system has already built SimpleLogger into the common library...
the POM.xml is,
<?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>
来源:https://stackoverflow.com/questions/57099974/how-to-retrieve-the-log-content-from-simplelogger-sl4fj