2

I am trying to configure my log output using Log4j and a log4j.properties file, but I am having trouble setting up colored output based on log level.

It appears that the logging utilities simply do not parse the %highlight ... conversion command.

I can tell that I am on the right track with the configuration, as the other parts of the pattern are working correctly.

I am basing my current code off of this S/O answer: log4j 2 adding multiple colors to console appender

I am assuming it is something simple that I am not seeing, as I am rather new to this. Unfortunately the documentation is hard to piece together, or is meant for xml based configurations.

Thanks for any help in advance.

Some resources I have been looking through already:

Here are the relevant files/code:

log4j.properties:

log4j.rootLogger=TRACE, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%highlight{%d %-5p [%t] (%F:%L) - %m%n}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}

Gradle dependencies:

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.+'
    compile 'org.slf4j:slf4j-log4j12:1.7.+'
    testCompile 'junit:junit:4.12'
}

Test to test logging:

@Test
public void testLogger(){
    LOGGER.info("Info level log");
    LOGGER.debug("Debug level log");
    LOGGER.warn("Warn level log");
    LOGGER.error("Error level log");
    LOGGER.trace("Trace level log");
}

Output:

log4j:ERROR Unexpected char [h] at position 2 in conversion patterrn.
%highlight{2017-07-08 13:18:21,243 INFO  [main] (LongLinkedListTest.java:40) - Info level log
}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}%highlight{2017-07-08 13:18:21,246 DEBUG [main] (LongLinkedListTest.java:41) - Debug level log
}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}%highlight{2017-07-08 13:18:21,246 WARN  [main] (LongLinkedListTest.java:42) - Warn level log
}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}%highlight{2017-07-08 13:18:21,246 ERROR [main] (LongLinkedListTest.java:43) - Error level log
}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}%highlight{2017-07-08 13:18:21,246 TRACE [main] (LongLinkedListTest.java:44) - Trace level log
}{FATAL=red, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}
Process finished with exit code 0

Final results:

For posterity, I ended up with the following:

Gradle Dependencies:

dependencies {
    //logging dependencies
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.+'
    compile 'org.apache.logging.log4j:log4j-api:2.+'
    compile 'org.apache.logging.log4j:log4j-core:2.+'
    compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2+'
    compile 'com.fasterxml.jackson.core:jackson-databind:2+'

    ...
}

Once I updated dependencies, I was able to move to a yaml based configuration, and using the %highlight notation worked like a charm!

If anyone wishes to see the final configuration, it can be found here: https://github.com/Epic-Breakfast-Productions/OWAT/blob/master/implementations/java/src/main/resources/log4j2.yaml

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Snappawapa
  • 1,697
  • 3
  • 20
  • 42

1 Answers1

3

Is it possible that you're mixing up Log4j2 with log4j 1.x? The log4j:ERROR error is an legacy log4j 1.2 error message.

Please simplify matters by only using Log4j2 dependencies and using a Log4j2 configuration. The configuration file is called log4j2.xml by default.

Please remove the org.slf4j:slf4j-log4j12:1.7.+ dependency and instead use log4j-api-2.x, log4j-core-2.x and log4j-slf4j-impl-2.x.

Useful links:

  1. Which jars
  2. Console appender
  3. PatternLayout with colors and highlighting
  4. Configuration

Log4j 1.2 became End of Life in summer 2015. Let's upgrade! :-)

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thanks for the response! I figured that I was using v1.... I was able to use the new dependencies, but now log4j cannot automatically pickup the `log4j.properties` files. Any insight? It looks like I might have to move to an xml or yml based configuration... – Snappawapa Jul 09 '17 at 16:32
  • After much fiddling, trial, and error, I got it figured out using a yaml configuration. Thanks again for the push in the right direction! – Snappawapa Jul 09 '17 at 19:29