Coverage Report - org.owasp.dependencycheck.utils.LogUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LogUtils
0%
0/26
0%
0/14
3.333
 
 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  
      * The logger.
 37  
      */
 38  0
     private static final Logger LOGGER = Logger.getLogger(LogUtils.class.getName());
 39  
 
 40  
     /**
 41  
      * Private constructor for a utility class.
 42  
      */
 43  
     private LogUtils() {
 44  
     }
 45  
 
 46  
     /**
 47  
      * Configures the logger for use by the application.
 48  
      *
 49  
      * @param in the input stream to read the log settings from
 50  
      * @param verboseLogFile the file path for the verbose log
 51  
      */
 52  
     public static void prepareLogger(InputStream in, String verboseLogFile) {
 53  
         try {
 54  0
             LogManager.getLogManager().reset();
 55  0
             LogManager.getLogManager().readConfiguration(in);
 56  
 
 57  0
             if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
 58  0
                 verboseLoggingEnabled = true;
 59  0
                 final Logger logger = Logger.getLogger("");
 60  0
                 final FileHandler fileHandler = new FileHandler(verboseLogFile, true);
 61  0
                 fileHandler.setFormatter(new SimpleFormatter());
 62  0
                 fileHandler.setLevel(Level.FINE);
 63  0
                 fileHandler.setFilter(new LogFilter());
 64  
 
 65  0
                 logger.addHandler(fileHandler);
 66  0
                 logger.setLevel(Level.FINE);
 67  
             }
 68  0
         } catch (IOException ex) {
 69  0
             LOGGER.log(Level.WARNING, "IO Error preparing the logger", ex);
 70  0
         } catch (SecurityException ex) {
 71  0
             LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
 72  0
         } catch (Throwable ex) {
 73  0
             LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
 74  
         } finally {
 75  0
             if (in != null) {
 76  
                 try {
 77  0
                     in.close();
 78  0
                 } catch (Throwable ex) {
 79  0
                     LOGGER.log(Level.FINEST, "Error closing resource stream", ex);
 80  0
                 }
 81  
             }
 82  
         }
 83  0
     }
 84  
     /**
 85  
      * Whether or not verbose logging is enabled.
 86  
      */
 87  0
     private static boolean verboseLoggingEnabled = false;
 88  
 
 89  
     /**
 90  
      * Get the value of verboseLoggingEnabled.
 91  
      *
 92  
      * @return the value of verboseLoggingEnabled
 93  
      */
 94  
     public static boolean isVerboseLoggingEnabled() {
 95  0
         return verboseLoggingEnabled;
 96  
     }
 97  
 }