Merge branch 'master' into BAEL-16646-2

This commit is contained in:
Alessio Stalla
2019-10-24 13:20:08 +02:00
parent db85c8f275
commit c499158763
20506 changed files with 1643665 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
<?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>
<artifactId>flogger</artifactId>
<parent>
<artifactId>logging-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-system-backend</artifactId>
<version>0.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-slf4j-backend</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-log4j-backend</artifactId>
<version>0.4</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,17 @@
package com.baeldung.flogger;
import com.google.common.flogger.FluentLogger;
import com.google.common.flogger.LoggerConfig;
import java.util.logging.Level;
public class FloggerExamples {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static void main(String[] args) {
LoggerConfig.of(logger).setLevel(Level.FINE);
Exception exception = new Exception("This is a test exception.");
logger.atInfo().withCause(exception).log("Log message with: %s", "Alfred");
}
}
@@ -0,0 +1,96 @@
package com.baeldung.flogger;
import com.google.common.flogger.FluentLogger;
import com.google.common.flogger.LoggerConfig;
import com.google.common.flogger.StackSize;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.stream.IntStream;
import static com.google.common.flogger.LazyArgs.lazy;
public class FloggerIntegrationTest {
static {
// System.setProperty("flogger.backend_factory", "com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance");
System.setProperty("flogger.backend_factory", "com.google.common.flogger.backend.slf4j.Slf4jBackendFactory#getInstance");
}
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Test
public void givenAnInterval_shouldLogAfterEveryInterval() {
IntStream.range(0, 100).forEach(value -> {
logger.atInfo().every(40).log("This log shows [every 40 iterations] => %d", value);
});
}
@Test
public void givenATimeInterval_shouldLogAfterEveryTimeInterval() {
IntStream.range(0, 1_000_0000).forEach(value -> {
logger.atInfo().atMostEvery(10, TimeUnit.SECONDS).log("This log shows [every 10 seconds] => %d", value);
});
}
@Test
public void givenAnObject_shouldLogTheObject() {
User user = new User();
logger.atInfo().log("The user is: %s", user); //correct
//The following ways of logging are not recommended
logger.atInfo().log("The user is: %s", user.toString());
logger.atInfo().log("The user is: %s" + user);
}
@Test
public void givenASimpleOperation_shouldLogTheResult() {
int result = 45 / 3;
logger.atInfo().log("The result is %d", result);
}
@Test
public void givenCodeThatThrowsAndException_shouldLogTheException() {
try {
int result = 45 / 0;
} catch (RuntimeException re) {
logger.atInfo().withStackTrace(StackSize.FULL).withCause(re).log("Message");
}
}
@Test
public void givenALoggingConfiguration_shouldLogAtTheConfiguredLevel() {
LoggerConfig.of(logger).setLevel(Level.FINE);
logger.atInfo().log("Info Message");
logger.atWarning().log("Warning Message");
logger.atSevere().log("Severe Message");
logger.atFinest().log("Finest Message");
logger.atFine().log("Fine Message");
logger.atFiner().log("Finer Message");
logger.atConfig().log("Config Message");
}
@Test
public void givenALongRunningMethodForStats_shouldCallTheMethodLazily() {
//Wrong way of doing it
logger.atFine().log("stats=%s", collectSummaries());
// Almost no work done at the log site and structure is preserved.
logger.atFine().log("stats=%s", lazy(() -> collectSummaries()));
}
public static String collectSummaries() {
//compute summaries in a long-running process
int items = 110;
int s = 30;
return String.format("%d seconds elapsed so far. %d items pending processing", s, items);
}
private class User {
String name = "Test";
@Override
public String toString() {
return name;
}
}
}