Skip to content

Commit 1be2e43

Browse files
committed
Merge branch 'logging' into sjc3
This improves the LogService implementation.
2 parents 1399b9a + 2145a5e commit 1be2e43

File tree

7 files changed

+76
-88
lines changed

7 files changed

+76
-88
lines changed

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

Lines changed: 57 additions & 60 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<>();
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() {
@@ -96,114 +81,109 @@ public AbstractLogService() {
9681

9782
}
9883

99-
// -- helper methods --
100-
101-
protected void log(final int level, final Object msg, final Throwable t) {
102-
if (level > getLevel()) return;
84+
// -- Internal methods --
10385

104-
if (msg != null || t == null) {
105-
log(level, msg);
106-
}
107-
if (t != null) log(t);
108-
}
109-
110-
protected void log(final int level, final Object msg) {
111-
final String prefix = getPrefix(level);
112-
log((prefix == null ? "" : prefix + " ") + msg);
113-
}
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);
11495

115-
protected String getPrefix(final int level) {
116-
switch (level) {
117-
case ERROR:
118-
return "[ERROR]";
119-
case WARN:
120-
return "[WARNING]";
121-
case INFO:
122-
return "[INFO]";
123-
case DEBUG:
124-
return "[DEBUG]";
125-
case TRACE:
126-
return "[TRACE]";
127-
default:
128-
return null;
129-
}
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);
130110
}
131111

132112
// -- LogService methods --
133113

134114
@Override
135115
public void debug(final Object msg) {
136-
log(DEBUG, msg, null);
116+
if (isDebug()) log(DEBUG, msg, null);
137117
}
138118

139119
@Override
140120
public void debug(final Throwable t) {
141-
log(DEBUG, null, t);
121+
if (isDebug()) log(DEBUG, null, t);
142122
}
143123

144124
@Override
145125
public void debug(final Object msg, final Throwable t) {
146-
log(DEBUG, msg, t);
126+
if (isDebug()) log(DEBUG, msg, t);
147127
}
148128

149129
@Override
150130
public void error(final Object msg) {
151-
log(ERROR, msg, null);
131+
if (isError()) log(ERROR, msg, null);
152132
}
153133

154134
@Override
155135
public void error(final Throwable t) {
156-
log(ERROR, null, t);
136+
if (isError()) log(ERROR, null, t);
157137
}
158138

159139
@Override
160140
public void error(final Object msg, final Throwable t) {
161-
log(ERROR, msg, t);
141+
if (isError()) log(ERROR, msg, t);
162142
}
163143

164144
@Override
165145
public void info(final Object msg) {
166-
log(INFO, msg, null);
146+
if (isInfo()) log(INFO, msg, null);
167147
}
168148

169149
@Override
170150
public void info(final Throwable t) {
171-
log(INFO, null, t);
151+
if (isInfo()) log(INFO, null, t);
172152
}
173153

174154
@Override
175155
public void info(final Object msg, final Throwable t) {
176-
log(INFO, msg, t);
156+
if (isInfo()) log(INFO, msg, t);
177157
}
178158

179159
@Override
180160
public void trace(final Object msg) {
181-
log(TRACE, msg, null);
161+
if (isTrace()) log(TRACE, msg, null);
182162
}
183163

184164
@Override
185165
public void trace(final Throwable t) {
186-
log(TRACE, null, t);
166+
if (isTrace()) log(TRACE, null, t);
187167
}
188168

189169
@Override
190170
public void trace(final Object msg, final Throwable t) {
191-
log(TRACE, msg, t);
171+
if (isTrace()) log(TRACE, msg, t);
192172
}
193173

194174
@Override
195175
public void warn(final Object msg) {
196-
log(WARN, msg, null);
176+
if (isWarn()) log(WARN, msg, null);
197177
}
198178

199179
@Override
200180
public void warn(final Throwable t) {
201-
log(WARN, null, t);
181+
if (isWarn()) log(WARN, null, t);
202182
}
203183

204184
@Override
205185
public void warn(final Object msg, final Throwable t) {
206-
log(WARN, msg, t);
186+
if (isWarn()) log(WARN, msg, t);
207187
}
208188

