Skip to content
alexhall edited this page Aug 8, 2011 · 1 revision

Logging-Config is targeted as users and describes how to configure logging in Spark and Sherpa for applications which use these libraries. This page is targeted at developers and describes how logging is configured in the Spark and Sherpa unit tests under Maven.

Default Configuration

The default logging configuration is as follows:

  • Projects containing code which generate log messages (currently spark-http-client and sherpa-java) use SLF4J to record log messages by declaring compile dependencies on slf4j-api.

  • Unit tests for these projects, as well as all projects which depend on them, are configured to use Log4j to record the log messages by declaring provided dependencies on slf4j-log4j12.

  • Log4j is configured to send logging output to the console at DEBUG level for Spark and Sherpa loggers, and at INFO level for all other loggers. The Log4j config files are located at:

    spark-spi/src/test/resources/log4j.properties: Configuration for Spark logging; imported by anything with a test-jar dependency on spark-spi.

    sherpa-java/src/test/resources/log4j.properties: Overrides the Spark logging configuration with additional configuration for Sherpa logging.

  • The Maven test plugin is configured to redirect console output from unit tests to files, one file per test. The output file for each test is located at target/surefire-reports/${testname}-output.txt.

Overriding the Default

The default configuration is designed to provide concise output to the console when running tests, but in such a way that debug output is not entirely lost. Still, when developing a particular component it can be convenient to send the debug output to the console instead of to a file (particularly when running a single test), or to tweak the logging settings to get more or less output from a particular class. The Maven project defines a couple of profiles which may be activated using the -P command-line switch for this purpose.

debugOut

The debugOut profile may be activated to send logging messages (and any other output, including System.out) to the console instead of a log file. Example:

# Sends output to file
> mvn test

# Sends output to console
> mvn -PdebugOut test

debugConfig

The debugConfig profile allows the developer to tweak the Log4j settings by reading the Log4j configuration from log4j-local.properties instead of the default log4j.properties. The repository is set up to ignore all files named log4j-local.properties, so this allows developers to modify the logging settings without running the risk of accidentally committing the custom logging settings back to the repository. It is intended to work in conjunction with the debugOut profile, but this is not required. You could even modify log4j-local.properties to write to a different log file instead of the console. Example:

# cd to the sub-module directory.
> cd sherpa-java
sherpa-java> cp src/test/resources/log4j.properties src/test/resources/log4j-local.properties

# Edit the log4j-local.properties to adjust the log level for your class of interest...

# Run the tests using the custom logging config
sherpa-java> mvn -PdebugOut,debugConfig test

Manually setting the Log4j config location

In addition to using the debugConfig profile, you may manually specify an arbitrary Log4j config file (e.g. from your home directory or another project directory) to use during testing via the -Dtest.log.config flag in Maven. Example:

# mvn -Dtest.log.config=file:/home/alex/my-log4j.properties test

Clone this wiki locally