2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
45 import jalview.log.JLogger;
46 import jalview.log.JalviewAppender;
51 public final static String SIMPLE_PATTERN = "%level - %m%n";
53 private static boolean init = false;
55 public static boolean isInit()
60 public static Level log4jLevel(JLogger.LogLevel loglevel)
62 return Level.toLevel(loglevel.toString());
65 public static void init(JLogger.LogLevel myLevel)
67 init(log4jLevel(myLevel));
70 public static void init(Level myLevel)
76 // configure the root logger to stderr
77 ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j
78 .getConfigurationBuilder();
80 configBuilder.setStatusLevel(Level.WARN);
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);
91 configBuilder.add(configBuilder.newRootLogger(myLevel)
92 .add(configBuilder.newAppenderRef(consoleLoggerName)));
94 Configurator.initialize(configBuilder.build());
99 System.err.println("Problems initializing the log4j system\n");
100 e.printStackTrace(System.err);
104 public static Logger getLogger(String name)
106 return getLogger(name, Level.INFO);
109 public static Logger getLogger(String name, JLogger.LogLevel loglevel)
111 return getLogger(name, log4jLevel(loglevel));
114 public static Logger getLogger(String name, Level level)
116 Logger logger = LogManager.getLogger(name);
117 Log4j.setLevel(logger, level);
121 public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()
123 return ConfigurationFactory.newConfigurationBuilder();
126 public static Layout getSimpleLayout()
128 return PatternLayout.newBuilder().withPattern(SIMPLE_PATTERN).build();
131 public static LayoutComponentBuilder getSimpleLayoutBuilder()
133 return getConfigurationBuilder().newLayout("PatternLayout")
134 .addAttribute("pattern", Log4j.SIMPLE_PATTERN);
137 public static Filter getThresholdFilter(Level level)
139 return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT,
140 Filter.Result.NEUTRAL);
143 public static FilterComponentBuilder getThresholdFilterBuilder()
145 return getConfigurationBuilder().newFilter("ThresholdFilter",
146 Filter.Result.ACCEPT, Filter.Result.NEUTRAL);
149 public static void setLevel(Logger logger, JLogger.LogLevel loglevel)
151 setLevel(logger, log4jLevel(loglevel));
154 public static void setLevel(Logger logger, Level level)
156 if (!Platform.isJS())
158 LoggerContext context = (LoggerContext) LogManager.getContext(false);
159 Configuration config = context.getConfiguration();
160 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
161 loggerConfig.setLevel(level);
163 Map<String, Appender> appenders = config.getAppenders();
165 Appender jappender = config.getAppender(JalviewAppender.NAME);
167 context.updateLoggers();
171 public static void setRootLevel(JLogger.LogLevel loglevel)
173 setRootLevel(log4jLevel(loglevel));
176 public static void setRootLevel(Level level)
178 setLevel(LogManager.getRootLogger(), level);
181 public static Appender getAppender(String name)
183 LoggerContext context = (LoggerContext) LogManager.getContext(false);
184 Configuration config = context.getConfiguration();
185 Map<String, Appender> appenders = config.getAppenders();
186 return appenders.get(name);
189 public static void addAppender(Logger logger, Logger logger2,
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
197 Appender appender = logger2AppendersMap.get(name2);
198 addAppender(logger, appender);
199 context.updateLoggers();
202 public static void addAppender(Logger logger, Appender appender)
204 if (appender == null)
206 LoggerContext context = (LoggerContext) LogManager.getContext(false);
207 Configuration config = context.getConfiguration();
208 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
209 if (loggerConfig == null)
212 Level level = loggerConfig.getLevel();
214 config.addAppender(appender);
215 loggerConfig.addAppender(appender, null, null);
217 context.updateLoggers();
220 public static void addAppenderToRootLogger(Appender appender)
222 Log4j.addAppender(LogManager.getRootLogger(), appender);