209189
@Override
@@ -258,6 +238,23 @@ public void setLevel(final String classOrPackageName, final int level) {
258238

259239
// -- Helper methods --
260240

241+
private String getPrefix(final int level) {
242+
switch (level) {
243+
case ERROR:
244+
return "[ERROR] ";
245+
case WARN:
246+
return "[WARNING] ";
247+
case INFO:
248+
return "[INFO] ";
249+
case DEBUG:
250+
return "[DEBUG] ";
251+
case TRACE:
252+
return "[TRACE] ";
253+
default:
254+
return "";
255+
}
256+
}
257+
261258
/** Extracts the log level value from a string. */
262259
private int level(final String logProp) {
263260
if (logProp == null) return -1;

src/main/java/org/scijava/log/StderrLogService.java renamed to src/main/java/org/scijava/log/DefaultLogService.java

Lines changed: 11 additions & 20 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)
45-
public class StderrLogService extends AbstractLogService {
51+
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
}

src/main/java/org/scijava/script/AbstractScriptEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import javax.script.ScriptEngineFactory;
4040
import javax.script.ScriptException;
4141

42+
import org.scijava.log.DefaultLogService;
4243
import org.scijava.log.LogService;
43-
import org.scijava.log.StderrLogService;
4444

4545
/**
4646
* This class implements dummy versions for ScriptEngine's methods that are not
@@ -70,7 +70,7 @@ public abstract class AbstractScriptEngine implements ScriptEngine {
7070

7171
public synchronized LogService log() {
7272
if (log == null) {
73-
log = new StderrLogService();
73+
log = new DefaultLogService();
7474
}
7575
return log;
7676
}

src/main/java/org/scijava/service/ServiceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
import org.scijava.Optional;
4444
import org.scijava.event.EventHandler;
4545
import org.scijava.event.EventService;
46+
import org.scijava.log.DefaultLogService;
4647
import org.scijava.log.LogService;
47-
import org.scijava.log.StderrLogService;
4848
import org.scijava.plugin.Parameter;
4949
import org.scijava.plugin.PluginInfo;
5050
import org.scijava.service.event.ServicesLoadedEvent;
@@ -114,7 +114,7 @@ public ServiceHelper(final Context context,
114114
{
115115
setContext(context);
116116
log = context.getService(LogService.class);
117-
if (log == null) log = new StderrLogService();
117+
if (log == null) log = new DefaultLogService();
118118
classPoolMap = new HashMap<>();
119119
classPoolList = new ArrayList<>();
120120
findServiceClasses(classPoolMap, classPoolList);

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void testFull() {
115115
org.scijava.ui.dnd.DefaultDragAndDropService.class,
116116
org.scijava.welcome.DefaultWelcomeService.class,
117117
org.scijava.widget.DefaultWidgetService.class,
118-
org.scijava.log.StderrLogService.class,
118+
org.scijava.log.DefaultLogService.class,
119119
org.scijava.platform.DefaultAppEventService.class,
120120
org.scijava.cache.DefaultCacheService.class};
121121

src/test/java/org/scijava/log/LogServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
public class LogServiceTest {
4545
@Test
4646
public void testDefaultLevel() {
47-
final LogService log = new StderrLogService();
47+
final LogService log = new DefaultLogService();
4848
int level = log.getLevel();
4949
assertTrue("default level (" + level + ") is at least INFO(" + WARN + ")", level >= WARN);
5050
}

src/test/java/org/scijava/service/ServiceIndexTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.junit.Test;
4242
import org.scijava.Context;
4343
import org.scijava.event.DefaultEventService;
44-
import org.scijava.log.StderrLogService;
44+
import org.scijava.log.DefaultLogService;
4545
import org.scijava.options.DefaultOptionsService;
4646
import org.scijava.options.OptionsService;
4747
import org.scijava.plugin.DefaultPluginService;
@@ -64,7 +64,7 @@ public void testGetAll() {
6464
assertSame(DefaultEventService.class, all.get(0).getClass());
6565
assertSame(DefaultPluginService.class, all.get(1).getClass());
6666
assertSame(DefaultThreadService.class, all.get(2).getClass());
67-
assertSame(StderrLogService.class, all.get(3).getClass());
67+
assertSame(DefaultLogService.class, all.get(3).getClass());
6868
context.dispose();
6969
}
7070

0 commit comments

Comments
 (0)