3131
3232package org .scijava .log ;
3333
34+ import java .io .PrintStream ;
3435import java .util .HashMap ;
3536import java .util .Map ;
3637import 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 ;
0 commit comments