All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.slf4j.plus.ExceptionLoggerFactory Maven / Gradle / Ivy

/*
 * Copyright 2015-2016 the original author or authors.
 *
 * 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 org.slf4j.plus;

import java.lang.reflect.Constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

/**
 * {@link LoggerFactory} for {@link ExceptionLogger}
 */
@NoArgsConstructor(access = AccessLevel.PROTECTED) // @checkstyle:ok
public class ExceptionLoggerFactory {
	
	/**
	 * {@link ExceptionLogger} class
	 */
	@Setter
	@NonNull
	private static Class loggerClass = ExceptionLogger.class;
	
	/**
	 * Default max frame length
	 */
	@Setter
	private static int defaultMaxFrameLength = 3;
	
	/**
	 * Get {@link ExceptionLogger} / Note: This method uses {@link Throwable#getStackTrace()} to get class name
	 * 
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger() {
		
		return getLogger(new Throwable().getStackTrace()[1].getClassName());
	}
	
	/**
	 * Get {@link ExceptionLogger} / Note: This method uses {@link Throwable#getStackTrace()} to get class name
	 * 
	 * @param maxFrameLength max frame length
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(int maxFrameLength) {
		
		return getLogger(new Throwable().getStackTrace()[1].getClassName(), maxFrameLength);
	}
	
	/**
	 * Get {@link ExceptionLogger}
	 * 
	 * @param clazz log class
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(Class clazz) {
		
		return getLogger(LoggerFactory.getLogger(clazz), defaultMaxFrameLength);
	}
	
	/**
	 * Get {@link ExceptionLogger}
	 * 
	 * @param clazz log class
	 * @param maxFrameLength max frame length
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(Class clazz, int maxFrameLength) {
		
		return getLogger(LoggerFactory.getLogger(clazz), maxFrameLength);
	}
	
	/**
	 * Get {@link ExceptionLogger}
	 * 
	 * @param name log name
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(String name) {
		
		return getLogger(LoggerFactory.getLogger(name), defaultMaxFrameLength);
	}
	
	/**
	 * Get {@link ExceptionLogger}
	 * 
	 * @param name log name
	 * @param maxFrameLength max frame length
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(String name, int maxFrameLength) {
		
		return getLogger(LoggerFactory.getLogger(name), maxFrameLength);
	}
	
	/**
	 * Get {@link ExceptionLogger}
	 * 
	 * @param delegate {@link Logger}
	 * @param maxFrameLength max frame length
	 * @return {@link ExceptionLogger}
	 */
	public static ExceptionLogger getLogger(Logger delegate, int maxFrameLength) {
		
		try {
			
			Constructor constructor = loggerClass.getDeclaredConstructor(Logger.class,
				int.class);
			
			constructor.setAccessible(true);
			
			return constructor.newInstance(delegate, maxFrameLength);
		}
		catch (ReflectiveOperationException e) {
			
			throw new IllegalStateException("Failed to create logger", e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy