My solutions to some not-so-common problems

Enabling SLF4J logging in EclipseLink

Posted: March 15th, 2010 | Author: | Filed under: Java | Tags: , , , | No Comments »

I’m using EclipseLink as JPA provider in some of my OSGi based projects. Latelly I also switched from Apache Commons Logging to SLF4J (shortly speaking not to be forced to write LOG.isDebugEnabled() in case of debug messages in my code). Then I found out, that EclipseLink is using it’s own logger which can not be configured using standard log4j.xml file.

I was Googling a while trying to find something about configuring logging in EclipseLink I’ve discovered this:

  1. EclipseLink is using it’s own logger and it’s configuration in persistence.xml file
  2. You can use custom logger which must implement org.eclipse.persistence.logging.SessionLog interface

I followed instructions about using custom logger and I wrote simple CustomSessionLogger:

public class CustomSessionLogger extends AbstractSessionLog implements SessionLog {
	public static final Logger LOG = LoggerFactory.getLogger(CustomSessionLogger.class);
	public void log(SessionLogEntry sessionLogEntry) {
		switch (sessionLogEntry.getLevel()) {
			case SEVERE:
				LOG.error(sessionLogEntry.getMessage());
				break;
			case WARNING:
				LOG.warn(sessionLogEntry.getMessage());
				break;
			case INFO:
				LOG.info(sessionLogEntry.getMessage());
				break;
			default:
				LOG.debug(sessionLogEntry.getMessage());
        	}
	}
}

and modified my persistence.xml file adding given properties:

<properties>
	<property name="eclipselink.logging.logger"
		value="com.whileitcompiles.eclipselink.CustomSessionLogger" />
	<property name="eclipselink.logging.level" value="SEVERE" />
</properties>

And configured logger in my log4j.xml file.
I’m not happy with this solution because of hard dependences to EclipseLink in my code (CustomSessionLogger) but at this moment I’m not aware of any better solution to this problem. I’m waiting on your comments.

  • Facebook
  • Twitter
  • Google Plus
  • HackerNews
  • Reddit
  • Delicious
  • LinkedIn
  • StumbleUpon
  • RSS


Leave a Reply

You must be logged in to post a comment.