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.EnumSet;
24 import java.util.HashMap;
27 import jalview.bin.Console;
28 import jalview.util.Platform;
30 public abstract class JLogger implements JLoggerI
32 protected String name;
34 protected LogLevel level;
36 private Object logger = null;
38 private static Map<String, JLogger> registry = new HashMap<>();
40 // implement these abstract methods
41 protected abstract void loggerSetup();
43 public abstract boolean loggerExists();
45 protected abstract void loggerSetLevel(LogLevel level);
47 protected abstract void loggerLogMessage(LogLevel level, String message,
50 public static boolean isLevel(String levelString)
52 for (LogLevel l : EnumSet.allOf(LogLevel.class))
54 if (l.name().equals(levelString))
60 public static LogLevel toLevel(String levelString)
64 return LogLevel.valueOf(levelString);
65 } catch (IllegalArgumentException e)
67 Console.error("Could not parse LogLevel '" + levelString + "'", e);
72 public static JLogger getLogger(Class c)
74 return getLogger(c, LogLevel.INFO);
77 public static JLogger getLogger(Class c, LogLevel loglevel)
79 return getLogger(c.getCanonicalName(), loglevel);
82 public static JLogger getLogger(String name)
84 return getLogger(name, LogLevel.INFO);
87 public static JLogger getLogger(String name, LogLevel loglevel)
89 return registry.containsKey(name) ? (JLogger) registry.get(name) : null;
96 protected JLogger(String name, LogLevel level)
101 this.registryStore();
104 protected void registryStore()
106 registry.put(this.name, this);
109 protected static boolean registryContainsKey(String name)
111 return registry.containsKey(name);
114 protected static JLogger registryGet(String name)
116 return registry.get(name);
119 public LogLevel getLevel()
124 public void setLevel(LogLevel level)
128 loggerSetLevel(level);
131 private boolean println(LogLevel loglevel, String message, Throwable t)
133 if (loglevel.compareTo(this.level) < 0)
137 if (!loggerExists() || Platform.isJS())
139 String logLine = String.format("%s: %s", loglevel.toString(),
141 jalview.bin.Console.outPrintln(logLine);
144 if (loglevel.compareTo(LogLevel.DEBUG) <= 0)
145 t.printStackTrace(System.err);
147 jalview.bin.Console.errPrintln(t.getMessage());
153 loggerLogMessage(loglevel, message, t);
158 public void trace(String message)
160 trace(message, null);
163 public void trace(String message, Throwable t)
165 println(LogLevel.TRACE, message, t);
168 public void debug(String message)
170 debug(message, null);
173 public void debug(String message, Throwable t)
175 println(LogLevel.DEBUG, message, t);
178 public void info(String message)
183 public void info(String message, Throwable t)
185 println(LogLevel.INFO, message, t);
188 public void warn(String message)
193 public void warn(String message, Throwable t)
195 println(LogLevel.WARN, message, t);
198 public void error(String message)
200 error(message, null);
203 public void error(String message, Throwable t)
205 println(LogLevel.ERROR, message, t);
208 public void fatal(String message)
210 fatal(message, null);
213 public void fatal(String message, Throwable t)
215 println(LogLevel.FATAL, message, t);
218 public boolean isDebugEnabled()
220 return level.compareTo(LogLevel.DEBUG) <= 0;
223 public boolean isTraceEnabled()
225 return level.compareTo(LogLevel.TRACE) <= 0;