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

uk.org.lidalia.slf4jtest.LoggingEvent Maven / Gradle / Ivy

package uk.org.lidalia.slf4jtest;

import java.io.PrintStream;
import java.util.Collections;
import java.util.Map;

import org.joda.time.Instant;
import org.slf4j.Marker;
import org.slf4j.helpers.MessageFormatter;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import uk.org.lidalia.lang.Identity;
import uk.org.lidalia.lang.RichObject;
import uk.org.lidalia.slf4jext.Level;

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.FluentIterable.from;
import static java.util.Arrays.asList;

/**
 * Representation of a call to a logger for test assertion purposes.
 * 

* The contract of {@link #equals(Object)} and {@link #hashCode} is that they compare the results of: *

    *
  • {@link #getLevel()}
  • *
  • {@link #getMdc()}
  • *
  • {@link #getMarker()}
  • *
  • {@link #getThrowable()}
  • *
  • {@link #getMessage()}
  • *
  • {@link #getArguments()}
  • *
*

* They do NOT compare the results of {@link #getTimestamp()} or {@link #getCreatingLogger()} as this would render it impractical * to create appropriate expected {@link LoggingEvent}s to compare against. *

* Constructors and convenient static factory methods exist to create {@link LoggingEvent}s with appropriate * defaults. These are not documented further as they should be self-evident. */ @SuppressWarnings({ "PMD.ExcessivePublicCount", "PMD.TooManyMethods" }) public class LoggingEvent extends RichObject { public static LoggingEvent trace(final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, message, arguments); } public static LoggingEvent trace(final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, throwable, message, arguments); } public static LoggingEvent trace(final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, marker, message, arguments); } public static LoggingEvent trace( final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, marker, throwable, message, arguments); } public static LoggingEvent trace(final Map mdc, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, mdc, message, arguments); } public static LoggingEvent trace( final Map mdc, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, mdc, throwable, message, arguments); } public static LoggingEvent trace( final Map mdc, final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, mdc, marker, message, arguments); } public static LoggingEvent trace( final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.TRACE, mdc, marker, throwable, message, arguments); } public static LoggingEvent debug(final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, message, arguments); } public static LoggingEvent debug(final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, throwable, message, arguments); } public static LoggingEvent debug(final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, marker, message, arguments); } public static LoggingEvent debug( final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, marker, throwable, message, arguments); } public static LoggingEvent debug(final Map mdc, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, mdc, message, arguments); } public static LoggingEvent debug( final Map mdc, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, mdc, throwable, message, arguments); } public static LoggingEvent debug( final Map mdc, final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, mdc, marker, message, arguments); } public static LoggingEvent debug( final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.DEBUG, mdc, marker, throwable, message, arguments); } public static LoggingEvent info(final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, message, arguments); } public static LoggingEvent info(final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, throwable, message, arguments); } public static LoggingEvent info(final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, marker, message, arguments); } public static LoggingEvent info( final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, marker, throwable, message, arguments); } public static LoggingEvent info(final Map mdc, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, mdc, message, arguments); } public static LoggingEvent info( final Map mdc, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, mdc, throwable, message, arguments); } public static LoggingEvent info( final Map mdc, final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, mdc, marker, message, arguments); } public static LoggingEvent info( final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.INFO, mdc, marker, throwable, message, arguments); } public static LoggingEvent warn(final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, message, arguments); } public static LoggingEvent warn(final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, throwable, message, arguments); } public static LoggingEvent warn(final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, marker, message, arguments); } public static LoggingEvent warn( final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, marker, throwable, message, arguments); } public static LoggingEvent warn(final Map mdc, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, mdc, message, arguments); } public static LoggingEvent warn( final Map mdc, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, mdc, throwable, message, arguments); } public static LoggingEvent warn( final Map mdc, final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, mdc, marker, message, arguments); } public static LoggingEvent warn( final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.WARN, mdc, marker, throwable, message, arguments); } public static LoggingEvent error(final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, message, arguments); } public static LoggingEvent error(final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, throwable, message, arguments); } public static LoggingEvent error(final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, marker, message, arguments); } public static LoggingEvent error( final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, marker, throwable, message, arguments); } public static LoggingEvent error(final Map mdc, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, mdc, message, arguments); } public static LoggingEvent error( final Map mdc, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, mdc, throwable, message, arguments); } public static LoggingEvent error( final Map mdc, final Marker marker, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, mdc, marker, message, arguments); } public static LoggingEvent error( final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { return new LoggingEvent(Level.ERROR, mdc, marker, throwable, message, arguments); } public LoggingEvent(final Level level, final String message, final Object... arguments) { this(level, Collections.emptyMap(), Optional.absent(), Optional.absent(), message, arguments); } public LoggingEvent(final Level level, final Throwable throwable, final String message, final Object... arguments) { this(level, Collections.emptyMap(), Optional.absent(), fromNullable(throwable), message, arguments); } public LoggingEvent(final Level level, final Marker marker, final String message, final Object... arguments) { this(level, Collections.emptyMap(), fromNullable(marker), Optional.absent(), message, arguments); } public LoggingEvent( final Level level, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { this(level, Collections.emptyMap(), fromNullable(marker), fromNullable(throwable), message, arguments); } public LoggingEvent(final Level level, final Map mdc, final String message, final Object... arguments) { this(level, mdc, Optional.absent(), Optional.absent(), message, arguments); } public LoggingEvent( final Level level, final Map mdc, final Throwable throwable, final String message, final Object... arguments) { this(level, mdc, Optional.absent(), fromNullable(throwable), message, arguments); } public LoggingEvent( final Level level, final Map mdc, final Marker marker, final String message, final Object... arguments) { this(level, mdc, fromNullable(marker), Optional.absent(), message, arguments); } public LoggingEvent( final Level level, final Map mdc, final Marker marker, final Throwable throwable, final String message, final Object... arguments) { this(level, mdc, fromNullable(marker), fromNullable(throwable), message, arguments); } private LoggingEvent( final Level level, final Map mdc, final Optional marker, final Optional throwable, final String message, final Object... arguments) { this(Optional.absent(), level, mdc, marker, throwable, message, arguments); } LoggingEvent( final Optional creatingLogger, final Level level, final Map mdc, final Optional marker, final Optional throwable, final String message, final Object... arguments) { super(); this.creatingLogger = creatingLogger; this.level = checkNotNull(level); this.mdc = ImmutableMap.copyOf(mdc); this.marker = checkNotNull(marker); this.throwable = checkNotNull(throwable); this.message = checkNotNull(message); this.arguments = from(asList(arguments)).transform(new Function() { @Override public Object apply(Object input) { return fromNullable(input).or((Object)absent()); } }).toList(); } @Identity private final Level level; @Identity private final ImmutableMap mdc; @Identity private final Optional marker; @Identity private final Optional throwable; @Identity private final String message; @Identity private final ImmutableList arguments; private final Optional creatingLogger; private final Instant timestamp = new Instant(); private final String threadName = Thread.currentThread().getName(); public Level getLevel() { return level; } public ImmutableMap getMdc() { return mdc; } public Optional getMarker() { return marker; } public String getMessage() { return message; } public ImmutableList getArguments() { return arguments; } public Optional getThrowable() { return throwable; } /** * @return the logger that created this logging event. * @throws IllegalStateException if this logging event was not created by a logger */ public TestLogger getCreatingLogger() { return creatingLogger.get(); } /** * @return the time at which this logging event was created */ public Instant getTimestamp() { return timestamp; } /** * @return the name of the thread that created this logging event */ public String getThreadName() { return threadName; } void print() { final PrintStream output = printStreamForLevel(); output.println(formatLogStatement()); throwable.transform(printThrowableTo(output)); } private static Function printThrowableTo(final PrintStream output) { return new Function() { @Override public String apply(final Throwable throwableToPrint) { throwableToPrint.printStackTrace(output); return ""; } }; } private String formatLogStatement() { return getTimestamp() + " [" + getThreadName() + "] " + getLevel() + safeLoggerName() + " - " + getFormattedMessage(); } private String safeLoggerName() { return creatingLogger.transform(toLoggerNameString).or(""); } private static final Function toLoggerNameString = new Function() { @Override public String apply(final TestLogger logger) { return " " + logger.getName(); } }; private String getFormattedMessage() { return MessageFormatter.arrayFormat(getMessage(), getArguments().toArray()).getMessage(); } private PrintStream printStreamForLevel() { switch (level) { case ERROR: case WARN: return System.err; default: return System.out; } } }