com.brambolt.util.Context Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brambolt-rt Show documentation
Show all versions of brambolt-rt Show documentation
Small utilities and convenience wrappers.
package com.brambolt.util;
import org.slf4j.Logger;
import java.util.Vector;
/**
* Contextual logging utility.
*/
@SuppressWarnings("unused")
public class Context {
/** Log level. */
@SuppressWarnings("unused")
public enum Level {
/** The debug log level. */
DEBUG(false),
/** The error log level. */
ERROR(true),
/** The fatal error log level. */
FATAL(true),
/** The info log level. */
INFO(false),
/** The trace log level. */
TRACE(false),
/** The warning log level. */
WARN(true);
private final boolean dirty;
/**
* Constructor.
* @param dirty True iff the level is dirty.
*/
Level(boolean dirty) {
this.dirty = dirty;
}
/**
* Indicates whether the level is dirty.
* @return True iff the level is dirty.
*/
public boolean isDirty() {
return dirty;
}
/**
* Indicates whether the level is clean.
* @return True iff the level is clean.
*/
public boolean isClean() {
return !isDirty();
}
}
/** Context entry. */
public static class Entry {
/** The log level of the context entry. */
public final Level level;
/** The source class for the context entry, may be null. */
public final Class> sourceClass;
/** An associated throwable, may be null. */
public final Throwable throwable;
/** The message for the context entry. */
public final String message;
/** The message parameters, may be empty or null. */
public final Object[] params;
/**
* Constructor.
* @param level The entry log level.
* @param sourceClass The entry source class.
* @param message The entry message.
* @param params The entry message parameters.
*/
public Entry(Level level, Class> sourceClass, String message, Object... params) {
this(level, sourceClass, null, message, params);
}
/**
* Constructor.
* @param level The entry log level.
* @param sourceClass The entry source class.
* @param throwable A caught throwable.
* @param message The entry message.
* @param params The entry message parameters.
*/
public Entry(Level level, Class> sourceClass, Throwable throwable, String message, Object... params) {
this.level = level;
this.sourceClass = sourceClass;
this.throwable = throwable;
this.message = message;
this.params = params;
}
}
/**
* Creates a context object.
* @param logger The logger to wrap.
* @return The instantiated context object.
*/
public static Context create(Logger logger) {
return new Context(logger);
}
private final Vector entries = new Vector<>();
private final Logger logger;
private Context(Logger logger) {
this.logger = logger;
}
/**
* Indicates whether the context is clean.
* @return True iff the context is clean.
*/
public boolean isClean() {
for (Entry entry: entries) {
if (entry.level.isDirty())
return false;
}
return true;
}
/**
* Indicates whether the context is dirty.
* @return True iff the context is dirty.
*/
public boolean isDirty() {
return !isClean();
}
/**
* Adds a warning to the context.
* @param c The source class for the warning.
* @param message The log statement message.
* @param params The message parameters.
*/
public void warn(Class> c, String message, Object... params) {
warn(c, null, message, params);
}
/**
* Adds a warning to the context.
* @param c The source class for the warning.
* @param t A caught throwable.
*/
public void warn(Class> c, Throwable t) {
warn(c, "...", t);
}
/**
* Adds a warning to the context.
* @param c The source class.
* @param t A caught throwable.
* @param message The message to log.
* @param params The message parameters.
*/
public void warn(Class> c, Throwable t, String message, Object... params) {
store(Level.WARN, c, t, message, params);
logger.warn(String.format(message, params), t);
}
/**
* Adds an error to the context. The context becomes dirty.
* @param c The source class.
* @param message The message to log.
* @param params The message parameters.
*/
public void error(Class> c, String message, Object... params) {
error(c, null, message, params);
}
/**
* Adds an error to the context. The context becomes dirty.
* @param c The source class.
* @param t A caught throwable.
*/
public void error(Class> c, Throwable t) {
error(c, "...", t);
}
/**
* Adds an error to the context. The context becomes dirty.
* @param c The source class.
* @param t A caught throwable.
* @param message The message to log.
* @param params The message parameters.
*/
public void error(Class> c, Throwable t, String message, Object... params) {
store(Level.ERROR, c, t, message, params);
}
/**
* Adds a fatal error to the context. The context becomes dirty.
* @param c The source class.
* @param message The message to log.
* @param params The message parameters.
*/
public void fatal(Class> c, String message, Object... params) {
fatal(c, null, message, params);
}
/**
* Adds a fatal error to the context. The context becomes dirty.
* @param c The source class.
* @param t A caught throwable.
*/
public void fatal(Class> c, Throwable t) {
fatal(c, t, "...");
}
/**
* Adds a fatal error to the context. The context becomes dirty.
* @param c The source class.
* @param t A caught throwable.
* @param message The message to log.
* @param params The message parameters.
*/
public void fatal(Class> c, Throwable t, String message, Object... params) {
store(Level.FATAL, c, t, message, params);
}
/**
* Adds an informational statement to the context.
* @param c The source class.
* @param message The message to log.
* @param params The message parameters.
*/
public void info(Class> c, String message, Object... params) {
store(Level.INFO, c, null, message, params);
}
private void store(Level l, Class> c, Throwable t, String message, Object... params) {
entries.add(new Entry(l, c, t, message, params));
}
/**
* Accessor for the added context entries.
* @return The existing context entries.
*/
public Vector getEntries() {
return entries;
}
/**
* Accessor for the wrapped logger.
* @return The wrapped logger held by the context object.
*/
public Logger getLogger() {
return logger;
}
}