Class MockLoggerFactory

java.lang.Object
io.micrometer.core.util.internal.logging.InternalLoggerFactory
io.micrometer.core.util.internal.logging.MockLoggerFactory

public class MockLoggerFactory extends io.micrometer.core.util.internal.logging.InternalLoggerFactory
Simple InternalLoggerFactory implementation that always returns an instance of MockLogger so components that fetch the logger this way: InternalLogger logger = InternalLoggerFactory.getInstance(MyClass.class);, get a MockLogger instance if they are created using factory.injectLogger(MyClass::new) or if InternalLoggerFactory.setDefaultFactory(mockLoggerFactory); was set previously.
  • Constructor Details

    • MockLoggerFactory

      public MockLoggerFactory()
  • Method Details

    • getLogger

      public MockLogger getLogger(Class<?> clazz)
      A convenient way to create a logger. In contrast of InternalLoggerFactory.getInstance(Class), this method returns a MockLogger instead of an InternalLoggerFactory so that you don't need to cast.
      Parameters:
      clazz - The class that you need the logger for.
      Returns:
      An instance of MockLogger for the provided class.
    • getLogger

      public MockLogger getLogger(String name)
      A convenient way to create a logger. In contrast of InternalLoggerFactory.getInstance(String), this method returns a MockLogger instead of an InternalLoggerFactory so that you don't need to cast.
      Parameters:
      name - The name of the logger.
      Returns:
      An instance of MockLogger for the provided name.
    • injectLogger

      public <T> T injectLogger(Supplier<T> supplier)
      A factory method that returns the result of the given Supplier and also injects the default factory. So if the logger is created this way: InternalLogger logger = InternalLoggerFactory.getInstance(MyClass.class);, the resulted logger will be a MockLogger.

      NOTE: This doesn't work when a target logger is a static field and has been initialized already. Use a non-static field for a target logger to make this work consistently.

      Type Parameters:
      T - The type of an object that will be created by the Supplier.
      Parameters:
      supplier - The logic that creates the object you need to inject the logger into.
      Returns:
      The result of the Supplier.
    • newInstance

      protected io.micrometer.core.util.internal.logging.InternalLogger newInstance(String name)
      Specified by:
      newInstance in class io.micrometer.core.util.internal.logging.InternalLoggerFactory