org.omnaest.utils.assertion
Class AssertLogger

java.lang.Object
  extended by org.omnaest.utils.assertion.AssertLogger
All Implemented Interfaces:
Serializable

public class AssertLogger
extends Object
implements Serializable

An AssertLogger provides methods of a Logger as well as methods of an Assert helper. This allows to easily log assertion based Exceptions and their stack trace information without having them to be raised into the callers logic. The callers logic will only have to deal with an easy boolean return value.

Usage examples

 AssertLogger assertLogger = new AssertLogger( SampleClass.class );
 
Simple logging:
 this.assertLogger.info.message( "Simple message logged with the INFO log level" );
 
 //generate and log a message only if the loglevel of DEBUG is enabled
 this.assertLogger.debug.message( new MessageFactory()
 {
   @Override
   public String message()
   {
     return "Message which is costly to generate";
   }
 } );
 
Logging catched exceptions:
 try
 {
   throw new Exception( "Some exception" );
 }
 catch ( Exception e )
 {
   this.assertLogger.warn.message( e );
 }
 
Assertions:
 assertLogger.debug.assertThat().isTrue( expression );
 assertLogger.warn.assertThat().isNotNull( object, "Additional message" );
 assertLogger.info.assertThat().fails( "Additional message" );
 
Assertions in combination with control flow:
 final Collection<String> collection = ...
 if ( this.assertLogger.error.assertThat().isNotEmpty( collection, "This collection must not be empty!" ) )
 {
   // this block is only executed if the collection is not empty 
 }
 
which replaces code like:
 try
 {
   //
   Assert.isNotEmpty( collection );
   
   //...      
 }
 catch ( Exception e )
 {
   this.logger.error( "Assertion failed", e );
 }
 

Author:
Omnaest
See Also:
AssertLogger.Loglevel, AssertLogger.LoglevelAssert, Assert, Logger, Serialized Form

Nested Class Summary
static interface AssertLogger.DirectAssert
          Provides assert methods
static interface AssertLogger.DirectAssertHandler
          Handler of an assertion result which allows to throw Exceptions or log messages
static interface AssertLogger.DirectAssertHandlerMessageChoice
           
static interface AssertLogger.DirectAssertResultValueProvider
          Allows to resolve the result of any previous assertion
static interface AssertLogger.DirectAssertSuccessFailureChoice
          Choice between AssertLogger.DirectAssertSuccessFailureChoice.onSuccess() or AssertLogger.DirectAssertSuccessFailureChoice.onFailure().
static interface AssertLogger.DirectAssertWithExpression
          AssertLogger.DirectAssert with a previous set expression
static interface AssertLogger.Loglevel
          Representation of a selected AssertLogger.Loglevel of the AssertLogger.
static interface AssertLogger.LoglevelAssert
          Provider for Assert based methods which throws appropriate Exceptions which it catches immediately and logs it to the underlying Logger using the selected AssertLogger.Loglevel.
protected static class AssertLogger.LoglevelImpl
          Implementation for AssertLogger.Loglevel and AssertLogger.LoglevelAssert
static interface AssertLogger.MessageFactory
          A AssertLogger.MessageFactory allows to produce messages
 
Field Summary
 AssertLogger.Loglevel debug
           
 AssertLogger.Loglevel error
           
 AssertLogger.Loglevel info
           
 org.slf4j.Logger logger
           
 AssertLogger.Loglevel trace
           
 AssertLogger.Loglevel warn
           
 
Constructor Summary
AssertLogger(Class<?> type)
          Creates an AssertLogger using the LoggerFactory to create an Logger instance.
AssertLogger(org.slf4j.Logger logger)
           
 
Method Summary
 AssertLogger.DirectAssert assertThat()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

public final org.slf4j.Logger logger

trace

public final AssertLogger.Loglevel trace

debug

public final AssertLogger.Loglevel debug

info

public final AssertLogger.Loglevel info

warn

public final AssertLogger.Loglevel warn

error

public final AssertLogger.Loglevel error
Constructor Detail

AssertLogger

public AssertLogger(Class<?> type)
Creates an AssertLogger using the LoggerFactory to create an Logger instance.

Parameters:
type -
See Also:
AssertLogger

AssertLogger

public AssertLogger(org.slf4j.Logger logger)
Parameters:
logger -
See Also:
AssertLogger
Method Detail

assertThat

public AssertLogger.DirectAssert assertThat()
Returns:
new AssertLogger.DirectAssert instance


Copyright © 2013. All Rights Reserved.