Merge branch 'spotless_2' into JAL-1551_2_11_3_spotlett
[jalview.git] / src / jalview / util / Log4j.java
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.util;
22
23 import java.util.Map;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.apache.logging.log4j.core.Appender;
29 import org.apache.logging.log4j.core.Filter;
30 import org.apache.logging.log4j.core.Layout;
31 import org.apache.logging.log4j.core.LoggerContext;
32 import org.apache.logging.log4j.core.appender.ConsoleAppender;
33 import org.apache.logging.log4j.core.config.Configuration;
34 import org.apache.logging.log4j.core.config.ConfigurationFactory;
35 import org.apache.logging.log4j.core.config.Configurator;
36 import org.apache.logging.log4j.core.config.LoggerConfig;
37 import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
38 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
39 import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder;
40 import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
41 import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
42 import org.apache.logging.log4j.core.filter.ThresholdFilter;
43 import org.apache.logging.log4j.core.layout.PatternLayout;
44
45 import jalview.log.JLogger;
46 import jalview.log.JalviewAppender;
47
48 public class Log4j
49 {
50
51   public final static String SIMPLE_PATTERN = "%level - %m%n";
52
53   private static boolean init = false;
54
55   public static boolean isInit()
56   {
57     return init;
58   }
59
60   public static Level log4jLevel(JLogger.LogLevel loglevel)
61   {
62     return Level.toLevel(loglevel.toString());
63   }
64
65   public static void init(JLogger.LogLevel myLevel)
66   {
67     init(log4jLevel(myLevel));
68   }
69
70   public static void init(Level myLevel)
71   {
72     if (init)
73       return;
74     try
75     {
76       // configure the root logger to stderr
77       ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j
78               .getConfigurationBuilder();
79
80       configBuilder.setStatusLevel(Level.WARN);
81
82       String consoleLoggerName = "STDERR";
83       AppenderComponentBuilder appenderBuilder = configBuilder
84               .newAppender(consoleLoggerName, "Console");
85       appenderBuilder.addAttribute("target",
86               ConsoleAppender.Target.SYSTEM_ERR);
87       appenderBuilder.add(Log4j.getSimpleLayoutBuilder());
88       appenderBuilder.add(Log4j.getThresholdFilterBuilder());
89       configBuilder.add(appenderBuilder);
90
91       configBuilder.add(configBuilder.newRootLogger(myLevel)
92               .add(configBuilder.newAppenderRef(consoleLoggerName)));
93
94       Configurator.initialize(configBuilder.build());
95
96       init = true;
97     } catch (Exception e)
98     {
99       System.err.println("Problems initializing the log4j system\n");
100       e.printStackTrace(System.err);
101     }
102   }
103
104   public static Logger getLogger(String name)
105   {
106     return getLogger(name, Level.INFO);
107   }
108
109   public static Logger getLogger(String name, JLogger.LogLevel loglevel)
110   {
111     return getLogger(name, log4jLevel(loglevel));
112   }
113
114   public static Logger getLogger(String name, Level level)
115   {
116     Logger logger = LogManager.getLogger(name);
117     Log4j.setLevel(logger, level);
118     return logger;
119   }
120
121   public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()
122   {
123     return ConfigurationFactory.newConfigurationBuilder();
124   }
125
126   public static Layout getSimpleLayout()
127   {
128     return PatternLayout.newBuilder().withPattern(SIMPLE_PATTERN).build();
129   }
130
131   public static LayoutComponentBuilder getSimpleLayoutBuilder()
132   {
133     return getConfigurationBuilder().newLayout("PatternLayout")
134             .addAttribute("pattern", Log4j.SIMPLE_PATTERN);
135   }
136
137   public static Filter getThresholdFilter(Level level)
138   {
139     return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT,
140             Filter.Result.NEUTRAL);
141   }
142
143   public static FilterComponentBuilder getThresholdFilterBuilder()
144   {
145     return getConfigurationBuilder().newFilter("ThresholdFilter",
146             Filter.Result.ACCEPT, Filter.Result.NEUTRAL);
147   }
148
149   public static void setLevel(Logger logger, JLogger.LogLevel loglevel)
150   {
151     setLevel(logger, log4jLevel(loglevel));
152   }
153
154   public static void setLevel(Logger logger, Level level)
155   {
156     if (!Platform.isJS())
157     {
158       LoggerContext context = (LoggerContext) LogManager.getContext(false);
159       Configuration config = context.getConfiguration();
160       LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
161       loggerConfig.setLevel(level);
162
163       Map<String, Appender> appenders = config.getAppenders();
164
165       Appender jappender = config.getAppender(JalviewAppender.NAME);
166
167       context.updateLoggers();
168     }
169   }
170
171   public static void setRootLevel(JLogger.LogLevel loglevel)
172   {
173     setRootLevel(log4jLevel(loglevel));
174   }
175
176   public static void setRootLevel(Level level)
177   {
178     setLevel(LogManager.getRootLogger(), level);
179   }
180
181   public static Appender getAppender(String name)
182   {
183     LoggerContext context = (LoggerContext) LogManager.getContext(false);
184     Configuration config = context.getConfiguration();
185     Map<String, Appender> appenders = config.getAppenders();
186     return appenders.get(name);
187   }
188
189   public static void addAppender(Logger logger, Logger logger2,
190           String name2)
191   {
192     LoggerContext context = (LoggerContext) LogManager.getContext(false);
193     Configuration config = context.getConfiguration();
194     LoggerConfig logger2Config = config.getLoggerConfig(logger2.getName());
195     Map<String, Appender> logger2AppendersMap = logger2Config
196             .getAppenders();
197     Appender appender = logger2AppendersMap.get(name2);
198     addAppender(logger, appender);
199     context.updateLoggers();
200   }
201
202   public static void addAppender(Logger logger, Appender appender)
203   {
204     if (appender == null)
205       return;
206     LoggerContext context = (LoggerContext) LogManager.getContext(false);
207     Configuration config = context.getConfiguration();
208     LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
209     if (loggerConfig == null)
210       return;
211
212     Level level = loggerConfig.getLevel();
213
214     config.addAppender(appender);
215     loggerConfig.addAppender(appender, null, null);
216
217     context.updateLoggers();
218   }
219
220   public static void addAppenderToRootLogger(Appender appender)
221   {
222     Log4j.addAppender(LogManager.getRootLogger(), appender);
223   }
224 }