简体   繁体   English

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

[英]Creating a custom logger using JClouds with Guice dependency injection

I am just stuck and need some pointers. 我只是被困住了,需要一些指导。 I have followed instruction on creating my own logging implementation, logging factory, and guice module; 我遵循了有关创建自己的日志记录实现,日志记录工厂和guice模块的说明; but just aren't sure what I am missing... 但不确定我想念的是什么...

My custom logger implemented w/ Factory. 我的自定义记录器通过Factory实现。 This extends from jClouds BaseLogger ; 这从jClouds BaseLogger扩展; not shown is the actually logging functionality. 未显示的是实际的日志记录功能。 Also created is the Factory, which I based off of their implementation of Log4JLogger Factory . 还创建了Factory,它基于Log4JLogger Factory的实现。 In this I needed a static getLogger and used the slf4j's LoggerFactory and casted it to jClouds interface type Logger. 在此,我需要一个静态的getLogger并使用slf4j的LoggerFactory并将其强制转换为jClouds接口类型Logger。 I added the '@Inject' annotation but I feel like this is what should be on the jClouds side, not mine. 我添加了“ @Inject”注释,但我觉得这应该是jClouds方面的,而不是我的。 (Not sure). (不确定)。 What am I missing though?? 我到底想念什么?

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)
  .
}

My custom logger Module 我的自定义记录器模块

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

My Guice module 我的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);
  }
}

This section will describe where I am using it. 本节将描述我在哪里使用它。

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

This approach is wrong, because you are trying to inject a Logger implementation that is bound to that exact same class, so you will get a recursive loop there. 这种方法是错误的,因为您尝试注入绑定到该完全相同的类的Logger实现,因此您将在此处获得递归循环。

If you are just trying to modify how the SLF4j logging driver works, you could just extend its classes. 如果您只是想修改SLF4j日志记录驱动程序的工作方式,则可以扩展其类。 However, if you are trying to "decorate" any jclouds logger with the null logger, and then delegate to the logging driver when needed, you could go for a more generic approach by using the following logging classes and module: 但是,如果您尝试用空记录器“装饰”任何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);
    }
}

This code will first create a logger that will delegate to an underlying logger that can be Log4j, SLF4j or whatever when it comes to log messages, and the provided module, configures the delegate logger that will be passed. 此代码将首先创建一个记录器,该记录器将委派给基础记录器,该记录器可以是Log4j,SLF4j或用于记录消息的任何内容,并且提供的模块配置将要传递的委托记录器。

You can use this as follows, for example to have the null logger delegate to the SLF4j logging driver: 您可以按以下方式使用它,例如,使空记录器委托给SLF4j记录驱动程序:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM