View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.log;
18  
19  import java.util.Timer;
20  
21  import org.opensaml.util.resource.FilesystemResource;
22  import org.opensaml.util.resource.Resource;
23  import org.opensaml.util.resource.ResourceChangeWatcher;
24  import org.opensaml.util.resource.ResourceException;
25  import org.slf4j.LoggerFactory;
26  
27  import ch.qos.logback.classic.LoggerContext;
28  import ch.qos.logback.core.status.ErrorStatus;
29  import ch.qos.logback.core.status.StatusManager;
30  
31  /**
32   * Simple logging service that watches for logback configuration file changes and reloads the file when a change occurs.
33   */
34  public class LogbackLoggingService {
35  
36      /**
37       * Constructor.
38       *
39       * @param taskTimer resource watchdog task timer
40       * @param loggingConfigurationFile logback configuration file
41       * @param pollingFrequency frequency the configuration file should be checked for changes
42       */
43      public LogbackLoggingService(Timer taskTimer, String loggingConfigurationFile, long pollingFrequency) {
44          LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
45          StatusManager statusManager = loggerContext.getStatusManager();
46          
47          try{
48              Resource configResource = new FilesystemResource(loggingConfigurationFile);
49              LogbackConfigurationChangeListener configChangeListener = new LogbackConfigurationChangeListener();
50              configChangeListener.onResourceCreate(configResource);
51              
52              ResourceChangeWatcher resourceWatcher = new ResourceChangeWatcher(configResource, pollingFrequency, 5);
53              resourceWatcher.getResourceListeners().add(configChangeListener);
54              
55              taskTimer.schedule(resourceWatcher, 0, pollingFrequency);
56          }catch(ResourceException e){
57              statusManager.add(new ErrorStatus("Error loading logging configuration file: "
58                      + loggingConfigurationFile, this, e));
59          }
60      }
61  }