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;
25 import jalview.log.JLogger;
26 import jalview.log.JLoggerI.LogLevel;
27 import jalview.log.JLoggerLog4j;
28 import jalview.util.ChannelProperties;
29 import jalview.util.Log4j;
30 import jalview.util.Platform;
35 public static JLoggerLog4j log;
37 public static void debug(String message, Throwable t)
39 if (Console.initLogger())
41 log.debug(message, t);
46 Console.printStackTrace(t);
51 public static void info(String message)
53 if (Console.initLogger())
55 log.info(message, null);
64 public static void trace(String message, Throwable t)
66 if (Console.initLogger())
68 log.trace(message, t);
73 Console.printStackTrace(t);
77 public static void debug(String message)
79 if (Console.initLogger())
81 log.debug(message, null);
90 public static void info(String message, Throwable t)
92 if (Console.initLogger())
99 Console.printStackTrace(t);
104 public static void warn(String message)
106 if (Console.initLogger())
108 log.warn(message, null);
117 public static void trace(String message)
119 if (Console.initLogger())
121 log.trace(message, null);
129 public static void warn(String message, Throwable t)
131 if (Console.initLogger())
133 log.warn(message, t);
138 Console.printStackTrace(t);
143 public static void error(String message)
145 if (Console.initLogger())
147 log.error(message, null);
151 jalview.bin.Console.errPrintln(message);
156 public static void error(String message, Throwable t)
158 if (Console.initLogger())
160 log.error(message, t);
164 jalview.bin.Console.errPrintln(message);
165 Console.printStackTrace(t);
170 public static void fatal(String message)
172 if (Console.initLogger())
174 log.fatal(message, null);
178 jalview.bin.Console.errPrintln(message);
183 public static void fatal(String message, Throwable t)
185 if (Console.initLogger())
187 log.fatal(message, t);
191 jalview.bin.Console.errPrintln(message);
192 Console.printStackTrace(t);
197 public static boolean isDebugEnabled()
199 return log == null ? false : log.isDebugEnabled();
202 public static boolean isTraceEnabled()
204 return log == null ? false : log.isTraceEnabled();
207 public static JLogger.LogLevel getCachedLogLevel()
209 return Console.getCachedLogLevel(Cache.JALVIEWLOGLEVEL);
212 public static JLogger.LogLevel getCachedLogLevel(String key)
214 return getLogLevel(Cache.getDefault(key, "INFO"));
217 public static JLogger.LogLevel getLogLevel(String level)
219 return JLogger.toLevel(level);
222 public static JLogger getLogger()
227 public static boolean initLogger()
229 return initLogger(null);
232 public static boolean initLogger(String providedLogLevel)
240 JLogger.LogLevel logLevel = JLogger.LogLevel.INFO;
242 if (providedLogLevel != null && JLogger.isLevel(providedLogLevel))
244 logLevel = Console.getLogLevel(providedLogLevel);
248 logLevel = getCachedLogLevel();
251 if (!Platform.isJS())
253 if (!Jalview.quiet())
255 jalview.bin.Console.errPrintln(
256 "Setting initial log level to " + logLevel.name());
258 Log4j.init(logLevel);
261 // is laxis used? Does getLogger do anything without a Logger object?
262 // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
263 JLoggerLog4j.getLogger("org.apache.axis", logLevel);
265 // The main application logger
266 log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
267 } catch (NoClassDefFoundError e)
270 .errPrintln("Could not initialise the logger framework");
271 Console.printStackTrace(e);
277 // Logging test message should go through the logger object
278 if (log.loggerExists())
279 log.debug(Console.LOGGING_TEST_MESSAGE);
280 // Tell the user that debug is enabled
281 debug(ChannelProperties.getProperty("app_name")
282 + " Debugging Output Follows.");
291 public static void setLogLevel(String logLevelString)
296 l = LogLevel.valueOf(logLevelString);
297 } catch (IllegalArgumentException | NullPointerException e1)
299 Console.debug("Invalid log level '" + logLevelString + "'");
305 if (!Platform.isJS())
309 JLoggerLog4j.getLogger("org.apache.axis", l);
313 public static void outPrint()
318 public static void outPrintln()
323 public static void outPrint(Object message)
325 outPrintMessage(message, false, false);
328 public static void outPrint(Object message, boolean forceStdout)
330 outPrintMessage(message, false, forceStdout);
333 public static void outPrintln(Object message)
335 outPrintMessage(message, true, false);
338 public static PrintStream outputStream(boolean forceStdout)
340 // send message to stderr if an output file to stdout is expected
341 if (!forceStdout && Jalview.getInstance() != null
342 && Jalview.getInstance().getBootstrapArgs() != null
343 && Jalview.getInstance().getBootstrapArgs().outputToStdout())
353 public static void outPrintMessage(Object message, boolean newline,
356 PrintStream ps = outputStream(forceStdout);
367 public static void errPrint()
372 public static void errPrintln()
377 public static void errPrint(Object message)
379 System.err.print(message);
382 public static void errPrintln(Object message)
384 System.err.println(message);
387 public static void debugPrintStackTrace(Throwable t)
389 if (!isDebugEnabled())
393 // send message to stderr if output to stdout is expected
397 public static void printStackTrace(Throwable t)
399 // send message to stderr if output to stdout is expected
400 t.printStackTrace(System.err);
403 public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";