Skip to content

Commit 9c2ddb4

Browse files
committed
Zlg.x(String) method automatically terminates the chain
1 parent 917c16b commit 9c2ddb4

File tree

13 files changed

+108
-87
lines changed

13 files changed

+108
-87
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class SysOutLoggingSample {
5555
private static final Zlg zlg = Zlg.forClass(SysOutLoggingSample.class).get();
5656

5757
public static void open(String address, int port, double timeoutSeconds) {
58+
zlg.i("Hello world");
5859
zlg.i("Pi is %.2f", z -> z.arg(Math.PI));
5960
zlg.i("Connecting to %s:%d [timeout: %.1f sec]", z -> z.arg(address).arg(port).arg(timeoutSeconds));
6061

@@ -303,9 +304,9 @@ private static final Zlg zlg = Zlg.forClass(MethodHandles.lookup().lookupClass()
303304

304305
private static final boolean TRACE_ENABLED = false;
305306

306-
public static void withStaticConstant(String address, int port, double timeoutSeconds) {
307+
public static void withStaticConstant(String address, int port, double timeout) {
307308
if (TRACE_ENABLED) {
308-
zlg.t("Connecting to %s:%d [timeout: %.1f sec]", z -> z.arg(address).arg(port).arg(timeoutSeconds));
309+
zlg.t("Connecting to %s:%d [timeout: %.1f sec]", z -> z.arg(address).arg(port).arg(timeout));
309310
}
310311
}
311312
```
@@ -315,12 +316,12 @@ public static void withStaticConstant(String address, int port, double timeoutSe
315316
```java
316317
private static final Zlg zlg = Zlg.forClass(MethodHandles.lookup().lookupClass()).get();
317318

318-
public static void withAssert(String address, int port, double timeoutSeconds) {
319-
assert zlg.t("Connecting to %s:%d [timeout: %.1f sec]").arg(address).arg(port).arg(timeoutSeconds).log();
319+
public static void withAssert(String address, int port, double timeout) {
320+
assert zlg.level(LogLevel.TRACE).format("Connecting to %s:%d [timeout: %.1f sec]").arg(address).arg(port).arg(timeout).log();
320321
}
321322
```
322323

323-
**Note:** Rather than chaining arguments within a lambda, the assertion example uses a continuous chaining style, culminating with a call to `log()`, which returns a constant `true`. If assertions are enabled with the `-ea` JVM argument, the log instruction will be evaluated and will never fail the assertion. Otherwise, the entire fluent chain will be dropped by DCE.
324+
**Note:** Rather than chaining arguments within a lambda, the assertion example uses a slightly longer, continuous chaining style, culminating with a call to `log()`, which returns a constant `true`. If assertions are enabled with the `-ea` JVM argument, the log instruction will be evaluated and will never fail the assertion. Otherwise, the entire fluent chain will be dropped by DCE.
324325

325326
The choice of using option one or two depends on whether you are targeting zero overhead for both production and testing scenarios or only for production. In case of the latter, the `-ea` flag naturally solves the problem, without forcing you to change your class before building. In either case, you will sacrifice code coverage, as both techniques introduce a parasitic branching instruction behind the scenes; only one path is traversed during the test.
326327

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: "maven-publish"
55
apply plugin: "com.jfrog.bintray"
66

77
group = "com.obsidiandynamics.zerolog"
8-
version = "0.7.0-SNAPSHOT"
8+
version = "0.7.0"
99

1010
def envUser = "BINTRAY_USER"
1111
def envKey = "BINTRAY_KEY"

slf4j17/src/test/java/com/obsidiandynamics/zerolog/sample/Slf4jLoggingSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class Slf4jLoggingSample {
99
private static final Zlg zlg = Zlg.forClass(MethodHandles.lookup().lookupClass()).get();
1010

1111
public static void main(String[] args) {
12-
zlg.i("Starting with %d args: %s").arg(args.length).arg(Arrays.asList(args)).done();
13-
zlg.w("An error occurred at %s").arg(new Date()).threw(new RuntimeException()).tag("I/O").done();
12+
zlg.i("Starting with %d args: %s", z -> z.arg(args.length).arg(Arrays.asList(args)));
13+
zlg.w("An error occurred at %s", z -> z.arg(new Date()).threw(new RuntimeException()).tag("I/O"));
1414
}
1515
}

src/main/java/com/obsidiandynamics/zerolog/Zlg.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,49 @@ default boolean log() {
5959

6060
boolean isEnabled(int level);
6161

62-
default LogChain e(String format) { return level(LogLevel.ERROR).format(format); }
62+
default void e(String format) {
63+
level(LogLevel.ERROR).format(format).done();
64+
}
6365

6466
default void e(String format, Consumer<LogChain> logChainConsumer) {
6567
level(LogLevel.ERROR).format(format).with(logChainConsumer);
6668
}
6769

68-
default LogChain w(String format) { return level(LogLevel.WARN).format(format); }
70+
default void w(String format) {
71+
level(LogLevel.WARN).format(format).done();
72+
}
6973

7074
default void w(String format, Consumer<LogChain> logChainConsumer) {
7175
level(LogLevel.WARN).format(format).with(logChainConsumer);
7276
}
7377

74-
default LogChain i(String format) { return level(LogLevel.INFO).format(format); }
78+
default void i(String format) {
79+
level(LogLevel.INFO).format(format).done();
80+
}
7581

7682
default void i(String format, Consumer<LogChain> logChainConsumer) {
7783
level(LogLevel.INFO).format(format).with(logChainConsumer);
7884
}
7985

80-
default LogChain c(String format) { return level(LogLevel.CONF).format(format); }
86+
default void c(String format) {
87+
level(LogLevel.CONF).format(format).done();
88+
}
8189

8290
default void c(String format, Consumer<LogChain> logChainConsumer) {
8391
level(LogLevel.CONF).format(format).with(logChainConsumer);
8492
}
8593

86-
default LogChain d(String format) { return level(LogLevel.DEBUG).format(format); }
94+
default void d(String format) {
95+
level(LogLevel.DEBUG).format(format).done();
96+
}
8797

8898
default void d(String format, Consumer<LogChain> logChainConsumer) {
8999
level(LogLevel.DEBUG).format(format).with(logChainConsumer);
90100
}
91101

92-
default LogChain t(String format) { return level(LogLevel.TRACE).format(format); }
102+
default void t(String format) {
103+
level(LogLevel.TRACE).format(format).done();
104+
}
93105

94106
default void t(String format, Consumer<LogChain> logChainConsumer) {
95107
level(LogLevel.TRACE).format(format).with(logChainConsumer);

src/test/java/com/obsidiandynamics/zerolog/MockLogTargetTest.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void testEntryToString() {
1616
final MockLogTarget target = new MockLogTarget(LogLevel.TRACE);
1717
final Zlg zlg = target.logger();
1818

19-
zlg.t("message").done();
19+
zlg.t("message");
2020
final List<Entry> entries = target.entries().list();
2121
assertNotNull(entries);
2222
assertEquals(1, entries.size());
@@ -67,7 +67,7 @@ public void testEntryRetention() {
6767
public void testEntriesIterator() {
6868
final MockLogTarget target = new MockLogTarget();
6969
final Zlg zlg = target.logger();
70-
zlg.t("message").done();
70+
zlg.t("message");
7171

7272
final Iterator<Entry> it = target.entries().iterator();
7373
assertTrue(it.hasNext());
@@ -79,9 +79,9 @@ public void testEntriesIterator() {
7979
public void testForLevel() {
8080
final MockLogTarget target = new MockLogTarget();
8181
final Zlg zlg = target.logger();
82-
zlg.t("trace").done();
83-
zlg.d("debug").done();
84-
zlg.c("conf").done();
82+
zlg.t("trace");
83+
zlg.d("debug");
84+
zlg.c("conf");
8585

8686
final List<Entry> entries = target.entries().forLevel(LogLevel.DEBUG).list();
8787
assertEquals(1, entries.size());
@@ -92,9 +92,9 @@ public void testForLevel() {
9292
public void testForLevelAndAbove() {
9393
final MockLogTarget target = new MockLogTarget();
9494
final Zlg zlg = target.logger();
95-
zlg.t("trace").done();
96-
zlg.d("debug").done();
97-
zlg.c("conf").done();
95+
zlg.t("trace");
96+
zlg.d("debug");
97+
zlg.c("conf");
9898

9999
final List<Entry> entries = target.entries().forLevelAndAbove(LogLevel.DEBUG).list();
100100
assertEquals(2, entries.size());
@@ -107,9 +107,9 @@ public void testBeforeAfter() {
107107
final long startTime = System.currentTimeMillis();
108108
final MockLogTarget target = new MockLogTarget();
109109
final Zlg zlg = target.logger();
110-
zlg.t("trace").done();
111-
zlg.d("debug").done();
112-
zlg.c("conf").done();
110+
zlg.t("trace");
111+
zlg.d("debug");
112+
zlg.c("conf");
113113
final long endTime = System.currentTimeMillis();
114114

115115
assertEquals(3, target.entries().after(startTime - 1).list().size());
@@ -123,9 +123,9 @@ public void testBeforeAfter() {
123123
public void testTag() {
124124
final MockLogTarget target = new MockLogTarget();
125125
final Zlg zlg = target.logger();
126-
zlg.t("trace").done();
127-
zlg.d("debug").tag("tag").done();
128-
zlg.c("conf").done();
126+
zlg.t("trace");
127+
zlg.d("debug", z -> z.tag("tag"));
128+
zlg.c("conf");
129129

130130
assertEquals(1, target.entries().tagged("tag").list().size());
131131
}
@@ -134,9 +134,9 @@ public void testTag() {
134134
public void testWithException() {
135135
final MockLogTarget target = new MockLogTarget();
136136
final Zlg zlg = target.logger();
137-
zlg.t("trace").done();
138-
zlg.d("debug").threw(new IOException("simulated")).done();
139-
zlg.c("conf").done();
137+
zlg.t("trace");
138+
zlg.d("debug", z -> z.threw(new IOException("simulated")));
139+
zlg.c("conf");
140140

141141
assertEquals(0, target.entries().withException(RuntimeException.class).list().size());
142142
assertEquals(1, target.entries().withException(IOException.class).list().size());
@@ -146,9 +146,9 @@ public void testWithException() {
146146
public void testContaining() {
147147
final MockLogTarget target = new MockLogTarget();
148148
final Zlg zlg = target.logger();
149-
zlg.t("trace").done();
150-
zlg.d("debug").done();
151-
zlg.c("conf").done();
149+
zlg.t("trace");
150+
zlg.d("debug");
151+
zlg.c("conf");
152152

153153
assertEquals(1, target.entries().containing("bug").list().size());
154154
}
@@ -157,9 +157,9 @@ public void testContaining() {
157157
public void testReset() {
158158
final MockLogTarget target = new MockLogTarget();
159159
final Zlg zlg = target.logger();
160-
zlg.t("trace").done();
161-
zlg.d("debug").done();
162-
zlg.c("conf").done();
160+
zlg.t("trace");
161+
zlg.d("debug");
162+
zlg.c("conf");
163163

164164
assertEquals(3, target.entries().list().size());
165165

0 commit comments

Comments
 (0)