added Slf4jConfigurationFactory to detect actual slf4j logging

implementation and load appropriate Slf4jConfiguration implementation
from META-INF/maven/slf4j-configuration.properties
This commit is contained in:
Hervé Boutemy 2012-12-18 23:44:18 +01:00
parent eb190f0295
commit 3511b09d1e
5 changed files with 92 additions and 8 deletions

View File

@ -40,9 +40,9 @@
import org.apache.maven.cli.event.DefaultEventSpyContext;
import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.cli.logging.Slf4jConfiguration;
import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.cli.logging.Slf4jStdoutLogger;
import org.apache.maven.cli.logging.impl.Slf4jSimpleConfiguration;
import org.apache.maven.cli.transfer.ConsoleMavenTransferListener;
import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
@ -133,8 +133,6 @@ public class MavenCli
private DefaultSecDispatcher dispatcher;
private Slf4jConfiguration slf4jConfiguration = new Slf4jSimpleConfiguration();
public MavenCli()
{
this( null );
@ -307,6 +305,9 @@ private void logging( CliRequest cliRequest )
cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET );
cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.ERRORS );
slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration( slf4jLoggerFactory );
if ( cliRequest.debug )
{
cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_DEBUG );
@ -346,7 +347,6 @@ else if ( cliRequest.quiet )
}
plexusLoggerManager = new Slf4jLoggerManager();
slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
slf4jLogger = slf4jLoggerFactory.getLogger( this.getClass().getName() );
}

View File

@ -29,10 +29,10 @@
*
* @author Hervé Boutemy
*/
public class AbstractSlf4jConfiguration
public class BaseSlf4jConfiguration
implements Slf4jConfiguration
{
private final Logger logger = LoggerFactory.getLogger( AbstractSlf4jConfiguration.class );
private final Logger logger = LoggerFactory.getLogger( BaseSlf4jConfiguration.class );
public void setRootLoggerLevel( Level level )
{

View File

@ -0,0 +1,81 @@
package org.apache.maven.cli.logging;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import org.codehaus.plexus.util.PropertyUtils;
import org.slf4j.ILoggerFactory;
/**
* Slf4jConfiguration factory, loading implementations from <code>META-INF/maven/slf4j-configuration.properties</code>
* configuration files in class loader.
*
* @author Hervé Boutemy
*/
public class Slf4jConfigurationFactory
{
public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
{
try
{
Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
String key = loggerFactory.getClass().getCanonicalName();
while ( resources.hasMoreElements() )
{
URL resource = resources.nextElement();
Properties conf = PropertyUtils.loadProperties( resource.openStream() );
String impl = conf.getProperty( key );
if ( impl != null )
{
return (Slf4jConfiguration) Class.forName( impl ).newInstance();
}
}
}
catch ( IOException e )
{
e.printStackTrace();
}
catch ( InstantiationException e )
{
e.printStackTrace();
}
catch ( IllegalAccessException e )
{
e.printStackTrace();
}
catch ( ClassNotFoundException e )
{
e.printStackTrace();
}
return new BaseSlf4jConfiguration();
}
}

View File

@ -21,7 +21,7 @@
import java.io.File;
import org.apache.maven.cli.logging.AbstractSlf4jConfiguration;
import org.apache.maven.cli.logging.BaseSlf4jConfiguration;
/**
* Configuration for slf4j-simple.
@ -29,7 +29,7 @@
* @author Hervé Boutemy
*/
public class Slf4jSimpleConfiguration
extends AbstractSlf4jConfiguration
extends BaseSlf4jConfiguration
{
@Override
public void setRootLoggerLevel( Level level )

View File

@ -0,0 +1,3 @@
# key = Slf4j effective logger factory implementation
# value = corresponding o.a.m.cli.logging.Slf4jConfiguration class
org.slf4j.impl.SimpleLoggerFactory org.apache.maven.cli.logging.impl.Slf4jSimpleConfiguration