
io.jsync.logging.impl.LoggerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsync.io Show documentation
Show all versions of jsync.io Show documentation
jsync.io is a non-blocking, event-driven networking framework for Java
/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you 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.
*
* Modified from original form by Tim Fox
*/
package io.jsync.logging.impl;
import io.jsync.logging.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.LogManager;
/**
* @author Tim Fox
*/
public class LoggerFactory {
public static final String LOGGER_DELEGATE_FACTORY_CLASS_NAME = "io.jsync.logger-delegate-factory-class-name";
public static final String LOGGER_PROPERTIES_FILE = "io.jsync.logger-properties-file";
private static final ConcurrentMap loggers = new ConcurrentHashMap<>();
private static volatile LogDelegateFactory delegateFactory;
static {
// By default we are going to load logging.properties from jsync.io internally.
// Users can override this by System.setProperty("io.jsync.logger-properties-file","logging2.properties");
if (System.getProperty("java.util.logging.config.file") == null) {
try {
InputStream finalInputStream = null;
// Load the default properties
String defaultPropertiesPath = "logging.properties";
InputStream defaultPropertiesStream =
LoggerFactory.class.getClassLoader().getResourceAsStream(defaultPropertiesPath);
if (defaultPropertiesStream != null) {
finalInputStream = defaultPropertiesStream;
if (System.getProperty("log4j.configuration") == null) {
System.setProperty("log4j.configuration", defaultPropertiesPath);
}
}
// Load the override properties
if (System.getProperty(LOGGER_PROPERTIES_FILE) != null) {
String overridePropertiesPath = System.getProperty(LOGGER_PROPERTIES_FILE);
InputStream overridePropertiesStream =
LoggerFactory.class.getClassLoader().getResourceAsStream(overridePropertiesPath);
if (overridePropertiesStream != null) {
if (finalInputStream != null) {
finalInputStream = new SequenceInputStream(finalInputStream, overridePropertiesStream);
}
}
}
if (finalInputStream != null) {
LogManager.getLogManager().readConfiguration(finalInputStream);
}
} catch (IOException e) {
}
}
initialise();
}
public static synchronized void initialise() {
LogDelegateFactory delegateFactory;
// If a system property is specified then this overrides any delegate factory which is set
// programmatically - this is primarily of use so we can configure the logger delegate on the client side.
// call to System.getProperty is wrapped in a try block as it will fail if the client runs in a secured
// environment
String className = JULLogDelegateFactory.class.getName();
try {
className = System.getProperty(LOGGER_DELEGATE_FACTORY_CLASS_NAME);
} catch (Exception e) {
}
if (className != null) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Class> clz = loader.loadClass(className);
delegateFactory = (LogDelegateFactory) clz.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Error instantiating transformer class \"" + className + "\"", e);
}
} else {
delegateFactory = new JULLogDelegateFactory();
}
LoggerFactory.delegateFactory = delegateFactory;
}
public static Logger getLogger(final Class> clazz) {
return getLogger(clazz.getCanonicalName());
}
public static Logger getLogger(final String name) {
Logger logger = loggers.get(name);
if (logger == null) {
LogDelegate delegate = delegateFactory.createDelegate(name);
logger = new Logger(delegate);
Logger oldLogger = loggers.putIfAbsent(name, logger);
if (oldLogger != null) {
logger = oldLogger;
}
}
return logger;
}
public static void removeLogger(String name) {
loggers.remove(name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy