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.util.HashMap;
26 import jalview.bin.Console;
27 import jalview.util.Platform;
29 public abstract class JLogger implements JLoggerI
31 protected String name;
33 protected LogLevel level;
35 private Object logger = null;
37 private static Map<String, JLogger> registry = new HashMap<>();
39 // implement these abstract methods
40 protected abstract void loggerSetup();
42 public abstract boolean loggerExists();
44 protected abstract void loggerSetLevel(LogLevel level);
46 protected abstract void loggerLogMessage(LogLevel level, String message,
49 public static LogLevel toLevel(String levelString)
53 return LogLevel.valueOf(levelString);
54 } catch (IllegalArgumentException e)
56 Console.error("Could not parse LogLevel '" + levelString + "'", e);
61 public static JLogger getLogger(Class c)
66 public static JLogger getLogger(Class c, LogLevel loglevel)
68 return getLogger(c.getCanonicalName(), loglevel);
71 public static JLogger getLogger(String name)
73 return getLogger(name, LogLevel.INFO);
76 public static JLogger getLogger(String name, LogLevel loglevel)
78 return registry.containsKey(name) ? (JLogger) registry.get(name) : null;
85 protected JLogger(String name, LogLevel level)
93 protected void registryStore()
95 registry.put(this.name, this);
98 protected static boolean registryContainsKey(String name)
100 return registry.containsKey(name);
103 protected static JLogger registryGet(String name)
105 return registry.get(name);
108 public LogLevel getLevel()
113 public void setLevel(LogLevel level)
117 loggerSetLevel(level);
120 private boolean println(LogLevel loglevel, String message, Throwable t)
122 if (loglevel.compareTo(this.level) < 0)
126 if (!loggerExists() || Platform.isJS())
128 String logLine = String.format("%s: %s", loglevel.toString(),
130 System.out.println(logLine);
133 if (loglevel.compareTo(LogLevel.DEBUG) <= 0)
134 t.printStackTrace(System.err);
136 System.err.println(t.getMessage());
142 loggerLogMessage(loglevel, message, t);
147 public void trace(String message)
149 trace(message, null);
152 public void trace(String message, Throwable t)
154 println(LogLevel.TRACE, message, t);
157 public void debug(String message)
159 debug(message, null);
162 public void debug(String message, Throwable t)
164 println(LogLevel.DEBUG, message, t);
167 public void info(String message)
172 public void info(String message, Throwable t)
174 println(LogLevel.INFO, message, t);
177 public void warn(String message)
182 public void warn(String message, Throwable t)
184 println(LogLevel.WARN, message, t);
187 public void error(String message)
189 error(message, null);
192 public void error(String message, Throwable t)
194 println(LogLevel.ERROR, message, t);
197 public void fatal(String message)
199 fatal(message, null);
202 public void fatal(String message, Throwable t)
204 println(LogLevel.FATAL, message, t);
207 public boolean isDebugEnabled()
209 return level.compareTo(LogLevel.DEBUG) <= 0;
212 public boolean isTraceEnabled()
214 return level.compareTo(LogLevel.TRACE) <= 0;