Coverage Report - org.owasp.dependencycheck.utils.LogUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LogUtils
8%
2/23
0%
0/12
3
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 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  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.util.logging.FileHandler;
 23  
 import java.util.logging.Level;
 24  
 import java.util.logging.LogManager;
 25  
 import java.util.logging.Logger;
 26  
 import java.util.logging.SimpleFormatter;
 27  
 
 28  
 /**
 29  
  * A utility class to aide in the setup of the logging mechanism.
 30  
  *
 31  
  * @author Jeremy Long <jeremy.long@owasp.org>
 32  
  */
 33  
 public final class LogUtils {
 34  
 
 35  
     /**
 36  
      * Private constructor for a utility class.
 37  
      */
 38  
     private LogUtils() {
 39  
     }
 40  
 
 41  
     /**
 42  
      * Configures the logger for use by the application.
 43  
      *
 44  
      * @param in the input stream to read the log settings from
 45  
      * @param verboseLogFile the file path for the verbose log
 46  
      */
 47  
     public static void prepareLogger(InputStream in, String verboseLogFile) {
 48  
         try {
 49  0
             LogManager.getLogManager().reset();
 50  0
             LogManager.getLogManager().readConfiguration(in);
 51  0
             if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
 52  0
                 verboseLoggingEnabled = true;
 53  0
                 final Logger logger = Logger.getLogger("");
 54  0
                 final FileHandler handler = new FileHandler(verboseLogFile, true);
 55  0
                 handler.setFormatter(new SimpleFormatter());
 56  0
                 handler.setLevel(Level.FINE);
 57  0
                 handler.setFilter(new LogFilter());
 58  0
                 logger.addHandler(handler);
 59  0
                 logger.setLevel(Level.FINE);
 60  
             }
 61  0
         } catch (IOException ex) {
 62  0
             Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "IO Error preparing the logger", ex);
 63  0
         } catch (SecurityException ex) {
 64  0
             Logger.getLogger(LogUtils.class.getName()).log(Level.FINE, "Error preparing the logger", ex);
 65  
         } finally {
 66  0
             if (in != null) {
 67  
                 try {
 68  0
                     in.close();
 69  0
                 } catch (Throwable ex) {
 70  0
                     Logger.getLogger(LogUtils.class.getName()).log(Level.FINEST, "Error closing resource stream", ex);
 71  0
                 }
 72  
             }
 73  
         }
 74  0
     }
 75  
     /**
 76  
      * Whether or not verbose logging is enabled.
 77  
      */
 78  1
     private static boolean verboseLoggingEnabled = false;
 79  
 
 80  
     /**
 81  
      * Get the value of verboseLoggingEnabled.
 82  
      *
 83  
      * @return the value of verboseLoggingEnabled
 84  
      */
 85  
     public static boolean isVerboseLoggingEnabled() {
 86  5
         return verboseLoggingEnabled;
 87  
     }
 88  
 }