Log4j

Aus SDQ-Wiki

Log4j as an OSGi Bundle

If you want to use the Apache Log4j Logging mechanisms by using a dependency to a Log4j OSGi-Bundle, you need to install the bundle from an Eclipse Orbit update site. For example, the version 1.2.15 of log4j can be found under

Add the update site, install the bundle and define the dependency to org.apache.log4j and you're fine.

FAQ Problem:

log4j:WARN No appenders could be found for logger (<CLASSNAME>).
log4j:WARN Please initialize the log4j system properly.

This appears in the console, when a class is used that declares and uses a log4j logger (e.g. in a fashion like this):

private static final Logger logger = Logger
	.getLogger(<CLASSNAME (of this class)>.class);

and then in the code:

logger.debug("Debug message");
logger.info("Info message");

Simple Solution (useful to get logging to work for JUnit tests):

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.SimpleLayout;

Then call

BasicConfigurator.resetConfiguration();
BasicConfigurator.configure(new ConsoleAppender(new SimpleLayout()));

once (e.g. in a @Before method).

Solution for Runconfiguration based on AbstractPCMLaunchConfigurationDelegate<T>:

@Override
protected ArrayList<LoggerAppenderStruct> setupLogging(Level logLevel) throws CoreException {
	ArrayList<LoggerAppenderStruct> loggerList = super.setupLogging(logLevel);

	loggerList.add(setupLogger("<PACKAGE>", logLevel,
		Level.DEBUG == logLevel ? DETAILED_LOG_PATTERN
		: SHORT_LOG_PATTERN));

	return loggerList;
}