Blog
Fluent-Log API landed in e(fx)clipse
Last week I came a cross Googles FLogger-API and I really liked it. Back in e(fx)clipse land I started to miss it but because introducing a dependency to other log-frameworks is not possible - I implemented our own fluent log API inspired by Flogger. So how do you use it:
// if you have a logger
Logger logger = LoggerCreator.createLogger( Sample.class );
FluentLogger flogger = FluentLogger.of( logger );
// if you use @Log
@Inject
@Log
FluentLogger flogger;
// Log something
FluentLogContext debug = flogger.atDebug();
debug.log( "Hello World" );
debug.log( "Hello World with format %s", 10 );
debug.log( () -> "Lazy Hello World" );
debug.log( t -> "Lazy Hello World with Context" + t, o );
// Log with exception
try {
// ...
} catch( Throwable l ) {
flogger.atInfo().withException( t ).log( "Hello World" );
}
// Throttle: Only log every 100 log statement
flogger.atInfo().throttleByCount(100)
.log( "Log every 100 time" );
// Throttle: Only log every minute
flogger.atInfo().throttleByTime(1, TimeUnit.MINUTES)
.log( "Log every minute" );
// Build your own condition fluent addition
logger.atInfo().with( Throttle::new ).every( 100 )
.log( "Log every 100 time" )