Coverage Report - org.owasp.dependencycheck.utils.LogUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LogUtils
0%
0/33
0%
0/12
4.667
 
 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.File;
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.util.logging.FileHandler;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.LogManager;
 26  
 import java.util.logging.Logger;
 27  
 import java.util.logging.SimpleFormatter;
 28  
 
 29  
 /**
 30  
  * A utility class to aide in the setup of the logging mechanism.
 31  
  *
 32  
  * @author Jeremy Long
 33  
  */
 34  
 public final class LogUtils {
 35  
 
 36  
     /**
 37  
      * The logger.
 38  
      */
 39  0
     private static final Logger LOGGER = Logger.getLogger(LogUtils.class.getName());
 40  
 
 41  
     /**
 42  
      * Private constructor for a utility class.
 43  
      */
 44  0
     private LogUtils() {
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Configures the logger for use by the application.
 49  
      *
 50  
      * @param in the input stream to read the log settings from
 51  
      * @param verboseLogFile the file path for the verbose log
 52  
      */
 53  
     public static void prepareLogger(InputStream in, String verboseLogFile) {
 54  
         try {
 55  0
             LogManager.getLogManager().reset();
 56  0
             LogManager.getLogManager().readConfiguration(in);
 57  
 
 58  0
             if (verboseLogFile != null && !verboseLogFile.isEmpty()) {
 59  0
                 verboseLoggingEnabled = true;
 60  0
                 final Logger logger = Logger.getLogger("");
 61  0
                 final File logFile = new File(verboseLogFile);
 62  0
                 final File logDir = logFile.getParentFile();
 63  0
                 if (logDir != null && !logDir.isDirectory() && !logDir.mkdirs()) {
 64  0
                     final String msg = String.format("Unable to create directory '%s', verbose logging will be disabled.",
 65  
                             logDir.getAbsolutePath());
 66  0
                     throw new IOException(msg);
 67  
                 }
 68  0
                 final FileHandler fileHandler = new FileHandler(verboseLogFile, true);
 69  0
                 fileHandler.setFormatter(new SimpleFormatter());
 70  0
                 fileHandler.setLevel(Level.FINE);
 71  0
                 fileHandler.setFilter(new LogFilter());
 72  
 
 73  0
                 logger.addHandler(fileHandler);
 74  0
                 logger.setLevel(Level.FINE);
 75  
             }
 76  0
         } catch (IOException ex) {
 77  0
             LOGGER.log(Level.WARNING, "IO Error preparing the logger", ex);
 78  0
         } catch (SecurityException ex) {
 79  0
             LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
 80  0
         } catch (Throwable ex) {
 81  0
             LOGGER.log(Level.WARNING, "Error preparing the logger", ex);
 82  
         } finally {
 83  0
             if (in != null) {
 84  
                 try {
 85  0
                     in.close();
 86  0
                 } catch (IOException ex) {
 87  0
                     LOGGER.log(Level.FINEST, "Error closing resource stream", ex);
 88  0
                 }
 89  
             }
 90  
         }
 91  0
     }
 92  
     /**
 93  
      * Whether or not verbose logging is enabled.
 94  
      */
 95  0
     private static boolean verboseLoggingEnabled = false;
 96  
 
 97  
     /**
 98  
      * Get the value of verboseLoggingEnabled.
 99  
      *
 100  
      * @return the value of verboseLoggingEnabled
 101  
      */
 102  
     public static boolean isVerboseLoggingEnabled() {
 103  0
         return verboseLoggingEnabled;
 104  
     }
 105  
 }