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);
268 // Jmol logging. Reduce logging level for INFO.
269 int jmolLogLevel = org.jmol.util.Logger.LEVEL_INFO;
273 jmolLogLevel = org.jmol.util.Logger.LEVEL_FATAL;
276 jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
279 jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
282 jmolLogLevel = org.jmol.util.Logger.LEVEL_ERROR;
285 jmolLogLevel = org.jmol.util.Logger.LEVEL_INFO;
288 jmolLogLevel = org.jmol.util.Logger.LEVEL_DEBUGHIGH;
291 jmolLogLevel = org.jmol.util.Logger.LEVEL_MAX;
294 org.jmol.util.Logger.setLogLevel(jmolLogLevel);
295 // org.jmol.util.Logger.setLogLevel(org.jmol.util.Logger.LEVEL_ERROR);
296 } catch (NoClassDefFoundError e)
299 .errPrintln("Could not initialise the logger framework");
300 Console.printStackTrace(e);
306 // Logging test message should go through the logger object
307 if (log.loggerExists())
308 log.debug(Console.LOGGING_TEST_MESSAGE);
309 // Tell the user that debug is enabled
310 debug(ChannelProperties.getProperty("app_name")
311 + " Debugging Output Follows.");
320 public static void setLogLevel(String logLevelString)
325 l = LogLevel.valueOf(logLevelString);
326 } catch (IllegalArgumentException | NullPointerException e1)
328 Console.debug("Invalid log level '" + logLevelString + "'");
334 if (!Platform.isJS())
338 JLoggerLog4j.getLogger("org.apache.axis", l);
342 public static void outPrint()
347 public static void outPrintln()
352 public static void outPrint(Object message)
354 outPrintMessage(message, false, false);
357 public static void outPrint(Object message, boolean forceStdout)
359 outPrintMessage(message, false, forceStdout);
362 public static void outPrintln(Object message)
364 outPrintMessage(message, true, false);
367 public static PrintStream outputStream(boolean forceStdout)
369 // send message to stderr if an output file to stdout is expected
370 if (!forceStdout && Jalview.getInstance() != null
371 && Jalview.getInstance().getBootstrapArgs() != null
372 && Jalview.getInstance().getBootstrapArgs().outputToStdout())
382 public static void outPrintMessage(Object message, boolean newline,
385 PrintStream ps = outputStream(forceStdout);
396 public static void errPrint()
401 public static void errPrintln()
406 public static void errPrint(Object message)
408 System.err.print(message);
411 public static void errPrintln(Object message)
413 System.err.println(message);
416 public static void debugPrintStackTrace(Throwable t)
418 if (!isDebugEnabled())
422 // send message to stderr if output to stdout is expected
426 public static void printStackTrace(Throwable t)
428 // send message to stderr if output to stdout is expected
429 t.printStackTrace(System.err);
432 public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";