Skip to content

Commit ac8c5ef

Browse files
Alfred-Samangamaibin
authored andcommitted
BAEL-3321 Flogger fluent logging (eugenp#7824)
* Added Flogger maven dependency * Implemented Flogger logging examples * Finished implementing Flogger logging examples * Changed Flogger Test to Integration tests
1 parent c1caed8 commit ac8c5ef

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

logging-modules/flogger/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>logging-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>flogger</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.google.flogger</groupId>
17+
<artifactId>flogger</artifactId>
18+
<version>0.4</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.google.flogger</groupId>
22+
<artifactId>flogger-system-backend</artifactId>
23+
<version>0.4</version>
24+
<scope>runtime</scope>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.flogger;
2+
3+
import com.google.common.flogger.FluentLogger;
4+
import com.google.common.flogger.LoggerConfig;
5+
6+
import java.util.logging.Level;
7+
8+
public class FloggerExamples {
9+
10+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
11+
12+
public static void main(String[] args) {
13+
LoggerConfig.of(logger).setLevel(Level.FINE);
14+
Exception exception = new Exception("This is a test exception.");
15+
logger.atInfo().withCause(exception).log("Log message with: %s", "Alfred");
16+
}
17+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.flogger;
2+
3+
import com.google.common.flogger.FluentLogger;
4+
import com.google.common.flogger.LoggerConfig;
5+
import com.google.common.flogger.StackSize;
6+
import org.junit.Test;
7+
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.logging.Level;
10+
import java.util.stream.IntStream;
11+
12+
import static com.google.common.flogger.LazyArgs.lazy;
13+
14+
public class FloggerIntegrationTest {
15+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
16+
17+
@Test
18+
public void givenAnInterval_shouldLogAfterEveryInterval() {
19+
IntStream.range(0, 100).forEach(value -> {
20+
logger.atInfo().every(40).log("This log shows [every 40 iterations] => %d", value);
21+
});
22+
}
23+
24+
@Test
25+
public void givenATimeInterval_shouldLogAfterEveryTimeInterval() {
26+
IntStream.range(0, 1_000_0000).forEach(value -> {
27+
logger.atInfo().atMostEvery(10, TimeUnit.SECONDS).log("This log shows [every 10 seconds] => %d", value);
28+
});
29+
}
30+
31+
@Test
32+
public void givenASimpleOperation_shouldLogTheResult() {
33+
int result = 45 / 3;
34+
logger.atInfo().log("The result is %d", result);
35+
}
36+
37+
@Test
38+
public void givenCodeThatThrowsAndException_shouldLogTheException() {
39+
try {
40+
int result = 45 / 0;
41+
} catch (RuntimeException re) {
42+
logger.atInfo().withStackTrace(StackSize.FULL).withCause(re).log("Message");
43+
}
44+
}
45+
46+
@Test
47+
public void givenALoggingConfiguration_shouldLogAtTheConfiguredLevel() {
48+
LoggerConfig.of(logger).setLevel(Level.FINE);
49+
logger.atInfo().log("Info Message");
50+
logger.atWarning().log("Warning Message");
51+
logger.atSevere().log("Severe Message");
52+
logger.atFinest().log("Finest Message");
53+
logger.atFine().log("Fine Message");
54+
logger.atFiner().log("Finer Message");
55+
logger.atConfig().log("Config Message");
56+
}
57+
58+
@Test
59+
public void givenALongRunningMethodForStats_shouldCallTheMethodLazily() {
60+
//Wrong way of doing it
61+
logger.atFine().log("stats=%s", collectSummaries());
62+
63+
// Almost no work done at the log site and structure is preserved.
64+
logger.atFine().log("stats=%s", lazy(() -> collectSummaries()));
65+
}
66+
67+
public static String collectSummaries() {
68+
//compute summaries in a long-running process
69+
int items = 110;
70+
int s = 30;
71+
return String.format("%d seconds elapsed so far. %d items pending processing", s, items);
72+
}
73+
}

logging-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>log4j2</module>
1919
<module>logback</module>
2020
<module>log-mdc</module>
21+
<module>flogger</module>
2122
</modules>
2223

2324
</project>

0 commit comments

Comments
 (0)