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

Documentation is missing explanation of GafferConfigurator

    XMLWordPrintable

Details

    Description

      Logback Manual - Chapter 3: Logback configuration explains configuration using ch.qos.logback.classic.joran.JoranConfigurator but there is no mention about how to use ch.qos.logback.classic.gaffer.GafferConfigurator.
      Logback Manual - Chapter 12: Groovy Configuration doesn't mention it either.

      I'd expect something along the code snippet below:

      import ch.qos.logback.classic.LoggerContext;
      import ch.qos.logback.classic.gaffer.GafferConfigurator;
      import ch.qos.logback.classic.joran.JoranConfigurator;
      import ch.qos.logback.core.joran.spi.JoranException;
      import ch.qos.logback.core.status.ErrorStatus;
      import ch.qos.logback.core.status.Status;
      import ch.qos.logback.core.status.StatusManager;
      import ch.qos.logback.core.status.StatusUtil;
      
      public static void configureLogging(URL configUrl)
      {
      	final String GROOVY_EXTENSION = ".groovy";
      	ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
      	if(loggerFactory instanceof LoggerContext)
      	{
      		LoggerContext loggerContext = (LoggerContext) loggerFactory;
      	
      		StatusManager sm = loggerContext.getStatusManager();
      		sm.clear();
      	
      		// reset previous configuration initially loaded from logback.groovy
      		loggerContext.reset();
      	
      		if(configUrl.toString().endsWith(GROOVY_EXTENSION))
      		{
      			GafferConfigurator configurator = new GafferConfigurator(loggerContext);
      			try
      			{
      				configurator.run(configUrl);
      			}
      			catch (RuntimeException ex)
      			{
      				sm.add(new ErrorStatus("Exception while configuring Logback!", configUrl, ex));
      			}
      		}
      		else
      		{
      			JoranConfigurator configurator = new JoranConfigurator();
      			configurator.setContext(loggerContext);
      			try
      			{
      				configurator.doConfigure(configUrl);
      			}
      			catch (JoranException ex)
      			{
      				sm.add(new ErrorStatus("Exception while configuring Logback!", configUrl, ex));
      			}
      		}
      	}
      }
      

      Another question is whether it would make sense to put this code into a general Configurator that's able to handle both cases.

      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: