使用带有Guice依赖项注入的JClouds创建自定义记录器

时间:2015-02-25 15:17:54

标签: java guice jclouds

我只是陷入困境,需要一些指针。我已经遵循了创建自己的日志记录实现,日志工厂和guice模块的说明;但是我不确定我错过了什么......

我的自定义记录器已实现w / Factory。这延伸自jClouds BaseLogger;未显示的是实际记录功能。还创建了Factory,它基于Log4JLogger Factory的实现。在这里我需要一个静态getLogger并使用slf4j的LoggerFactory并将其转换为jClouds接口类型Logger。我添加了' @ Inject'注释,但我觉得这应该是jClouds方面,而不是我的。 (不确定)。我错过了什么?

import org.jclouds.logging.BaseLogger;
import org.jclouds.logging.Logger;
import com.google.inject.Inject;

public class NullLoggerExt extends BaseLogger {
  private final org.jclouds.logging.Logger logger;

  public static class NullLoggerExtFactory implements LoggerFactory {
    public Logger getLogger(String category) {
      return new NullLoggerExt( (Logger) org.slf4j.LoggerFactory.getLogger(category) );
    }
  }

  @Inject   <--- is this what I need?
  public NullLoggerExt(org.jclouds.logging.Logger logger) {
    this.logger = logger;
  }
  .
  . (implemented abstract methods)
  .
}

我的自定义记录器模块

import NullLoggerExt;

import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.logging.config.LoggingModule;

public class NullLoggerModuleExt extends LoggingModule {

  @Override
  public LoggerFactory createLoggerFactory() {
    return new NullLoggerExt.NullLoggerExtFactory();
  }
}

我的Guice模块

import NullLoggerExt;
import com.google.inject.AbstractModule;
import org.jclouds.logging.Logger;

public class BindLoggerModule extends AbstractModule {

  @Override
  protected void configure() {
    bind(Logger.class).to(NullLoggerExt.class);
  }
}

本节将介绍我使用它的位置。

public class CloudHandler
{
  //Actual logger
  private static final Iterable<Module> detailedTrace = 
            ImmutableSet.<Module> of( new SLF4JLoggingModule() );

  Injector injector = Guice.createInjector(new BindLoggerModule());
  NullLoggerModuleExt customLog = injector.getInstance(NullLoggerModuleExt.class);

  //My performance logger
  private final Iterable<Module> baseLog =  
            ImmutableSet.<Module> of( customLog );

  public int connect( ... ) {
    contextBuilder...

    //Problem area
    if (debug mode == true)
      contextBuilder.modules( detailedTrace );
    else
      contextBuilder.modules( baseLog );

    //Throws CreationException
    ctx = contextBuilder.buildView( BlobStoreContext.class );
    return Success or Failure;
  }
}

1 个答案:

答案 0 :(得分:2)

这种方法是错误的,因为你试图注入一个绑定到同一个类的Logger实现,所以你将在那里得到一个递归循环。

如果您只是想修改SLF4j日志记录驱动程序的工作方式,则可以扩展其类。但是,如果您尝试使用空记录器“装饰”任何jclouds记录器,然后在需要时委派给日志记录驱动程序,则可以使用以下日志记录类和模块来寻求更通用的方法:

static class DelegatingNullLogger extends BaseLogger {
    private final Logger logger;

    public static class DelegatingNullLoggerFactory implements LoggerFactory {
        private final LoggerFactory delegate;

        public DelegatingNullLoggerFactory(LoggerFactory delegate) {
            this.delegate = checkNotNull(delegate, "delegate");
        }

        @Override
        public Logger getLogger(String category) {
            return new DelegatingNullLogger(delegate.getLogger(category));
        }
    }

    public DelegatingNullLogger(Logger logger) {
        this.logger = logger;
    }

    // Implement methods
}

static class NullDelegatingLoggingModule extends LoggingModule {
    private final LoggerFactory loggerFactory;

    public NullDelegatingLoggingModule(LoggerFactory loggerFactory) {
        this.loggerFactory = checkNotNull(loggerFactory, "loggerFactory");
    }

    @Override
    public LoggerFactory createLoggerFactory() {
        return new DelegatingNullLogger.DelegatingNullLoggerFactory(loggerFactory);
    }
}

此代码将首先创建一个记录器,该记录器将委托给可以是Log4j,SLF4j或其他任何日志消息的底层记录器,并且提供的模块配置将传递的委托记录器。

您可以按如下方式使用此方法,例如将空记录器委托给SLF4j日志记录驱动程序:

LoggerFactory loggerFactory = new SLF4JLogger.SLF4JLoggerFactory();
LoggingModule loggingModule = new NullDelegatingLoggingModule(loggerFactory);

BlobStoreContext ctx = ContextBuilder.newBuilder("provider")
        .modules(ImmutableSet.of(loggingModule))
        (...)
        .buildView(BlobStoreContext.class);