diff --git a/logging-modules/flogger/pom.xml b/logging-modules/flogger/pom.xml new file mode 100644 index 0000000000..2b96332bf6 --- /dev/null +++ b/logging-modules/flogger/pom.xml @@ -0,0 +1,27 @@ + + + + logging-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + flogger + + + + com.google.flogger + flogger + 0.4 + + + com.google.flogger + flogger-system-backend + 0.4 + runtime + + + \ No newline at end of file diff --git a/logging-modules/flogger/src/main/java/com/baeldung/flogger/FloggerExamples.java b/logging-modules/flogger/src/main/java/com/baeldung/flogger/FloggerExamples.java new file mode 100644 index 0000000000..252d2d38e4 --- /dev/null +++ b/logging-modules/flogger/src/main/java/com/baeldung/flogger/FloggerExamples.java @@ -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"); + } +} diff --git a/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java new file mode 100644 index 0000000000..69460dc045 --- /dev/null +++ b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java @@ -0,0 +1,73 @@ +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 { + 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 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); + } +} diff --git a/logging-modules/pom.xml b/logging-modules/pom.xml index a303e50ff1..927afb6ca9 100644 --- a/logging-modules/pom.xml +++ b/logging-modules/pom.xml @@ -18,6 +18,7 @@ log4j2 logback log-mdc + flogger