net.openhft.chronicle.logger.slf4j.ChronicleLoggerFactory Maven / Gradle / Ivy
The newest version!
/*
* 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.slf4j;
import net.openhft.chronicle.logger.ChronicleLogManager;
import net.openhft.chronicle.logger.ChronicleLogWriter;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.helpers.NOPLogger;
import org.slf4j.impl.SimpleLogger;
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:
* {@code -Dchronicle.logger.properties=${pathOfYourPropertiesFile}}
*
* The following system properties are supported to configure the behavior of this
* logger:
*
* - {@code chronicle.logger.root.path}
* - {@code chronicle.logger.root.level}
* - {@code chronicle.logger.root.append}
*
*/
public class ChronicleLoggerFactory implements ILoggerFactory {
private final Map loggers;
private final ChronicleLogManager manager;
// *************************************************************************
//
// *************************************************************************
/**
* c-tor
*/
public ChronicleLoggerFactory() {
this.loggers = new ConcurrentHashMap<>();
this.manager = ChronicleLogManager.getInstance();
}
// *************************************************************************
// for testing
// *************************************************************************
/**
* Return an appropriate {@link ChronicleLogger} instance by name.
*/
@Override
public Logger getLogger(String name) {
try {
return doGetLogger(name);
} catch (Exception e) {
System.err.println("Unable to initialize chronicle-logger-slf4j (" + name + ")\n " + e.getMessage());
e.printStackTrace();
}
return NOPLogger.NOP_LOGGER;
}
// *************************************************************************
//
// *************************************************************************
synchronized void reload() {
this.loggers.clear();
this.manager.reload();
}
private synchronized Logger doGetLogger(String name) {
Logger logger = loggers.get(name);
if (logger == null) {
if (name != null && name.startsWith("net.openhft")) {
SimpleLogger.lazyInit();
logger = new SimpleLogger(name);
} else {
final ChronicleLogWriter writer = manager.getWriter(name);
logger = new ChronicleLogger(writer, name, manager.cfg().getLevel(name));
}
loggers.put(name, logger);
}
return logger;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy