Class LogbackLoggingService

All Implemented Interfaces:
Component, DestructableComponent, IdentifiableComponent, IdentifiedComponent, InitializableComponent, UnmodifiableComponent, LoggingService, ReloadableService<Object>, Aware, ApplicationContextAware

public class LogbackLoggingService extends AbstractReloadableService<Object> implements LoggingService, ApplicationContextAware
Simple LoggingService that watches for logback configuration file changes and reloads the file when a change occurs.
Since:
9.0.0
  • Field Details

    • loggerContext

      @NonnullAfterInit private ch.qos.logback.classic.LoggerContext loggerContext
      Logback logger context.
    • statusManager

      @NonnullAfterInit private ch.qos.logback.core.status.StatusManager statusManager
      Logger used to log messages without relying on the logging system to be full initialized.
    • fallbackConfiguration

      @NonnullAfterInit private Resource fallbackConfiguration
      URL to the fallback logback configuration found in the classpath.
    • configurationResource

      @NonnullAfterInit private Resource configurationResource
      Logging configuration resource.
    • homePropertyName

      @Nullable @NotEmpty private String homePropertyName
      Optional name of a property to populate into the LoggerContext.
    • applicationContext

      @Nullable private ApplicationContext applicationContext
      Spring application context.
  • Constructor Details

    • LogbackLoggingService

      public LogbackLoggingService()
  • Method Details

    • setLoggingConfiguration

      public void setLoggingConfiguration(@Nonnull Resource configuration)
      Sets the logging configuration.
      Specified by:
      setLoggingConfiguration in interface LoggingService
      Parameters:
      configuration - logging configuration
    • setFallbackConfiguration

      public void setFallbackConfiguration(@Nonnull Resource fallback)
      Set a resource to use as a fallback configuration if the primary is unavailable.
      Parameters:
      fallback - fallback configuration resouurce
    • setHomePropertyName

      public void setHomePropertyName(@Nullable @NotEmpty String name)
      Set name of a property to load into LoggerContext.
      Parameters:
      name - property name
    • setApplicationContext

      public void setApplicationContext(@Nonnull ApplicationContext context)
      Specified by:
      setApplicationContext in interface ApplicationContextAware
    • getServiceableComponent

      @Nonnull public ServiceableComponent<Object> getServiceableComponent()
      Get the serviceable component that this service supports. If the component hasn't been successfully loaded yet or if this service does not support a ServiceableComponent, a ServiceException is raised.

      On a non-null value, the returned component will be pinned and MUST be closed. This can be done by exploiting the fact that a ServiceableComponent implements AutoCloseable

      . This service does not support a ServiceableComponent, so return null.
      Specified by:
      getServiceableComponent in interface ReloadableService<Object>
      Returns:
      the component, if appropriate.
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      This method checks to ensure that the component ID is not null. Performs the initialization of the component. This method is executed within the lock on the object being initialized. The default implementation of this method is a no-op.
      Overrides:
      doInitialize in class AbstractReloadableService<Object>
      Throws:
      ComponentInitializationException - thrown if there is a problem initializing the component
    • shouldReload

      protected boolean shouldReload()
      Called by the AbstractReloadableService<T>.ServiceReloadTask to determine if the service should be reloaded.

      No lock is held when this method is called, so any locking needed should be handled internally.

      Specified by:
      shouldReload in class AbstractReloadableService<Object>
      Returns:
      true iff the service should be reloaded
    • doReload

      protected void doReload()
      Performs the actual reload.

      No lock is held when this method is called, so any locking needed should be handled internally.

      Overrides:
      doReload in class AbstractReloadableService<Object>
    • loadLoggingConfiguration

      protected void loadLoggingConfiguration()
      Reads and loads in a new logging configuration.
      Throws:
      ServiceException - thrown if there is a problem loading the logging configuration
    • loadLoggingConfiguration

      protected void loadLoggingConfiguration(@Nonnull InputStream loggingConfig)
      Loads a logging configuration in to the active logger context. Error messages are printed out to the status manager.
      Parameters:
      loggingConfig - logging configuration file
      Throws:
      ServiceException - thrown is there is a problem loading the logging configuration
    • loadHomeProperty

      protected void loadHomeProperty()
      Add the "home" property from the Spring application context to the logger context.

      This is principally to allow a defaulted value of this property to be referenced within the logging configuration itself as a location.