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:
- http://logging.apache.org/log4j/1.2/manual.html
- https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
- http://saltnlight5.blogspot.com/2013/08/how-to-configure-slf4j-with-different.html
- http://logging.apache.org/log4j/2.x/manual/layouts.html
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