Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change colour of logging output log4j [duplicate]

I'm currently working on a Java project and we are using logging. When a Log is made it is always printed in plain black text, whatever the logging level (INFO, ERROR, etc.)

How can I override the colour of these outputs so for example all ERROR logs will be red while all WARN logs will be orange etc.

Thanks

EDIT: I managed to download the ANSIColorLayout file and my log4j.properties now refers to it. However I get the following errors:

log4j:WARN No such property [all] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [reset] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [stacktrace] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [info] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [error] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [defaultcolor] in org.apache.log4j.ConsoleAppender.

My log4j.properties file looks like this:

log4j.rootLogger = DEBUG, CA, FA

log4j.appender.CA = org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout = balle.logging.ANSIColorLayout
log4j.appender.CA.layout.ConversionPattern=%-5p [%d{MM-dd-yyyy HH:mm:ss}] %c - %m%n
log4j.appender.CA.all=\u001B[1;37m log4j.appender.A1.fatal=\u001B[1;31m
log4j.appender.CA.error=\u001B[0;31m log4j.appender.A1.warn=\u001B[1;33m
log4j.appender.CA.info=\u001B[0;37m log4j.appender.A1.debug=\u001B[0;36m
log4j.appender.CA.reset=\u001B[1;37m
log4j.appender.CA.stacktrace=\u001B[0;31m
log4j.appender.CA.defaultcolor=\u001B[1;37m

My guess is that I'm supposed to use my own custom ConsoleAppender? Does anyone have any ideas?

Thanks

like image 258
Dan_Dan_Man Avatar asked Feb 20 '12 19:02

Dan_Dan_Man


1 Answers

You can download one of the various ANSIColorLayout.java implementation. These "color loggers" work by extending the PatternLayout class.

Then you can simply do something like this in your log4j properties:

log4j.appender.stdout.layout=com.acme.ANSIColorLayout

Here's a link to one ANSIColorLayout.java ready to use:

http://code.google.com/p/a-distributed-file-system/source/browse/trunk/DistributedFileSystem/ui/net/dfs/ui/ANSIColorLayout.java

like image 117
TacticalCoder Avatar answered Oct 12 '22 16:10

TacticalCoder