/** * Constructor. * * @param taskTimer resource watchdog task timer * @param loggingConfigurationFile logback configuration file * @param pollingFrequency frequency the configuration file should be checked for changes */ public LogbackLoggingService(Timer taskTimer, String loggingConfigurationFile, long pollingFrequency) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); StatusManager statusManager = loggerContext.getStatusManager(); try{ Resource configResource = new FilesystemResource(loggingConfigurationFile); LogbackConfigurationChangeListener configChangeListener = new LogbackConfigurationChangeListener(); configChangeListener.onResourceCreate(configResource); ResourceChangeWatcher resourceWatcher = new ResourceChangeWatcher(configResource, pollingFrequency, 5); resourceWatcher.getResourceListeners().add(configChangeListener); taskTimer.schedule(resourceWatcher, 0, pollingFrequency); }catch(ResourceException e){ statusManager.add(new ErrorStatus("Error loading logging configuration file: " + loggingConfigurationFile, this, e)); } } }
/** {@inheritDoc} */ public void initialize() throws ServiceException { if (isDestroyed()) { throw new SecurityException(getId() + " service has been destroyed, it may not be initialized."); } if (isInitialized()) { return; } try { log.debug("Initializing {} service with resources: {}", getId(), getServiceConfigurations()); if (resourcePollingFrequency > 0) { ResourceChangeWatcher changeWatcher; ResourceChangeListener changeListener = new ConfigurationResourceListener(); for (Resource configurationResournce : getServiceConfigurations()) { changeWatcher = new ResourceChangeWatcher(configurationResournce, resourcePollingFrequency, resourcePollingRetryAttempts); changeWatcher.getResourceListeners().add(changeListener); pollingTimer.schedule(changeWatcher, resourcePollingFrequency, resourcePollingFrequency); } } loadContext(); } catch (ResourceException e) { throw new ServiceException("Unable to initialize service: " + getId(), e); } }