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

Loading a logback config with different contextName throws IllegalStateException

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 1.0.9
    • logback-classic
    • None

    Description

      With a default logback.xml containing this

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
      <contextName>Foo</contextName>
      [..]
      </configuration>

      reloading the Logback configuration by means of

      private static void initLogbackConfig(URL configUrl) {
      ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
      if(loggerFactory instanceof LoggerContext) {
      LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
      // reset previous configuration initially loaded from logback.xml
      loggerContext.reset();
      JoranConfigurator configurator = new JoranConfigurator();
      configurator.setContext(loggerContext);
      try

      { configurator.doConfigure(configUrl); StatusPrinter.print(loggerContext); }

      catch(JoranException ex)

      { StatusPrinter.print(loggerContext); }

      }
      }

      with a file containing

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
      <contextName>Foobar</contextName>
      [..]
      </configuration>

      produces the following error:

      12:21:28,919 |-ERROR in ch.qos.logback.classic.joran.action.ContextNameAction - Failed to rename context [Foo] as [Foobar] java.lang.IllegalStateException: Context has been already given a name
      at java.lang.IllegalStateException: Context has been already given a name
      at at ch.qos.logback.core.ContextBase.setName(ContextBase.java:128)
      at at ch.qos.logback.classic.LoggerContext.setName(LoggerContext.java:107)
      at at ch.qos.logback.classic.joran.action.ContextNameAction.body(ContextNameAction.java:31)
      at at ch.qos.logback.core.joran.spi.Interpreter.callBodyAction(Interpreter.java:296)
      at at ch.qos.logback.core.joran.spi.Interpreter.characters(Interpreter.java:176)
      at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:57)
      at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:157)
      at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:143)
      at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:106)
      at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:56)

      Is this expected behavior? After all, loggerContext.reset() has been called and I'd assume that this would also reset the contextName. Feels like a bug.

      Attachments

        Activity

          People

            logback-dev Logback dev list
            jhuxhorn Joern Huxhorn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: