Mercurial > hg > release > thermostat-0.15
changeset 256:422ac3e169ce
Configure logging through logging.properties file.
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/001013.html
PR 928
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Mon, 23 Apr 2012 10:52:52 +0200 |
parents | 2dce17087aaf |
children | 9c2e7e00665c |
files | common/src/main/java/com/redhat/thermostat/cli/Launcher.java common/src/main/java/com/redhat/thermostat/common/config/ConfigUtils.java common/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java distribution/config/logging.properties distribution/pom.xml |
diffstat | 5 files changed, 100 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/common/src/main/java/com/redhat/thermostat/cli/Launcher.java Fri Apr 20 18:43:24 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/cli/Launcher.java Mon Apr 23 10:52:52 2012 +0200 @@ -41,7 +41,9 @@ import java.util.Collection; import java.util.ServiceLoader; +import com.redhat.thermostat.common.config.InvalidConfigurationException; import com.redhat.thermostat.common.storage.ConnectionException; +import com.redhat.thermostat.common.utils.LoggingUtils; public class Launcher { @@ -52,6 +54,7 @@ private String[] args; public void run(String[] args) { + initLogging(); this.args = args; registerDefaultCommands(); if (hasNoArguments() || unknownCommand()) { @@ -61,6 +64,19 @@ } } + private void initLogging() { + try { + LoggingUtils.loadGlobalLoggingConfig(); + } catch (InvalidConfigurationException e) { + System.err.println("WARNING: Could not read global Thermostat logging configuration."); + } + try { + LoggingUtils.loadUserLoggingConfig(); + } catch (InvalidConfigurationException e) { + // We intentionally ignore this. + } + } + private boolean hasNoArguments() { return args.length == 0; }
--- a/common/src/main/java/com/redhat/thermostat/common/config/ConfigUtils.java Fri Apr 20 18:43:24 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/common/config/ConfigUtils.java Mon Apr 23 10:52:52 2012 +0200 @@ -40,6 +40,8 @@ public class ConfigUtils { + private static final String THERMOSTAT_USER_DIR = ".thermostat"; + public static String getThermostatHome() throws InvalidConfigurationException { // allow this to be specified also as a property, especially for // tests, this overrides the env setting @@ -53,7 +55,12 @@ } return home; } - + + public static String getThermostatUserHome() { + String home = System.getProperty("user.home"); + return home + File.separator + THERMOSTAT_USER_DIR; + } + public static File getBackendsBaseDirectory() throws InvalidConfigurationException { String loc = getThermostatHome() + File.separatorChar + "backends"; File file = new File(loc);
--- a/common/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java Fri Apr 20 18:43:24 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java Mon Apr 23 10:52:52 2012 +0200 @@ -36,6 +36,9 @@ package com.redhat.thermostat.common.utils; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; @@ -43,6 +46,8 @@ import java.util.logging.Logger; import com.redhat.thermostat.common.LogFormatter; +import com.redhat.thermostat.common.config.ConfigUtils; +import com.redhat.thermostat.common.config.InvalidConfigurationException; /** * A few helper functions to facilitate using loggers @@ -106,4 +111,29 @@ root.addHandler(handler); } + public static void loadGlobalLoggingConfig() throws InvalidConfigurationException { + File thermostatEtcDir = new File(ConfigUtils.getThermostatHome(), "etc"); + File loggingPropertiesFile = new File(thermostatEtcDir, "logging.properties"); + if (loggingPropertiesFile.isFile()) { + readLoggingProperties(loggingPropertiesFile); + } + } + + public static void loadUserLoggingConfig() throws InvalidConfigurationException { + File thermostatUserDir = new File(ConfigUtils.getThermostatUserHome()); + File loggingPropertiesFile = new File(thermostatUserDir, "logging.properties"); + if (loggingPropertiesFile.isFile()) { + readLoggingProperties(loggingPropertiesFile); + } + } + + private static void readLoggingProperties(File loggingPropertiesFile) + throws InvalidConfigurationException { + try (FileInputStream fis = new FileInputStream(loggingPropertiesFile)){ + LogManager.getLogManager().readConfiguration(fis); + } catch (SecurityException | IOException e) { + throw new InvalidConfigurationException("Could not read logging.properties", e); + } + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/distribution/config/logging.properties Mon Apr 23 10:52:52 2012 +0200 @@ -0,0 +1,37 @@ +# Copyright 2012 Red Hat, Inc. +# +# This file is part of Thermostat. +# +# Thermostat is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2, or (at your +# option) any later version. +# +# Thermostat is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Thermostat; see the file COPYING. If not see +# <http://www.gnu.org/licenses/>. +# +# Linking this code with other modules is making a combined work +# based on this code. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this code give +# you permission to link this code with independent modules to +# produce an executable, regardless of the license terms of these +# independent modules, and to copy and distribute the resulting +# executable under terms of your choice, provided that you also +# meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module +# which is not derived from or based on this code. If you modify +# this code, you may extend this exception to your version of the +# library, but you are not obligated to do so. If you do not wish +# to do so, delete this exception statement from your version. + +handlers=java.util.logging.ConsoleHandler +com.redhat.thermostat.level=INFO +com.mongodb.level=OFF
--- a/distribution/pom.xml Fri Apr 20 18:43:24 2012 +0200 +++ b/distribution/pom.xml Mon Apr 23 10:52:52 2012 +0200 @@ -116,6 +116,14 @@ <include>system/backend.properties</include> </includes> </resource> + <resource> + <directory>config</directory> + <targetPath>etc</targetPath> + <filtering>true</filtering> + <includes> + <include>logging.properties</include> + </includes> + </resource> </resources> </configuration> </execution> @@ -143,6 +151,7 @@ <phase>prepare-package</phase> <configuration> <target> + <mkdir dir="${project.build.directory}/etc" /> <mkdir dir="${project.build.directory}/storage" /> <mkdir dir="${project.build.directory}/storage/db" /> <mkdir dir="${project.build.directory}/storage/logs" />