Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1245

Impossible to override logback.groovy with test configuration.

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.1.8
    • Fix Version/s: 1.2.0
    • Component/s: logback-classic
    • Labels:
      None

      Description

      It is currently impossible to override an existing logback.groovy configuration file with either logback-test.groovy (currently not considered at all) or logback-test-xml.

      The reason for this is located in the method ContextInitializer.findURLOfDefaultConfigurationFile which prefers Groovy over XML in general:

      public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
              ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this);
              URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
      }
      

      I'd suggest to change it like this:

      public static final String GROOVY_TEST_AUTOCONFIG_FILE = "logback-test.groovy";
      
      public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
              ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this);
              URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(GROOVY_TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
      }
      

      or at least into this if overriding configuration via logback-test.groovy is intentionally left out for reasons unknown to me:

      public URL findURLOfDefaultConfigurationFile(boolean updateStatus) {
              ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this);
              URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
              if (url != null) {
                  return url;
              }
      
              return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
      }
      

      That way a -test Logback configuration would always be preferred over one without -test.

        Attachments

          Activity

            People

            • Assignee:
              ceki Ceki Gülcü
              Reporter:
              jhuxhorn Joern Huxhorn
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: