
net.openhft.chronicle.logger.slf4j.ChronicleLoggerFactory Maven / Gradle / Ivy
/*
* Copyright 2014 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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.slf4j;
import net.openhft.chronicle.logger.*;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Simple implementation of Logger that sends all enabled slf4j messages,
* for all defined loggers, to one or more VanillaChronicle..
*
*
* To configure this sl4j binding you need to specify the location of a properties
* files via system properties:
*
* -Dchronicle.logger.properties=${pathOfYourPropertiesFile}
*
* The following system properties are supported to configure the behavior of this
* logger:
*
*
* chronicle.logger.root.path
* chronicle.logger.root.level
* chronicle.logger.root.shortName
* chronicle.logger.root.append
* chronicle.logger.root.format
* chronicle.logger.root.type
*
*/
public class ChronicleLoggerFactory implements ILoggerFactory {
private final Map loggers;
private ChronicleLogManager manager;
/**
* c-tor
*/
public ChronicleLoggerFactory() {
this.loggers = new ConcurrentHashMap<>();
this.manager = ChronicleLogManager.getInstance();
}
// *************************************************************************
//
// *************************************************************************
/**
* Return an appropriate {@link ChronicleLogger} instance by name.
*/
@Override
public Logger getLogger(String name) {
try {
return doGetLogger(name);
} catch(Exception e) {
System.err.println(
new StringBuilder("Unable to initialize chronicle-logger-slf4j ")
.append("(")
.append(name)
.append(")")
.append("\n ")
.append(e.getMessage())
.toString()
);
}
return NOPLogger.NOP_LOGGER;
}
// *************************************************************************
//
// *************************************************************************
/**
* Preload loggers
*/
public synchronized void warmup() {
}
/**
* close underlying Chronicles
*/
public synchronized void shutdown() {
this.manager.clear();
this.loggers.clear();
}
/**
*
*/
public synchronized void reload() {
shutdown();
this.manager.reload();
}
// *************************************************************************
//
// *************************************************************************
private synchronized Logger doGetLogger(String name) throws IOException {
Logger logger = loggers.get(name);
if (logger == null) {
final ChronicleLogWriter writer = manager.createWriter(name);
if(manager.isSimple(name)) {
logger = new ChronicleLogger.Text(
writer,
name,
ChronicleLogLevel.WARN);
} else if(manager.isBinary(name)) {
logger = new ChronicleLogger.Binary(
writer,
name,
manager.cfg().getLevel(name));
} else if(manager.isText(name)) {
logger = new ChronicleLogger.Text(
writer,
name,
manager.cfg().getLevel(name));
}
if(logger != null) {
loggers.put(name, logger);
} else {
System.err.println(
new StringBuilder("Unable to get a logger for ")
.append("(")
.append(name)
.append(")")
.toString()
);
}
}
return logger;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy