View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.xml;
19  
20  import org.apache.log4j.config.Log4j1Configuration;
21  import org.apache.logging.log4j.core.LoggerContext;
22  import org.apache.logging.log4j.core.config.Configuration;
23  import org.apache.logging.log4j.core.config.ConfigurationFactory;
24  import org.apache.logging.log4j.core.config.ConfigurationSource;
25  import org.apache.logging.log4j.core.config.Order;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.status.StatusLogger;
28  import org.apache.logging.log4j.util.PropertiesUtil;
29  
30  /**
31   * Constructs a Configuration usable in Log4j 2 from a Log4j 1 configuration file.
32   */
33  @Plugin(name = "Log4j1XmlConfigurationFactory", category = ConfigurationFactory.CATEGORY)
34  @Order(2)
35  public class XmlConfigurationFactory extends ConfigurationFactory {
36  
37      public static final String FILE_EXTENSION = ".xml";
38  
39      private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
40  
41      /**
42       * File name prefix for test configurations.
43       */
44      protected static final String TEST_PREFIX = "log4j-test";
45  
46      /**
47       * File name prefix for standard configurations.
48       */
49      protected static final String DEFAULT_PREFIX = "log4j";
50  
51      @Override
52      protected String[] getSupportedTypes() {
53          if (!PropertiesUtil.getProperties().getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
54              return null;
55          }
56          return new String[] { FILE_EXTENSION };
57      }
58  
59      @Override
60      public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
61          int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
62          return new XmlConfiguration(loggerContext, source, interval);
63      }
64  
65      @Override
66      protected String getTestPrefix() {
67          return TEST_PREFIX;
68      }
69  
70      @Override
71      protected String getDefaultPrefix() {
72          return DEFAULT_PREFIX;
73      }
74  
75      @Override
76      protected String getVersion() {
77          return LOG4J1_VERSION;
78      }
79  }