io.github.selcukes.commons.logging.LoggerFactory Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* 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 io.github.selcukes.commons.logging;
import io.github.selcukes.commons.SelcukesBanner;
import io.github.selcukes.commons.config.ConfigFactory;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import static java.util.Objects.requireNonNull;
public final class LoggerFactory {
private static final Set listeners = ConcurrentHashMap.newKeySet();
private static boolean initialized;
private LoggerFactory() {
}
public static void addListener(LogRecordListener listener) {
listeners.add(listener);
}
public static void removeListener(LogRecordListener listener) {
listeners.remove(listener);
}
public static synchronized Logger getLogger(Class> clazz) {
requireNonNull(clazz, "Class must not be null");
initialize();
return new DelegatingLogger(clazz.getName());
}
private static synchronized void initialize() {
if (initialized) {
return;
}
initialized = true;
ConfigFactory.loadLoggerProperties();
SelcukesBanner.printBanner();
}
private static final class DelegatingLogger implements Logger {
private static final String THIS_LOGGER_CLASS = DelegatingLogger.class.getName();
private final String name;
private final java.util.logging.Logger julLogger;
DelegatingLogger(String name) {
this.name = name;
this.julLogger = java.util.logging.Logger.getLogger(name);
}
@Override
public void error(Supplier message) {
log(Level.SEVERE, null, message);
}
@Override
public void error(Throwable throwable, Supplier message) {
log(Level.SEVERE, throwable, message);
}
@Override
public void warn(Supplier message) {
log(Level.WARNING, null, message);
}
@Override
public void warn(Throwable throwable, Supplier message) {
log(Level.WARNING, throwable, message);
}
@Override
public void info(Supplier message) {
log(Level.INFO, null, message);
}
@Override
public void info(Throwable throwable, Supplier message) {
log(Level.INFO, throwable, message);
}
@Override
public void config(Supplier message) {
log(Level.CONFIG, null, message);
}
@Override
public void config(Throwable throwable, Supplier message) {
log(Level.CONFIG, throwable, message);
}
@Override
public void debug(Supplier message) {
log(Level.FINE, null, message);
}
@Override
public void debug(Throwable throwable, Supplier message) {
log(Level.FINE, throwable, message);
}
@Override
public void trace(Supplier message) {
log(Level.FINER, null, message);
}
@Override
public void trace(Throwable throwable, Supplier message) {
log(Level.FINER, throwable, message);
}
private void log(Level level, Throwable throwable, Supplier message) {
boolean loggable = julLogger.isLoggable(level);
if (loggable || !listeners.isEmpty()) {
LogRecord logRecord = createLogRecord(level, throwable, message);
julLogger.log(logRecord);
for (LogRecordListener listener : listeners) {
listener.logRecordSubmitted(logRecord);
}
}
}
private LogRecord createLogRecord(Level level, Throwable throwable, Supplier message) {
StackTraceElement[] stack = new Throwable().getStackTrace();
String sourceClassName = null;
String sourceMethodName = null;
boolean found = false;
for (StackTraceElement element : stack) {
String className = element.getClassName();
if (THIS_LOGGER_CLASS.equals(className)) {
found = true; // Next element is calling this logger
} else if (found) {
sourceClassName = className;
sourceMethodName = element.getMethodName();
break;
}
}
LogRecord logRecord = new LogRecord(level, message == null ? null : message.get());
logRecord.setLoggerName(name);
logRecord.setThrown(throwable);
logRecord.setSourceClassName(sourceClassName);
logRecord.setSourceMethodName(sourceMethodName);
logRecord.setResourceBundleName(julLogger.getResourceBundleName());
logRecord.setResourceBundle(julLogger.getResourceBundle());
return logRecord;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy