1 /* 2 * Copyright (C) 2012 Google 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 com.google.caliper.config; 18 19 import static java.util.logging.Level.SEVERE; 20 import static java.util.logging.Level.WARNING; 21 22 import com.google.caliper.model.Run; 23 import com.google.caliper.options.CaliperDirectory; 24 import com.google.common.annotations.VisibleForTesting; 25 import com.google.common.base.Charsets; 26 import com.google.common.io.Closer; 27 28 import org.joda.time.format.ISODateTimeFormat; 29 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.IOException; 33 import java.util.logging.FileHandler; 34 import java.util.logging.LogManager; 35 import java.util.logging.Logger; 36 import java.util.logging.SimpleFormatter; 37 38 import javax.inject.Inject; 39 import javax.inject.Singleton; 40 41 /** 42 * Loading the logging configuration at {@code ~/.caliper/logging.properties} if present. 43 */ 44 @Singleton 45 final class LoggingConfigLoader { 46 private static final Logger logger = Logger.getLogger(LoggingConfigLoader.class.getName()); 47 48 private final File caliperDirectory; 49 private final LogManager logManager; 50 private final Run run; 51 LoggingConfigLoader(@aliperDirectory File caliperDirectory, LogManager logManager, Run run)52 @Inject LoggingConfigLoader(@CaliperDirectory File caliperDirectory, LogManager logManager, 53 Run run) { 54 this.caliperDirectory = caliperDirectory; 55 this.logManager = logManager; 56 this.run = run; 57 } 58 loadLoggingConfig()59 @Inject void loadLoggingConfig() { 60 File loggingPropertiesFile = new File(caliperDirectory, "logging.properties"); 61 if (loggingPropertiesFile.isFile()) { 62 Closer closer = Closer.create(); 63 FileInputStream fis = null; 64 try { 65 fis = closer.register(new FileInputStream(loggingPropertiesFile)); 66 logManager.readConfiguration(fis); 67 } catch (SecurityException e) { 68 logConfigurationException(e); 69 } catch (IOException e) { 70 logConfigurationException(e); 71 } finally { 72 try { 73 closer.close(); 74 } catch (IOException e) { 75 logger.log(SEVERE, "could not close " + loggingPropertiesFile, e); 76 } 77 } 78 logger.info(String.format("Using logging configuration at %s", loggingPropertiesFile)); 79 } else { 80 try { 81 maybeLoadDefaultLogConfiguration(LogManager.getLogManager()); 82 } catch (SecurityException e) { 83 logConfigurationException(e); 84 } catch (IOException e) { 85 logConfigurationException(e); 86 } 87 } 88 } 89 maybeLoadDefaultLogConfiguration(LogManager logManager)90 @VisibleForTesting void maybeLoadDefaultLogConfiguration(LogManager logManager) 91 throws SecurityException, IOException { 92 logManager.reset(); 93 File logDirectory = new File(caliperDirectory, "log"); 94 logDirectory.mkdirs(); 95 FileHandler fileHandler = new FileHandler(String.format("%s%c%s.%s.log", 96 logDirectory.getAbsolutePath(), File.separatorChar, 97 ISODateTimeFormat.basicDateTimeNoMillis().print(run.startTime()), run.id())); 98 fileHandler.setEncoding(Charsets.UTF_8.name()); 99 fileHandler.setFormatter(new SimpleFormatter()); 100 Logger globalLogger = logManager.getLogger(""); 101 globalLogger.addHandler(fileHandler); 102 } 103 logConfigurationException(Exception e)104 private static void logConfigurationException(Exception e) { 105 logger.log(WARNING, "Could not apply the logging configuration", e); 106 } 107 } 108