net.openhft.chronicle.logger.ChronicleLogConfig Maven / Gradle / Ivy
/*
* Copyright 2014-2020 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author lburgazzoli
* @author dpisklov
*
* Configuration example:
*
* # default
* chronicle.logger.base = ${java.io.tmpdir}/chronicle/${pid}
*
* # logger : root
* chronicle.logger.root.path = ${chronicle.logger.base}/root
* chronicle.logger.root.level = debug
* chronicle.logger.root.shortName = false
* chronicle.logger.root.append = false
*
* # logger : Logger1
* chronicle.logger.Logger1.path = ${chronicle.logger.base}/logger_1
* chronicle.logger.Logger1.level = info
* chronicle.logger.Logger1.wireType = json
*/
public class ChronicleLogConfig {
public static final String KEY_LEVEL = "level";
public static final String KEY_PATH = "path";
public static final String KEY_WIRETYPE = "wireType";
public static final String KEY_APPEND = "append";
public static final String PLACEHOLDER_START = "${";
public static final String PLACEHOLDER_END = "}";
private static final String KEY_PROPERTIES_FILE = "chronicle.logger.properties";
private static final String KEY_PREFIX = "chronicle.logger.";
private static final String KEY_PREFIX_ROOT = "chronicle.logger.root.";
private static final String KEY_CFG_PREFIX = "chronicle.logger.root.cfg.";
private static final String PLACEHOLDER_PID = "${pid}";
private static final List DEFAULT_CFG_LOCATIONS = Arrays.asList(
"chronicle-logger.properties",
"config/chronicle-logger.properties"
);
private static final String PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
private final Properties properties;
private final LogAppenderConfig appenderConfig;
private ChronicleLogConfig(final Properties properties, final LogAppenderConfig appenderConfig) {
this.properties = properties;
this.appenderConfig = appenderConfig;
}
public static ChronicleLogConfig load(final Properties properties) {
return new ChronicleLogConfig(
properties,
loadAppenderConfig(properties)
);
}
/**
* @param cfgPath the configuration path
* @return the configuration object
*/
public static ChronicleLogConfig load(String cfgPath) {
try {
return load(getConfigurationStream(cfgPath));
} catch (Exception e) {
// is printing stack trace and falling through really the right thing
// to do here, or should it throw out?
e.printStackTrace();
}
return null;
}
private static ChronicleLogConfig load(InputStream in) {
if (in != null) {
Properties properties = new Properties();
try {
properties.load(in);
in.close();
} catch (IOException ignored) {
}
return load(interpolate(properties));
} else {
System.err.printf(
"Unable to configure chronicle-logger:"
+ " configuration file not found in default locations (%s)"
+ " or System property (%s) is not defined \n",
DEFAULT_CFG_LOCATIONS.toString(),
KEY_PROPERTIES_FILE);
}
return null;
}
/**
* @return the configuration object
*/
public static ChronicleLogConfig load() {
try {
InputStream is = getConfigurationStream(System.getProperty(KEY_PROPERTIES_FILE));
if (is == null) {
for (String location : DEFAULT_CFG_LOCATIONS) {
is = getConfigurationStream(location);
if (is != null) {
break;
}
}
}
if (is != null) {
return load(is);
}
} catch (Exception e) {
// is printing stack trace and falling through really the right thing
// to do here, or should it throw out?
e.printStackTrace();
}
return null;
}
private static InputStream getConfigurationStream(String cfgPath) throws IOException {
if (cfgPath != null) {
final File cfgFile = new File(cfgPath);
if (!cfgFile.exists()) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(cfgPath);
} else if (cfgFile.canRead()) {
return new FileInputStream(cfgFile);
}
}
return null;
}
private static Properties interpolate(final Properties props) {
int amended;
do {
amended = 0;
for (Map.Entry