View Javadoc

1   package ch.qos.logback.classic.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.net.MalformedURLException;
6   import java.net.URL;
7   import java.util.List;
8   
9   import ch.qos.logback.classic.BasicConfigurator;
10  import ch.qos.logback.classic.LoggerContext;
11  import ch.qos.logback.classic.joran.JoranConfigurator;
12  import ch.qos.logback.core.joran.spi.JoranException;
13  import ch.qos.logback.core.status.ErrorStatus;
14  import ch.qos.logback.core.status.InfoStatus;
15  import ch.qos.logback.core.status.StatusManager;
16  import ch.qos.logback.core.status.WarnStatus;
17  import ch.qos.logback.core.util.Loader;
18  import ch.qos.logback.core.util.OptionHelper;
19  
20  // contributors
21  // Ted Graham, Matt Fowles, see also http://jira.qos.ch/browse/LBCORE-32
22  /**
23   * This class contains logback's logic for automatic configuration
24   * 
25   * @author Ceki Gulcu
26   */
27  public class ContextInitializer {
28  
29    final public static String AUTOCONFIG_FILE = "logback.xml";
30    final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
31    final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";
32    final public static String STATUS_LISTENER_CLASS = "logback.statusListenerClass";
33    final public static String SYSOUT = "SYSOUT";
34  
35    final LoggerContext loggerContext;
36  
37    public ContextInitializer(LoggerContext loggerContext) {
38      this.loggerContext = loggerContext;
39    }
40  
41    public void configureByResource(URL url) throws JoranException {
42      if (url == null) {
43        throw new IllegalArgumentException("URL argument cannot be null");
44      }
45      JoranConfigurator configurator = new JoranConfigurator();
46      configurator.setContext(loggerContext);
47      configurator.doConfigure(url);
48    }
49  
50    private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) {
51      String logbackConfigFile = OptionHelper.getSystemProperty(CONFIG_FILE_PROPERTY);
52      
53      if (logbackConfigFile != null) {
54        URL result = null;
55        try {
56          result = new URL(logbackConfigFile);
57          return result;
58        } catch (MalformedURLException e) {
59          // so, resource is not a URL:
60          // attempt to get the resource from the class path
61          result = Loader.getResource(logbackConfigFile, classLoader);
62          if (result != null) {
63            return result;
64          }
65          File f = new File(logbackConfigFile);
66          if (f.exists() && f.isFile()) {
67            try {
68              result = f.toURI().toURL();
69              return result;
70            } catch (MalformedURLException e1) {
71            }
72          }
73        } finally {
74          if (updateStatus) {
75            statusOnResourceSearch(logbackConfigFile, classLoader, result);
76          }
77        }
78      }
79      return null;
80    }
81  
82    public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
83      ClassLoader myClassLoader = this.getClass().getClassLoader();
84      URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
85      if (url != null) {
86        return url;
87      }
88  
89      url = Loader.getResource(TEST_AUTOCONFIG_FILE, myClassLoader);
90      if (updateStatus) {
91        statusOnResourceSearch(TEST_AUTOCONFIG_FILE, myClassLoader, url);
92      }
93      if (url != null) {
94        return url;
95      }
96  
97      url = Loader.getResource(AUTOCONFIG_FILE, myClassLoader);
98      if (updateStatus) {
99        statusOnResourceSearch(AUTOCONFIG_FILE, myClassLoader, url);
100     }
101     return url;
102   }
103 
104   public void autoConfig() throws JoranException {
105     StatusListenerConfigHelper.installIfAsked(loggerContext);
106     URL url = findURLOfDefaultConfigurationFile(true);
107     if (url != null) {
108       configureByResource(url);
109     } else {
110       BasicConfigurator.configure(loggerContext);
111     }
112   }
113 
114   private void multiplicityWarning(String resourceName, ClassLoader classLoader ) {
115     List<URL> urlList = null;
116     StatusManager sm = loggerContext.getStatusManager();
117     try {
118       urlList = Loader.getResourceOccurenceCount(resourceName, classLoader);
119     } catch (IOException e) {
120       sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]",
121           loggerContext, e));
122     }
123     if(urlList != null && urlList.size() > 1) {
124       sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.",
125           loggerContext));
126       for(URL url: urlList) {
127       sm.add(new WarnStatus("Resource ["+resourceName+"] occurs at ["+url.toString()+"]",
128           loggerContext));
129       }
130     }
131   }
132   
133   private void statusOnResourceSearch(String resourceName, ClassLoader classLoader, URL url) {
134     StatusManager sm = loggerContext.getStatusManager();
135     if (url == null) {
136       sm.add(new InfoStatus("Could NOT find resource [" + resourceName + "]",
137           loggerContext));
138     } else {
139       sm.add(new InfoStatus("Found resource [" + resourceName + "] at ["+url.toString()+"]",
140           loggerContext));
141       multiplicityWarning(resourceName, classLoader);
142     }
143   }
144 
145 }