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 boolean isLevel(String levelString)
51 for (LogLevel l : LogLevel.values())
53 if (l.name().equals(levelString))
59 public static LogLevel toLevel(String levelString)
63 return LogLevel.valueOf(levelString);
64 } catch (IllegalArgumentException e)
66 Console.error("Could not parse LogLevel '" + levelString + "'", e);
71 public static JLogger getLogger(Class c)
73 return getLogger(c, LogLevel.INFO);
76 public static JLogger getLogger(Class c, LogLevel loglevel)
78 return getLogger(c.getCanonicalName(), loglevel);
81 public static JLogger getLogger(String name)
83 return getLogger(name, LogLevel.INFO);
86 public static JLogger getLogger(String name, LogLevel loglevel)
88 return registry.containsKey(name) ? (JLogger) registry.get(name) : null;
95 protected JLogger(String name, LogLevel level)
100 this.registryStore();
103 protected void registryStore()
105 registry.put(this.name, this);
108 protected static boolean registryContainsKey(String name)
110 return registry.containsKey(name);
113 protected static JLogger registryGet(String name)
115 return registry.get(name);
118 public LogLevel getLevel()
123 public void setLevel(LogLevel level)
127 loggerSetLevel(level);
130 private boolean println(LogLevel loglevel, String message, Throwable t)
132 if (loglevel.compareTo(this.level) < 0)
136 if (!loggerExists() || Platform.isJS())
138 String logLine = String.format("%s: %s", loglevel.toString(),
140 System.out.println(logLine);
143 if (loglevel.compareTo(LogLevel.DEBUG) <= 0)
144 t.printStackTrace(System.err);
146 System.err.println(t.getMessage());
152 loggerLogMessage(loglevel, message, t);
157 public void trace(String message)
159 trace(message, null);
162 public void trace(String message, Throwable t)
164 println(LogLevel.TRACE, message, t);
167 public void debug(String message)
169 debug(message, null);
172 public void debug(String message, Throwable t)
174 println(LogLevel.DEBUG, message, t);
177 public void info(String message)
182 public void info(String message, Throwable t)
184 println(LogLevel.INFO, message, t);
187 public void warn(String message)
192 public void warn(String message, Throwable t)
194 println(LogLevel.WARN, message, t);
197 public void error(String message)
199 error(message, null);
202 public void error(String message, Throwable t)
204 println(LogLevel.ERROR, message, t);
207 public void fatal(String message)
209 fatal(message, null);
212 public void fatal(String message, Throwable t)
214 println(LogLevel.FATAL, message, t);
217 public boolean isDebugEnabled()
219 return level.compareTo(LogLevel.DEBUG) <= 0;
222 public boolean isTraceEnabled()
224 return level.compareTo(LogLevel.TRACE) <= 0;