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

io.github.albertus82.util.logging.AnnotationConfigHandler Maven / Gradle / Ivy

Go to download

Java SWT/JFace Utility Library including a Preferences Framework, Lightweight HTTP Server and macOS support.

There is a newer version: 20.1.0
Show newest version
package io.github.albertus82.util.logging;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

import io.github.albertus82.util.logging.annotation.ExcludeLoggers;
import io.github.albertus82.util.logging.annotation.LogFormat;

public abstract class AnnotationConfigHandler extends Handler {

	private final String[] exclusions;

	protected AnnotationConfigHandler() {
		// Exclusions
		final ExcludeLoggers excludeLoggers = getClass().getAnnotation(ExcludeLoggers.class);
		if (excludeLoggers != null && excludeLoggers.value() != null) {
			exclusions = excludeLoggers.value();
		}
		else {
			exclusions = new String[] {};
		}

		// Format
		final LogFormat format = getClass().getAnnotation(LogFormat.class);
		if (format != null && format.value() != null) {
			setFormatter(new CustomFormatter(format.value()));
		}
	}

	@Override
	public boolean isLoggable(final LogRecord rec) {
		boolean loggable = super.isLoggable(rec);
		if (!loggable) {
			return false;
		}
		else if (rec.getLoggerName() == null) {
			return true;
		}
		else {
			for (final String exclusion : exclusions) {
				if (rec.getLoggerName().startsWith(exclusion)) {
					return false;
				}
			}
			return true;
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy