Skip to content

Commit 3a530d0

Browse files
committed
Logger: funnel all logging through a single method
This change eliminates the various protected log methods of AbstractLogService, in favor of a single method: alwaysLog(int level, Object message, Throwable t) All other logging methods in the Logger interface now have default implementations which funnel to this method, which should make it simpler to implement this interface. This change also introduces new methods for logging information at any arbitrary log level: log(int level, Object message) log(int level, Throwable t) log(int level, Object message, Throwable t) All level-specific logging methods now lean on these general-purpose log methods, which do the level threshold check, and then call alwaysLog.
1 parent 2005e7e commit 3a530d0

File tree

4 files changed

+144
-202
lines changed

4 files changed

+144
-202
lines changed

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

Lines changed: 2 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Base class for {@link LogService} implementations.
4343
*
4444
* @author Johannes Schindelin
45+
* @author Curtis Rueden
4546
*/
4647
public abstract class AbstractLogService extends AbstractService implements
4748
LogService
@@ -52,22 +53,6 @@ public abstract class AbstractLogService extends AbstractService implements
5253
private final Map<String, Integer> classAndPackageLevels =
5354
new HashMap<>();
5455

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

7358
public AbstractLogService() {
@@ -95,126 +80,9 @@ public AbstractLogService(final Properties properties) {
9580
propName.substring(logLevelPrefix.length());
9681
setLevel(classOrPackageName, LogLevel.value(properties.getProperty(propName)));
9782
}
98-
99-
}
100-
101-
// -- helper methods --
102-
103-
protected void log(final int level, final Object msg, final Throwable t) {
104-
if (level > getLevel()) return;
105-
106-
if (msg != null || t == null) {
107-
log(level, msg);
108-
}
109-
if (t != null) log(t);
110-
}
111-
112-
protected void log(final int level, final Object msg) {
113-
final String prefix = LogLevel.prefix(level);
114-
log((prefix == null ? "" : prefix + " ") + msg);
115-
}
116-
117-
// -- LogService methods --
118-
119-
@Override
120-
public void debug(final Object msg) {
121-
log(LogLevel.DEBUG, msg, null);
122-
}
123-
124-
@Override
125-
public void debug(final Throwable t) {
126-
log(LogLevel.DEBUG, null, t);
127-
}
128-
129-
@Override
130-
public void debug(final Object msg, final Throwable t) {
131-
log(LogLevel.DEBUG, msg, t);
132-
}
133-
134-
@Override
135-
public void error(final Object msg) {
136-
log(LogLevel.ERROR, msg, null);
137-
}
138-
139-
@Override
140-
public void error(final Throwable t) {
141-
log(LogLevel.ERROR, null, t);
142-
}
143-
144-
@Override
145-
public void error(final Object msg, final Throwable t) {
146-
log(LogLevel.ERROR, msg, t);
147-
}
148-
149-
@Override
150-
public void info(final Object msg) {
151-
log(LogLevel.INFO, msg, null);
152-
}
153-
154-
@Override
155-
public void info(final Throwable t) {
156-
log(LogLevel.INFO, null, t);
15783
}
15884

159-
@Override
160-
public void info(final Object msg, final Throwable t) {
161-
log(LogLevel.INFO, msg, t);
162-
}
163-
164-
@Override
165-
public void trace(final Object msg) {
166-
log(LogLevel.TRACE, msg, null);
167-
}
168-
169-
@Override
170-
public void trace(final Throwable t) {
171-
log(LogLevel.TRACE, null, t);
172-
}
173-
174-
@Override
175-
public void trace(final Object msg, final Throwable t) {
176-
log(LogLevel.TRACE, msg, t);
177-
}
178-
179-
@Override
180-
public void warn(final Object msg) {
181-
log(LogLevel.WARN, msg, null);
182-
}
183-
184-
@Override
185-
public void warn(final Throwable t) {
186-
log(LogLevel.WARN, null, t);
187-
}
188-
189-
@Override
190-
public void warn(final Object msg, final Throwable t) {
191-
log(LogLevel.WARN, msg, t);
192-
}
193-
194-
@Override
195-
public boolean isDebug() {
196-
return getLevel() >= LogLevel.DEBUG;
197-
}
198-
199-
@Override
200-
public boolean isError() {
201-
return getLevel() >= LogLevel.ERROR;
202-
}
203-
204-
@Override
205-
public boolean isInfo() {
206-
return getLevel() >= LogLevel.INFO;
207-
}
208-
209-
@Override
210-
public boolean isTrace() {
211-
return getLevel() >= LogLevel.TRACE;
212-
}
213-
214-
@Override
215-
public boolean isWarn() {
216-
return getLevel() >= LogLevel.WARN;
217-
}
85+
// -- Logger methods --
21886

