Stubbisms – Tony’s Weblog

July 15, 2008

Some serious logging niceness – Logback with Eclipse plugin

Filed under: Java — Tags: , , , — Antony Stubbs @ 12:25 am

I’ve just stumbled upon a new logging framework called Logback – I’d seen it before, but this time, someone I respect was switching to it, and someone he respects wrote an article on it which sums it up pretty nicely.

I especially take notice of the console Eclipse plugin – that’s what has made me download, install and try it out. And so far – nice. Serious nice.

The killer feature of the console plugin being two:

  1. Go to the java class and line where any given logging request was issued by double-clicking on its output line.
  2. Change the output format dynamically whenever you chose to do so.

And just for fun…

Java

package org.tony.nathan.daniel.james;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.util.LoggerStatusPrinter;

public class LogbackJavaTest {

	public static void main(String[] args) {

	    Logger log = LoggerFactory.getLogger(LogbackJavaTest.class);
	    log.debug( "Hello world." );
	    log.info( "interesting info" );
	    log.warn( "WARNING WILL ROBINSON!" );
	    log.error( "I'm sorry Dave..." );
	    try {
		throw new Exception("Holly crapo we're dying!" );
	    } catch (Exception e) {
		log.error( "Something bad happened", e);
	    }
	    LoggerStatusPrinter.printStatusInDefaultContext();

	}
]

Groovy

package org.tony.nathan.daniel.james

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import ch.qos.logback.classic.util.LoggerStatusPrinter

class LogBackGroovyTest {
	static void main(args) {

	    Logger log = LoggerFactory.getLogger(LogBackGroovyTest.class)
	    log.debug "Hello world."
	    log.info "interesting info"
	    log.warn "WARNING WILL ROBINSON!"
	    log.error "I'm sorry Dave..."
	    try {
			throw new Exception("Holly crapo we're dying!" )
		} catch (Exception e) {
			log.error( "Something bad happened", e)
		}
	    LoggerStatusPrinter.printStatusInDefaultContext()

	}
}

Scala

package org.tony.nathan.daniel.james

import org.slf4j.{Logger,LoggerFactory}

import ch.qos.logback.classic.util.LoggerStatusPrinter

object LogBackScalaTest {

  val log = LoggerFactory getLogger(LogBackScalaTest getClass)
  log debug( "Hello world." )
  log info( "interesting info" )
  log warn( "WARNING WILL ROBINSON!" )
  log error( "I'm sorry Dave..." )
  try {
    throw new Exception( "Holly crapo we're dying!" )
  }
  catch {
    case e => log.error( "Something bad happened", e)
  }
  LoggerStatusPrinter printStatusInDefaultContext

}
Advertisements

1 Comment »

  1. […] as mentioned in my previous post, along with SLF4J is a step into the recent present for logging technology (as opposed to living in […]

    Pingback by Death to Commons Logging - Long Live SLF4J! « Stubbisms - Tony’s Weblog — July 27, 2008 @ 2:05 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: