Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.log

File JLoggerLog4j.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
27% 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.5728155457.3%
 

Contributing tests

This file is covered by 73 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  6 toggle public static void setLogfile(File f)
55    {
56  6 logfile = f;
57    }
58   
 
59  12 toggle public static JLoggerLog4j getLogger(String name, LogLevel loglevel)
60    {
61  12 if (!Platform.isJS())
62    {
63  12 if (!Jalview.quiet())
64    {
65  12 jalview.bin.Console
66    .errPrintln("Setting log level to " + loglevel.name());
67    }
68  12 Log4j.init(loglevel, logfile);
69    }
70   
71  12 return registryContainsKey(name) ? (JLoggerLog4j) registryGet(name)
72    : new JLoggerLog4j(name, loglevel);
73    }
74   
 
75  12 toggle private JLoggerLog4j(String name, LogLevel level)
76    {
77  12 this.name = name;
78  12 this.level = level;
79  12 this.loggerSetup();
80  12 this.registryStore();
81    }
82   
 
83  12 toggle @Override
84    protected void loggerSetup()
85    {
86  12 if (!Platform.isJS())
87  12 this.logger = Log4j.isInit() ? Log4j.getLogger(this.name, this.level)
88    : null;
89    }
90   
 
91  1860 toggle @Override
92    public boolean loggerExists()
93    {
94  1860 return logger != null;
95    }
96   
 
97  47 toggle @Override
98    protected void loggerSetLevel(JLoggerI.LogLevel level)
99    {
100  47 if (loggerExists())
101    {
102  47 Log4j.setLevel(logger, level);
103    }
104    }
105   
 
106  880 toggle @Override
107    protected void loggerLogMessage(LogLevel level, String message,
108    Throwable t)
109    {
110  880 if (!loggerExists())
111  0 return;
112  880 if (t != null)
113    {
114  11 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  10 case WARN:
123  10 logger.warn(message, t);
124  10 break;
125  0 case INFO:
126  0 logger.info(message, t);
127  0 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  869 switch (level)
142    {
143  0 case FATAL:
144  0 logger.fatal(message);
145  0 break;
146  5 case ERROR:
147  5 logger.error(message);
148  5 break;
149  33 case WARN:
150  33 logger.warn(message);
151  33 break;
152  705 case INFO:
153  705 logger.info(message);
154  705 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  47 toggle private Logger getLoggerObject()
169    {
170  47 return this.logger;
171    }
172   
 
173  47 toggle public synchronized static void addAppender(JLoggerLog4j level,
174    Appender appender)
175    {
176  47 if (!Platform.isJS())
177  47 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    }