2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.io.PrintStream;
24 import java.util.Locale;
26 import jalview.log.JLogger;
27 import jalview.log.JLoggerI;
28 import jalview.log.JLoggerI.LogLevel;
29 import jalview.log.JLoggerLog4j;
30 import jalview.util.ChannelProperties;
31 import jalview.util.Log4j;
32 import jalview.util.Platform;
37 public static JLoggerLog4j log;
39 public static void debug(String message, Throwable t)
41 if (Console.initLogger())
43 log.debug(message, t);
48 Console.printStackTrace(t);
53 public static void info(String message)
55 if (Console.initLogger())
57 log.info(message, null);
66 public static void trace(String message, Throwable t)
68 if (Console.initLogger())
70 log.trace(message, t);
75 Console.printStackTrace(t);
79 public static void debug(String message)
81 if (Console.initLogger())
83 log.debug(message, null);
92 public static void info(String message, Throwable t)
94 if (Console.initLogger())
101 Console.printStackTrace(t);
106 public static void warn(String message)
108 if (Console.initLogger())
110 log.warn(message, null);
119 public static void trace(String message)
121 if (Console.initLogger())
123 log.trace(message, null);
131 public static void warn(String message, Throwable t)
133 if (Console.initLogger())
135 log.warn(message, t);
140 Console.printStackTrace(t);
145 public static void error(String message)
147 if (Console.initLogger())
149 log.error(message, null);
153 jalview.bin.Console.errPrintln(message);
158 public static void error(String message, Throwable t)
160 if (Console.initLogger())
162 log.error(message, t);
166 jalview.bin.Console.errPrintln(message);
167 Console.printStackTrace(t);
172 public static void fatal(String message)
174 if (Console.initLogger())
176 log.fatal(message, null);
180 jalview.bin.Console.errPrintln(message);
185 public static void fatal(String message, Throwable t)
187 if (Console.initLogger())
189 log.fatal(message, t);
193 jalview.bin.Console.errPrintln(message);
194 Console.printStackTrace(t);
199 public static boolean isDebugEnabled()
201 return log == null ? false : log.isDebugEnabled();
204 public static boolean isTraceEnabled()
206 return log == null ? false : log.isTraceEnabled();
209 public static JLogger.LogLevel getCachedLogLevel()
211 return Console.getCachedLogLevel(Cache.JALVIEWLOGLEVEL);
214 public static JLogger.LogLevel getCachedLogLevel(String key)
216 return getLogLevel(Cache.getDefault(key, "INFO"));
219 public static JLogger.LogLevel getLogLevel(String level)
221 return JLogger.toLevel(level);
224 public static JLogger getLogger()
229 public static boolean initLogger()
231 return initLogger(null);
234 public static boolean initLogger(String providedLogLevel)
242 JLogger.LogLevel logLevel = JLogger.LogLevel.INFO;
244 if (JLogger.isLevel(providedLogLevel))
246 logLevel = Console.getLogLevel(providedLogLevel);
250 logLevel = getCachedLogLevel();
253 if (!Platform.isJS())
255 if (!Jalview.quiet())
257 jalview.bin.Console.errPrintln(
258 "Setting initial log level to " + logLevel.name());
260 Log4j.init(logLevel);
263 // is laxis used? Does getLogger do anything without a Logger object?
264 // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
265 JLoggerLog4j.getLogger("org.apache.axis", logLevel);
267 // The main application logger
268 log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
269 } catch (NoClassDefFoundError e)
272 .errPrintln("Could not initialise the logger framework");
273 Console.printStackTrace(e);
279 // Logging test message should go through the logger object
280 if (log.loggerExists())
281 log.debug(Console.LOGGING_TEST_MESSAGE);
282 // Tell the user that debug is enabled
283 debug(ChannelProperties.getProperty("app_name")
284 + " Debugging Output Follows.");
293 public static void setLogLevel(String logLevelString)
295 for (LogLevel logLevel : JLoggerI.LogLevel.values())
297 if (logLevel.toString().toLowerCase(Locale.ROOT)
298 .equals(logLevelString.toLowerCase(Locale.ROOT)))
300 log.setLevel(logLevel);
301 if (!Platform.isJS())
303 Log4j.init(logLevel);
305 JLoggerLog4j.getLogger("org.apache.axis", logLevel);
311 public static void outPrint()
316 public static void outPrintln()
321 public static void outPrint(Object message)
323 outPrintMessage(message, false, false);
326 public static void outPrint(Object message, boolean forceStdout)
328 outPrintMessage(message, false, forceStdout);
331 public static void outPrintln(Object message)
333 outPrintMessage(message, true, false);
336 public static PrintStream outputStream(boolean forceStdout)
338 // send message to stderr if an output file to stdout is expected
339 if (!forceStdout && Jalview.getInstance() != null
340 && Jalview.getInstance().getBootstrapArgs() != null
341 && Jalview.getInstance().getBootstrapArgs().outputToStdout())
351 public static void outPrintMessage(Object message, boolean newline,
354 PrintStream ps = outputStream(forceStdout);
365 public static void errPrint()
370 public static void errPrintln()
375 public static void errPrint(Object message)
377 System.err.print(message);
380 public static void errPrintln(Object message)
382 System.err.println(message);
385 public static void debugPrintStackTrace(Throwable t)
387 if (!isDebugEnabled())
391 // send message to stderr if output to stdout is expected
395 public static void printStackTrace(Throwable t)
397 // send message to stderr if output to stdout is expected
398 t.printStackTrace(System.err);
401 public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";