Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 11:45:30 GMT
  2. Package jalview.log

File JLogger.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
45% of files have more coverage

Code metrics

14
49
28
1
243
184
37
0.76
1.75
28
1.32

Classes

Class Line # Actions
JLogger 30 49 37
0.5054945350.5%
 

Contributing tests

This file is covered by 466 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.log;
22   
23    import java.util.EnumSet;
24    import java.util.HashMap;
25    import java.util.Map;
26   
27    import jalview.bin.Console;
28    import jalview.util.Platform;
29   
 
30    public abstract class JLogger implements JLoggerI
31    {
32    protected String name;
33   
34    protected LogLevel level;
35   
36    private Object logger = null;
37   
38    private static Map<String, JLogger> registry = new HashMap<>();
39   
40    // implement these abstract methods
41    protected abstract void loggerSetup();
42   
43    public abstract boolean loggerExists();
44   
45    protected abstract void loggerSetLevel(LogLevel level);
46   
47    protected abstract void loggerLogMessage(LogLevel level, String message,
48    Throwable t);
49   
 
50  6 toggle public static boolean isLevel(String levelString)
51    {
52  6 for (LogLevel l : EnumSet.allOf(LogLevel.class))
53    {
54  19 if (l.name().equals(levelString))
55  6 return true;
56    }
57  0 return false;
58    }
59   
 
60  55 toggle public static LogLevel toLevel(String levelString)
61    {
62  55 try
63    {
64  55 return LogLevel.valueOf(levelString);
65    } catch (IllegalArgumentException e)
66    {
67  0 Console.error("Could not parse LogLevel '" + levelString + "'", e);
68  0 return LogLevel.INFO;
69    }
70    }
71   
 
72  0 toggle public static JLogger getLogger(Class c)
73    {
74  0 return getLogger(c, LogLevel.INFO);
75    }
76   
 
77  0 toggle public static JLogger getLogger(Class c, LogLevel loglevel)
78    {
79  0 return getLogger(c.getCanonicalName(), loglevel);
80    }
81   
 
82  0 toggle public static JLogger getLogger(String name)
83    {
84  0 return getLogger(name, LogLevel.INFO);
85    }
86   
 
87  0 toggle public static JLogger getLogger(String name, LogLevel loglevel)
88    {
89  0 return registry.containsKey(name) ? (JLogger) registry.get(name) : null;
90    }
91   
 
92  110 toggle protected JLogger()
93    {
94    }
95   
 
96  0 toggle protected JLogger(String name, LogLevel level)
97    {
98  0 this.name = name;
99  0 this.level = level;
100  0 this.loggerSetup();
101  0 this.registryStore();
102    }
103   
 
104  110 toggle protected void registryStore()
105    {
106  110 registry.put(this.name, this);
107    }
108   
 
109  110 toggle protected static boolean registryContainsKey(String name)
110    {
111  110 return registry.containsKey(name);
112    }
113   
 
114  0 toggle protected static JLogger registryGet(String name)
115    {
116  0 return registry.get(name);
117    }
118   
 
119  75 toggle @Override
120    public LogLevel getLevel()
121    {
122  75 return this.level;
123    }
124   
 
125  75 toggle @Override
126    public void setLevel(LogLevel level)
127    {
128  75 this.level = level;
129  75 if (loggerExists())
130  75 loggerSetLevel(level);
131    }
132   
 
133  18675 toggle private boolean println(LogLevel loglevel, String message, Throwable t)
134    {
135  18675 if (loglevel.compareTo(this.level) < 0)
136    {
137  16995 return false;
138    }
139  1680 if (!loggerExists() || Platform.isJS())
140    {
141  0 String logLine = String.format("%s: %s", loglevel.toString(),
142    message);
143  0 jalview.bin.Console.outPrintln(logLine);
144  0 if (t != null)
145    {
146  0 if (loglevel.compareTo(LogLevel.DEBUG) <= 0)
147  0 t.printStackTrace(System.err);
148    else
149  0 jalview.bin.Console.errPrintln(t.getMessage());
150    }
151  0 return false;
152    }
153    else
154    {
155  1680 loggerLogMessage(loglevel, message, t);
156  1677 return true;
157    }
158    }
159   
 
160  0 toggle @Override
161    public void trace(String message)
162    {
163  0 trace(message, null);
164    }
165   
 
166  2477 toggle @Override
167    public void trace(String message, Throwable t)
168    {
169  2477 println(LogLevel.TRACE, message, t);
170    }
171   
 
172  55 toggle @Override
173    public void debug(String message)
174    {
175  55 debug(message, null);
176    }
177   
 
178  14642 toggle @Override
179    public void debug(String message, Throwable t)
180    {
181  14642 println(LogLevel.DEBUG, message, t);
182    }
183   
 
184  0 toggle @Override
185    public void info(String message)
186    {
187  0 info(message, null);
188    }
189   
 
190  1421 toggle @Override
191    public void info(String message, Throwable t)
192    {
193  1421 println(LogLevel.INFO, message, t);
194    }
195   
 
196  0 toggle @Override
197    public void warn(String message)
198    {
199  0 warn(message, null);
200    }
201   
 
202  106 toggle @Override
203    public void warn(String message, Throwable t)
204    {
205  106 println(LogLevel.WARN, message, t);
206    }
207   
 
208  0 toggle @Override
209    public void error(String message)
210    {
211  0 error(message, null);
212    }
213   
 
214  29 toggle @Override
215    public void error(String message, Throwable t)
216    {
217  29 println(LogLevel.ERROR, message, t);
218    }
219   
 
220  0 toggle @Override
221    public void fatal(String message)
222    {
223  0 fatal(message, null);
224    }
225   
 
226  0 toggle @Override
227    public void fatal(String message, Throwable t)
228    {
229  0 println(LogLevel.FATAL, message, t);
230    }
231   
 
232  194 toggle @Override
233    public boolean isDebugEnabled()
234    {
235  194 return level.compareTo(LogLevel.DEBUG) <= 0;
236    }
237   
 
238  46 toggle @Override
239    public boolean isTraceEnabled()
240    {
241  46 return level.compareTo(LogLevel.TRACE) <= 0;
242    }
243    }