1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File LogbackLogger.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
88% of files have more coverage

Code metrics

16
42
5
1
101
77
17
0.4
8.4
5
3.4

Classes

Class Line # Actions
LogbackLogger 14 42 0% 17 24
0.6190476461.9%
 

Contributing tests

This file is covered by 392 tests. .

Source view

1    package org.europa.together.application;
2   
3    import java.io.File;
4    import java.util.Arrays;
5    import org.europa.together.business.Logger;
6    import org.europa.together.domain.LogLevel;
7    import org.europa.together.utils.Constraints;
8    import org.slf4j.LoggerFactory;
9   
10    /**
11    * Implementation of the Logger as Wrapper for SLF4j and logback Framework.
12    */
13    //@Repository
 
14    public class LogbackLogger implements Logger {
15   
16    private static final long serialVersionUID = 1L;
17    private final String configurationFile = Constraints.SYSTEM_APP_DIR + "/logback.xml";
18    private Class<?> instance = null;
19    private org.slf4j.Logger logger = null;
20   
21    /**
22    * Create an instance of the logging class. THe configuration file will be
23    * searched in the Directory, where the application is running.<br>
24    *
25    * @see Logger.SYSTEM_APP_DIR /logback.xml If is no configuration present at
26    * this position, the logger use the default configuration in the classpath,
27    * with the console appender.
28    *
29    * @param instance The instance of the logged CLASS
30    */
 
31  73 toggle public LogbackLogger(final Class<?> instance) {
32  73 this.instance = instance;
33   
34    //FALLBACK
35  73 if (new File(configurationFile).exists()) {
36  1 System.setProperty("configurationFile", configurationFile);
37    }
38    }
39   
 
40  5135 toggle @Override
41    public LogLevel log(final String message, final LogLevel level) {
42  5135 instanceLogger();
43  5135 switch (level) {
44  226 case TRACE:
45  226 logger.trace(message);
46  226 break;
47  4604 case DEBUG:
48  4604 logger.debug(message);
49  4604 break;
50  154 case INFO:
51  154 logger.info(message);
52  154 break;
53  134 case WARN:
54  134 logger.warn(message);
55  134 break;
56  16 default:
57  16 logger.error(message);
58  16 break;
59    }
60  5134 return level;
61    }
62   
 
63  0 toggle @Override
64    public LogLevel getConfiguredLogLevel() {
65  0 instanceLogger();
66  0 LogLevel level = null;
67  0 if (logger.isErrorEnabled()) {
68  0 level = LogLevel.ERROR;
69    }
70  0 if (logger.isWarnEnabled()) {
71  0 level = LogLevel.WARN;
72    }
73  0 if (logger.isInfoEnabled()) {
74  0 level = LogLevel.INFO;
75    }
76  0 if (logger.isDebugEnabled()) {
77  0 level = LogLevel.DEBUG;
78    }
79  0 if (logger.isTraceEnabled()) {
80  0 level = LogLevel.TRACE;
81    }
82  0 return level;
83    }
84   
 
85  57 toggle @Override
86    public String catchException(final Exception ex) {
87  57 instanceLogger();
88  57 String exceptionType = ex.getClass().getSimpleName();
89  57 logger.error(exceptionType + ": " + ex.getMessage());
90  57 if (exceptionType.equals("NullPointerException")) {
91  16 logger.error(Arrays.toString(ex.getStackTrace()));
92    }
93  57 return ex.getMessage();
94    }
95   
 
96  5192 toggle private void instanceLogger() {
97  5192 if (logger == null) {
98  66 logger = LoggerFactory.getILoggerFactory().getLogger(instance.getName());
99    }
100    }
101    }