使用 JClouds 和 Guice 依赖注入创建自定义记录器

Creating a custom logger using JClouds with Guice dependency injection

我只是卡住了,需要一些指导。我已按照有关创建自己的日志记录实现、日志记录工厂和 guice 模块的说明进行操作;但不确定我错过了什么...

我的自定义记录器通过 Factory 实现。这扩展自 jClouds BaseLogger; not shown is the actually logging functionality. Also created is the Factory, which I based off of their implementation of 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;
  }
}

这种方法是错误的,因为您试图注入一个绑定到完全相同 class 的 Logger 实现,所以您会在那里得到一个递归循环。

如果您只是想修改 SLF4j 日志记录驱动程序的工作方式,您可以扩展其 classes。但是,如果您尝试 "decorate" 任何带有空记录器的 jclouds 记录器,然后在需要时委托给记录驱动程序,您可以通过使用以下日志记录 classes 和模块:

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);