Skip to content

Commit d419af4

Browse files
committed
Improve DefaultLogService to use stdout and stderr
When logging critical messages (WARN and ERROR levels), use stderr. When logging non-critical messages (e.g., INFO level), use stdout. This change will be very useful for differentiating between different log levels in a simpler way; in particular, ImageJ can send both stdout and stderr to a Console window, coloring stderr messages in red and setting the Console to visible when they occur, but not displaying the Console by default when only stdout messages occur (because many ImageJ plugins spam stdout with lots of debugging output).
1 parent 8877f4d commit d419af4

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package org.scijava.log;
3333

34+
import java.io.PrintStream;
3435
import java.util.HashMap;
3536
import java.util.Map;
3637
import java.util.Properties;
@@ -51,22 +52,6 @@ public abstract class AbstractLogService extends AbstractService implements
5152
private final Map<String, Integer> classAndPackageLevels =
5253
new HashMap<String, Integer>();
5354

54-
// -- abstract methods --
55-
56-
/**
57-
* Displays a message.
58-
*
59-
* @param msg the message to display.
60-
*/
61-
protected abstract void log(final String msg);
62-
63-
/**
64-
* Displays an exception.
65-
*
66-
* @param t the exception to display.
67-
*/
68-
protected abstract void log(final Throwable t);
69-
7055
// -- constructor --
7156

7257
public AbstractLogService() {
@@ -98,15 +83,30 @@ public AbstractLogService() {
9883

9984
// -- Internal methods --
10085

101-
protected void log(final int level, final Object msg, final Throwable t) {
102-
if (msg != null || t == null) {
103-
log(level, msg);
104-
}
105-
if (t != null) log(t);
106-
}
86+
/**
87+
* Displays a message and/or exception at the given logging level.
88+
*
89+
* @param level The logging level of the information.
90+
* @param msg The message to display.
91+
* @param t The exception to display.
92+
*/
93+
protected abstract void log(final int level, final Object msg,
94+
final Throwable t);
10795

108-
protected void log(final int level, final Object msg) {
109-
log(getPrefix(level) + msg);
96+
/**
97+
* Displays a message and/or exception at the given logging level, using the
98+
* specific output stream.
99+
*
100+
* @param stream The output stream to which the information should be sent.
101+
* @param level The logging level of the information.
102+
* @param msg The message to display.
103+
* @param t The exception to display.
104+
*/
105+
protected void log(final PrintStream stream, final int level,
106+
final Object msg, final Throwable t)
107+
{
108+
if (msg != null || t == null) stream.println(getPrefix(level) + msg);
109+
if (t != null) t.printStackTrace(stream);
110110
}
111111

112112
// -- LogService methods --

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,23 @@
3636
import org.scijava.service.Service;
3737

3838
/**
39-
* Implementation of {@link LogService} using the standard error stream.
39+
* Default implementation of {@link LogService}.
40+
* <p>
41+
* It uses the standard output and error streams, logging critical messages (
42+
* {@link LogService#WARN} and {@link LogService#ERROR} levels) to
43+
* {@code stderr}, and non-critical messages ({@link LogService#INFO},
44+
* {@link LogService#DEBUG} and {@link LogService#TRACE} levels) to
45+
* {@code stdout}.
46+
* </p>
4047
*
41-
* @author Johannes Schindelin
4248
* @author Curtis Rueden
4349
*/
4450
@Plugin(type = Service.class, priority = Priority.LOW_PRIORITY)
4551
public class DefaultLogService extends AbstractLogService {
4652

47-
/**
48-
* Prints a message to stderr.
49-
*
50-
* @param message the message
51-
*/
5253
@Override
53-
protected void log(final String message) {
54-
System.err.println(message);
55-
}
56-
57-
/**
58-
* Prints an exception to stderr.
59-
*
60-
* @param t the exception
61-
*/
62-
@Override
63-
protected void log(final Throwable t) {
64-
t.printStackTrace();
54+
protected void log(final int level, final Object msg, final Throwable t) {
55+
log(level > WARN ? System.out : System.err, level, msg, t);
6556
}
6657

6758
}

0 commit comments

Comments
 (0)