Skip to content

Commit 0caa53a

Browse files
committed
AbstractLogService: migrate log level extraction to LogLevel class
1 parent 3f624c7 commit 0caa53a

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public AbstractLogService() {
7575

7676
// global log level property
7777
final String logProp = System.getProperty(LOG_LEVEL_PROPERTY);
78-
final int level = level(logProp);
78+
final int level = LogLevel.value(logProp);
7979
if (level >= 0) setLevel(level);
8080

8181
if (getLevel() == 0)
@@ -90,7 +90,7 @@ public AbstractLogService() {
9090
if (!propName.startsWith(logLevelPrefix)) continue;
9191
final String classOrPackageName =
9292
propName.substring(logLevelPrefix.length());
93-
setLevel(classOrPackageName, level(props.getProperty(propName)));
93+
setLevel(classOrPackageName, LogLevel.value(props.getProperty(propName)));
9494
}
9595

9696
}
@@ -248,29 +248,6 @@ protected String getPrefix(final int level) {
248248

249249
// -- Helper methods --
250250

251-
/** Extracts the log level value from a string. */
252-
private int level(final String logProp) {
253-
if (logProp == null) return -1;
254-
255-
// check whether it's a string label (e.g., "debug")
256-
final String log = logProp.trim().toLowerCase();
257-
if (log.startsWith("n")) return NONE;
258-
if (log.startsWith("e")) return ERROR;
259-
if (log.startsWith("w")) return WARN;
260-
if (log.startsWith("i")) return INFO;
261-
if (log.startsWith("d")) return DEBUG;
262-
if (log.startsWith("t")) return TRACE;
263-
264-
// check whether it's a numerical value (e.g., 5)
265-
try {
266-
return Integer.parseInt(log);
267-
}
268-
catch (final NumberFormatException exc) {
269-
// nope!
270-
}
271-
return -1;
272-
}
273-
274251
private String callingClass() {
275252
final String thisClass = AbstractLogService.class.getName();
276253
for (final StackTraceElement element : new Exception().getStackTrace()) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,32 @@ public static String prefix(final int level) {
6565
return "LEVEL" + level;
6666
}
6767
}
68+
69+
/**
70+
* Extracts the log level value from a string.
71+
*
72+
* @return The log level, or -1 if the level cannot be parsed.
73+
*/
74+
public static int value(final String s) {
75+
if (s == null) return -1;
76+
77+
// check whether it's a string label (e.g., "debug")
78+
final String log = s.trim().toLowerCase();
79+
if (log.startsWith("n")) return LogLevel.NONE;
80+
if (log.startsWith("e")) return LogLevel.ERROR;
81+
if (log.startsWith("w")) return LogLevel.WARN;
82+
if (log.startsWith("i")) return LogLevel.INFO;
83+
if (log.startsWith("d")) return LogLevel.DEBUG;
84+
if (log.startsWith("t")) return LogLevel.TRACE;
85+
86+
// check whether it's a numerical value (e.g., 5)
87+
try {
88+
return Integer.parseInt(log);
89+
}
90+
catch (final NumberFormatException exc) {
91+
// nope!
92+
}
93+
return -1;
94+
}
95+
6896
}

0 commit comments

Comments
 (0)