21987
@Override
22088
public int getLevel() {

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

Lines changed: 138 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
package org.scijava.log;
3333

34+
import static org.scijava.log.LogLevel.DEBUG;
35+
import static org.scijava.log.LogLevel.ERROR;
36+
import static org.scijava.log.LogLevel.INFO;
37+
import static org.scijava.log.LogLevel.TRACE;
38+
import static org.scijava.log.LogLevel.WARN;
39+
3440
/**
3541
* Interface for objects which can produce log messages.
3642
* <p>
@@ -44,45 +50,137 @@
4450
*/
4551
public interface Logger {
4652

47-
void debug(Object msg);
48-
49-
void debug(Throwable t);
50-
51-
void debug(Object msg, Throwable t);
52-
53-
void error(Object msg);
54-
55-
void error(Throwable t);
56-
57-
void error(Object msg, Throwable t);
58-
59-
void info(Object msg);
60-
61-
void info(Throwable t);
62-
63-
void info(Object msg, Throwable t);
64-
65-
void trace(Object msg);
66-
67-
void trace(Throwable t);
68-
69-
void trace(Object msg, Throwable t);
70-
71-
void warn(Object msg);
72-
73-
void warn(Throwable t);
74-
75-
void warn(Object msg, Throwable t);
76-
77-
boolean isDebug();
78-
79-
boolean isError();
80-
81-
boolean isInfo();
82-
83-
boolean isTrace();
84-
85-
boolean isWarn();
86-
53+
default void debug(final Object msg) {
54+
log(DEBUG, msg);
55+
}
56+
57+
default void debug(final Throwable t) {
58+
log(DEBUG, t);
59+
}
60+
61+
default void debug(final Object msg, final Throwable t) {
62+
log(DEBUG, msg, t);
63+
}
64+
65+
default void error(final Object msg) {
66+
log(ERROR, msg);
67+
}
68+
69+
default void error(final Throwable t) {
70+
log(ERROR, t);
71+
}
72+
73+
default void error(final Object msg, final Throwable t) {
74+
log(ERROR, msg, t);
75+
}
76+
77+
default void info(final Object msg) {
78+
log(INFO, msg);
79+
}
80+
81+
default void info(final Throwable t) {
82+
log(INFO, t);
83+
}
84+
85+
default void info(final Object msg, final Throwable t) {
86+
log(INFO, msg, t);
87+
}
88+
89+
default void trace(final Object msg) {
90+
log(TRACE, msg);
91+
}
92+
93+
default void trace(final Throwable t) {
94+
log(TRACE, t);
95+
}
96+
97+
default void trace(final Object msg, final Throwable t) {
98+
log(TRACE, msg, t);
99+
}
100+
101+
default void warn(final Object msg) {
102+
log(WARN, msg);
103+
}
104+
105+
default void warn(final Throwable t) {
106+
log(WARN, t);
107+
}
108+
109+
default void warn(final Object msg, final Throwable t) {
110+
log(WARN, msg, t);
111+
}
112+
113+
default boolean isDebug() {
114+
return isLevel(DEBUG);
115+
}
116+
117+
default boolean isError() {
118+
return isLevel(ERROR);
119+
}
120+
121+
default boolean isInfo() {
122+
return isLevel(INFO);
123+
}
124+
125+
default boolean isTrace() {
126+
return isLevel(TRACE);
127+
}
128+
129+
default boolean isWarn() {
130+
return isLevel(WARN);
131+
}
132+
133+
default boolean isLevel(final int level) {
134+
return getLevel() >= level;
135+
}
136+
137+
/**
138+
* Logs a message.
139+
*
140+
* @param level The level at which the message will be logged. If the current
141+
* level (given by {@link #getLevel()} is below this one, no logging
142+
* is performed.
143+
* @param msg The message to log.
144+
*/
145+
default void log(final int level, final Object msg) {
146+
log(level, msg, null);
147+
}
148+
149+
/**
150+
* Logs an exception.
151+
*
152+
* @param level The level at which the exception will be logged. If the
153+
* current level (given by {@link #getLevel()} is below this one, no
154+
* logging is performed.
155+
* @param t The exception to log.
156+
*/
157+
default void log(final int level, final Throwable t) {
158+
log(level, null, t);
159+
}
160+
161+
/**
162+
* Logs a message with an exception.
163+
*
164+
* @param level The level at which the information will be logged. If the
165+
* current level (given by {@link #getLevel()} is below this one, no
166+
* logging is performed.
167+
* @param msg The message to log.
168+
* @param t The exception to log.
169+
*/
170+
default void log(final int level, final Object msg, final Throwable t) {
171+
if (isLevel(level)) alwaysLog(level, msg, t);
172+
}
173+
174+
/**
175+
* Logs a message with an exception. This message will always be logged even
176+
* if its level is above the current level (given by {@link #getLevel()}).
177+
*
178+
* @param level The level at which the information will be logged.
179+
* @param msg The message to log.
180+
* @param t The exception to log.
181+
*/
182+
void alwaysLog(int level, Object msg, Throwable t);
183+
184+
/** Returns the log level of this logger. see {@link LogLevel} */
87185
int getLevel();
88186
}

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,12 @@
5151
public class StderrLogService extends AbstractLogService {
5252

5353
@Override
54-
protected void log(final int level, final Object msg) {
54+
public void alwaysLog(final int level, final Object msg, final Throwable t) {
5555
final String prefix = LogLevel.prefix(level);
5656
final String message = (prefix == null ? "" : prefix + " ") + msg;
5757
// NB: Emit severe messages to stderr, and less severe ones to stdout.
5858
if (level <= LogLevel.WARN) System.err.println(message);
5959
else System.out.println(message);
60+
if (t != null) t.printStackTrace();
6061
}
61-
62-
/**
63-
* Prints a message to stderr.
64-
*
65-
* @param message the message
66-
*/
67-
@Override
68-
protected void log(final String message) {
69-
System.err.println(message);
70-
}
71-
72-
/**
73-
* Prints an exception to stderr.
74-
*
75-
* @param t the exception
76-
*/
77-
@Override
78-
protected void log(final Throwable t) {
79-
t.printStackTrace();
80-
}
81-
8262
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,8 @@ public Throwable exception() {
206206
}
207207

208208
@Override
209-
protected void log(String msg) {
210-
this.message = msg;
211-
}
212-
213-
@Override
214-
protected void log(Throwable t) {
209+
public void alwaysLog(int level, Object msg, Throwable t) {
210+
this.message = LogLevel.prefix(level) + msg;
215211
this.exception = t;
216212
}
217213
}

0 commit comments

Comments
 (0)