Skip to content

Commit 05c6957

Browse files
maarztctrueden
authored andcommitted
Format log messages using LogMessage.toString
And test that the method works as expected. Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
1 parent dd2dac1 commit 05c6957

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

src/main/java/org/scijava/log/LogMessage.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package org.scijava.log;
3333

34+
import java.io.PrintWriter;
35+
import java.io.StringWriter;
3436
import java.util.Collection;
3537
import java.util.Collections;
3638
import java.util.Date;
@@ -109,7 +111,20 @@ public void attach(Object value) {
109111

110112
@Override
111113
public String toString() {
112-
return "LogMessage {" + level + ", " + message + ", " + throwable + "}";
114+
return format(this);
113115
}
114116

117+
// -- Utility methods --
118+
119+
public static String format(final LogMessage message) {
120+
final StringWriter sw = new StringWriter();
121+
final PrintWriter printer = new PrintWriter(sw);
122+
printer.print("[" + message.time() + "] ");
123+
printer.print("[" + LogLevel.prefix(message.level()) + "] ");
124+
printer.println(message.text());
125+
if (message.throwable() != null) {
126+
message.throwable().printStackTrace(printer);
127+
}
128+
return sw.toString();
129+
}
115130
}

src/main/java/org/scijava/log/StderrLogService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
package org.scijava.log;
3434

35+
import java.io.PrintStream;
36+
3537
import org.scijava.Priority;
3638
import org.scijava.plugin.Plugin;
3739
import org.scijava.service.Service;
@@ -52,11 +54,7 @@ public class StderrLogService extends AbstractLogService {
5254

5355
@Override
5456
public void alwaysLog(final int level, final Object msg, final Throwable t) {
55-
final String prefix = LogLevel.prefix(level);
56-
final String message = (prefix == null ? "" : prefix + " ") + msg;
57-
// NB: Emit severe messages to stderr, and less severe ones to stdout.
58-
if (level <= LogLevel.WARN) System.err.println(message);
59-
else System.out.println(message);
60-
if (t != null) t.printStackTrace();
57+
final PrintStream out = (level <= LogLevel.WARN) ? System.err : System.out;
58+
out.print(new LogMessage(level, msg, t));
6159
}
6260
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.log;
33+
34+
import org.junit.Assert;
35+
import org.junit.Test;
36+
37+
/**
38+
* Tests {@link LogMessage}.
39+
*
40+
* @author Matthias Arzt
41+
*/
42+
public class LogMessageTest {
43+
44+
/** Tests {@link LogMessage#toString()}. */
45+
@Test
46+
public void testToString() {
47+
String nameOfThisMethod = "testToString";
48+
// setup
49+
LogMessage message = new LogMessage(LogLevel.DEBUG, 42, new NullPointerException());
50+
// process
51+
String s = message.toString();
52+
//test
53+
Assert.assertTrue("Log message contains level", s.contains(LogLevel.prefix(message.level())));
54+
Assert.assertTrue("Log message contains msg", s.contains(message.text()));
55+
Assert.assertTrue("Log message contains throwable", s.contains(message.throwable().toString()));
56+
Assert.assertTrue("Log message contains stack trace", s.contains(nameOfThisMethod));
57+
}
58+
59+
@Test
60+
public void testToStringOptionalParameters() {
61+
// setup
62+
LogMessage message = new LogMessage(LogLevel.WARN, null, null);
63+
64+
// process
65+
// Can it still format the message if optional parameters are null?
66+
String s = message.toString();
67+
68+
// test
69+
Assert.assertTrue("Log message contains level", s.contains(LogLevel.prefix(message.level())));
70+
}
71+
}

0 commit comments

Comments
 (0)