View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    * <p>
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    * <p>
9    * or (per the licensee's choosing)
10   * <p>
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.util;
15  
16  import java.net.URL;
17  import java.util.Comparator;
18  import java.util.List;
19  
20  import ch.qos.logback.classic.BasicConfigurator;
21  import ch.qos.logback.classic.ClassicConstants;
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.joran.JoranConfigurator;
24  import ch.qos.logback.classic.spi.Configurator;
25  import ch.qos.logback.core.CoreConstants;
26  import ch.qos.logback.core.LogbackException;
27  import ch.qos.logback.core.joran.spi.JoranException;
28  import ch.qos.logback.core.status.InfoStatus;
29  import ch.qos.logback.core.util.EnvUtil;
30  import ch.qos.logback.core.util.StatusListenerConfigHelper;
31  
32  // contributors
33  // Ted Graham, Matt Fowles, see also http://jira.qos.ch/browse/LBCORE-32
34  
35  /**
36   * This class contains logback's logic for automatic configuration
37   *
38   * @author Ceki Gulcu
39   */
40  public class ContextInitializer {
41  
42      final public static String AUTOCONFIG_FILE = DefaultJoranConfigurator.AUTOCONFIG_FILE;
43      final public static String TEST_AUTOCONFIG_FILE = DefaultJoranConfigurator.TEST_AUTOCONFIG_FILE;
44      /**
45       * @deprecated Please use ClassicConstants.CONFIG_FILE_PROPERTY instead
46       */
47      final public static String CONFIG_FILE_PROPERTY = ClassicConstants.CONFIG_FILE_PROPERTY;
48  
49      final LoggerContext loggerContext;
50  
51      public ContextInitializer(LoggerContext loggerContext) {
52          this.loggerContext = loggerContext;
53      }
54  
55      public void configureByResource(URL url) throws JoranException {
56          if (url == null) {
57              throw new IllegalArgumentException("URL argument cannot be null");
58          }
59          final String urlString = url.toString();
60          if (urlString.endsWith("xml")) {
61              JoranConfigurator configurator = new JoranConfigurator();
62              configurator.setContext(loggerContext);
63              configurator.doConfigure(url);
64          } else {
65              throw new LogbackException(
66                      "Unexpected filename extension of file [" + url + "]. Should be .xml");
67          }
68      }
69  
70      void joranConfigureByResource(URL url) throws JoranException {
71          JoranConfigurator configurator = new JoranConfigurator();
72          configurator.setContext(loggerContext);
73          configurator.doConfigure(url);
74      }
75  
76      public void autoConfig() throws JoranException {
77          autoConfig(Configurator.class.getClassLoader());
78      }
79  
80      public void autoConfig(ClassLoader classLoader) throws JoranException {
81          String versionStr = EnvUtil.logbackVersion();
82          loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
83          StatusListenerConfigHelper.installIfAsked(loggerContext);
84          List<Configurator> configuratorList = ClassicEnvUtil.loadFromServiceLoader(Configurator.class, classLoader);
85          sortByPriority(configuratorList);
86  
87          for (Configurator c : configuratorList) {
88              try {
89                  c.setContext(loggerContext);
90                  Configurator.ExecutionStatus status = c.configure(loggerContext);
91                  if (status == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) {
92                      return;
93                  }
94              } catch (Exception e) {
95                  throw new LogbackException(
96                          String.format("Failed to initialize Configurator: %s using ServiceLoader",
97                                  c != null ? c.getClass().getCanonicalName() : "null"),
98                          e);
99              }
100 
101         }
102 
103         // at this stage invoke basicConfigurator
104         BasicConfigurator basicConfigurator = new BasicConfigurator();
105         basicConfigurator.setContext(loggerContext);
106         basicConfigurator.configure(loggerContext);
107 
108     }
109 
110     private void sortByPriority(List<Configurator> configuratorList) {
111         configuratorList.sort(new Comparator<Configurator>() {
112             @Override
113             public int compare(Configurator o1, Configurator o2) {
114                 if (o1.getClass() == o2.getClass())
115                     return 0;
116                 if (o1 instanceof DefaultJoranConfigurator) {
117                     return 1;
118                 }
119 
120                 // otherwise do not intervene
121                 return 0;
122             }
123         });
124     }
125 
126 
127 }