Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.log

File JLoggerLog4j.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
37% of files have more coverage

Code metrics

20
70
13
1
187
146
37
0.53
5.38
13
2.85

Classes

Class Line # Actions
JLoggerLog4j 32 70 37
0.6019417660.2%
 

Contributing tests

This file is covered by 115 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.io.File;
24   
25    import org.apache.logging.log4j.Logger;
26    import org.apache.logging.log4j.core.Appender;
27   
28    import jalview.bin.Jalview;
29    import jalview.util.Log4j;
30    import jalview.util.Platform;
31   
 
32    public class JLoggerLog4j extends JLogger implements JLoggerI
33    {
34    private Logger logger = null;
35   
36    private static File logfile = null;
37   
 
38  0 toggle public static JLoggerLog4j getLogger(Class c)
39    {
40  0 return getLogger(c.getCanonicalName());
41    }
42   
 
43  0 toggle public static JLoggerLog4j getLogger(Class c, LogLevel loglevel)
44    {
45  0 return getLogger(c.getCanonicalName(), loglevel);
46    }
47   
 
48  0 toggle public static JLoggerLog4j getLogger(String name)
49    {
50  0 return getLogger(name, LogLevel.INFO);
51    }
52   
53    // This must be called before Log4j.init(), so before JLoggerLog4j.getLogger()
 
54  55 toggle public static void setLogfile(File f)
55    {
56  55 logfile = f;
57    }
58   
 
59  110 toggle public static JLoggerLog4j getLogger(String name, LogLevel loglevel)
60    {
61  110 if (!Platform.isJS())
62    {
63  110 if (!Jalview.quiet())
64    {
65  110 jalview.bin.Console
66    .errPrintln("Setting log level to " + loglevel.name());
67    }
68  110 Log4j.init(loglevel, logfile);
69    }
70   
71  110 return registryContainsKey(name) ? (JLoggerLog4j) registryGet(name)
72    : new JLoggerLog4j(name, loglevel);
73    }
74   
 
75  110 toggle private JLoggerLog4j(String name, LogLevel level)
76    {
77  110 this.name = name;
78  110 this.level = level;
79  110 this.loggerSetup();
80  110 this.registryStore();
81    }
82   
 
83  110 toggle @Override
84    protected void loggerSetup()
85    {
86  110 if (!Platform.isJS())
87  110 this.logger = Log4j.isInit() ? Log4j.getLogger(this.name, this.level)
88    : null;
89    }
90   
 
91  2893 toggle @Override
92    public boolean loggerExists()
93    {
94  2893 return logger != null;
95    }
96   
 
97  75 toggle @Override
98    protected void loggerSetLevel(JLoggerI.LogLevel level)
99    {
100  75 if (loggerExists())
101    {
102  75 Log4j.setLevel(logger, level);
103    }
104    }
105   
 
106  1344 toggle @Override
107    protected void loggerLogMessage(LogLevel level, String message,
108    Throwable t)
109    {
110  1344 if (!loggerExists())
111  0 return;
112  1344 if (t != null)
113    {
114  17 switch (level)
115    {
116  0 case FATAL:
117  0 logger.fatal(message, t);
118  0 break;
119  1 case ERROR:
120  1 logger.error(message, t);
121  1 break;
122  14 case WARN:
123  14 logger.warn(message, t);
124  14 break;
125  2 case INFO:
126  2 logger.info(message, t);
127  2 break;
128  0 case DEBUG:
129  0 logger.debug(message, t);
130  0 break;
131  0 case TRACE:
132  0 logger.trace(message, t);
133  0 break;
134  0 case ALL:
135  0 logger.trace(message, t);
136  0 break;
137    }
138    }
139    else
140    {
141  1327 switch (level)
142    {
143  0 case FATAL:
144  0 logger.fatal(message);
145  0 break;
146  33 case ERROR:
147  33 logger.error(message);
148  33 break;
149  93 case WARN:
150  93 logger.warn(message);
151  93 break;
152  1075 case INFO:
153  1075 logger.info(message);
154  1075 break;
155  126 case DEBUG:
156  126 logger.debug(message);
157  125 break;
158  0 case TRACE:
159  0 logger.trace(message);
160  0 break;
161  0 case ALL:
162  0 logger.trace(message);
163  0 break;
164    }
165    }
166    }
167   
 
168  75 toggle private Logger getLoggerObject()
169    {
170  75 return this.logger;
171    }
172   
 
173  75 toggle public synchronized static void addAppender(JLoggerLog4j level,
174    Appender appender)
175    {
176  75 if (!Platform.isJS())
177  75 Log4j.addAppender(level.getLoggerObject(), appender);
178    }
179   
 
180  0 toggle public synchronized static void addAppender(JLoggerLog4j l1,
181    JLoggerLog4j l2, String name)
182    {
183  0 if (!Platform.isJS())
184  0 Log4j.addAppender(l1.getLoggerObject(), l2.getLoggerObject(), name);
185    }
186   
187    }