Skip to content

Commit 3f624c7

Browse files
committed
LogService: Add LogLevel class to organize logging levels
1 parent 1196466 commit 3f624c7

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,10 @@ protected void log(final int level, final Object msg, final Throwable t) {
107107
}
108108

109109
protected void log(final int level, final Object msg) {
110-
final String prefix = getPrefix(level);
110+
final String prefix = LogLevel.prefix(level);
111111
log((prefix == null ? "" : prefix + " ") + msg);
112112
}
113113

114-
protected String getPrefix(final int level) {
115-
switch (level) {
116-
case ERROR:
117-
return "[ERROR]";
118-
case WARN:
119-
return "[WARNING]";
120-
case INFO:
121-
return "[INFO]";
122-
case DEBUG:
123-
return "[DEBUG]";
124-
case TRACE:
125-
return "[TRACE]";
126-
default:
127-
return null;
128-
}
129-
}
130-
131114
// -- LogService methods --
132115

133116
@Override
@@ -255,6 +238,14 @@ public void setLevel(final String classOrPackageName, final int level) {
255238
classAndPackageLevels.put(classOrPackageName, level);
256239
}
257240

241+
// -- Deprecated --
242+
243+
/** @deprecated Use {@link LogLevel#prefix(int)} instead. */
244+
@Deprecated
245+
protected String getPrefix(final int level) {
246+
return "[" + LogLevel.prefix(level) + "]";
247+
}
248+
258249
// -- Helper methods --
259250

260251
/** Extracts the log level value from a string. */
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/**
35+
* Constants for specifying a logger's level of verbosity.
36+
*
37+
* @author Curtis Rueden
38+
*/
39+
public final class LogLevel {
40+
41+
private LogLevel() {
42+
// prevent instantiation of utility class
43+
}
44+
45+
public static final int NONE = 0;
46+
public static final int ERROR = 1;
47+
public static final int WARN = 2;
48+
public static final int INFO = 3;
49+
public static final int DEBUG = 4;
50+
public static final int TRACE = 5;
51+
52+
public static String prefix(final int level) {
53+
switch (level) {
54+
case ERROR:
55+
return "ERROR";
56+
case WARN:
57+
return "WARNING";
58+
case INFO:
59+
return "INFO";
60+
case DEBUG:
61+
return "DEBUG";
62+
case TRACE:
63+
return "TRACE";
64+
default:
65+
return "LEVEL" + level;
66+
}
67+
}
68+
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,26 @@ public interface LogService extends SciJavaService {
5050
/** System property to set for overriding the default logging level. */
5151
String LOG_LEVEL_PROPERTY = "scijava.log.level";
5252

53-
int NONE = 0;
54-
int ERROR = 1;
55-
int WARN = 2;
56-
int INFO = 3;
57-
int DEBUG = 4;
58-
int TRACE = 5;
53+
// -- Deprecated --
54+
55+
/** @deprecated Use {@link LogLevel#NONE}. */
56+
@Deprecated
57+
int NONE = LogLevel.NONE;
58+
/** @deprecated Use {@link LogLevel#ERROR}. */
59+
@Deprecated
60+
int ERROR = LogLevel.ERROR;
61+
/** @deprecated Use {@link LogLevel#WARN}. */
62+
@Deprecated
63+
int WARN = LogLevel.WARN;
64+
/** @deprecated Use {@link LogLevel#INFO}. */
65+
@Deprecated
66+
int INFO = LogLevel.INFO;
67+
/** @deprecated Use {@link LogLevel#DEBUG}. */
68+
@Deprecated
69+
int DEBUG = LogLevel.DEBUG;
70+
/** @deprecated Use {@link LogLevel#TRACE}. */
71+
@Deprecated
72+
int TRACE = LogLevel.TRACE;
5973

6074
void debug(Object msg);
6175

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class StderrLogService extends AbstractLogService {
5252

5353
@Override
5454
protected void log(final int level, final Object msg) {
55-
final String prefix = getPrefix(level);
55+
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 <= WARN) System.err.println(message);

0 commit comments

Comments
 (0)