Making a Log4J Console Appender Use Different Colors for Different Threads

Making a log4j console appender use different colors for different threads

You can extend PatternLayout and override format(ILoggingEvent). There you could look at LoggingEvent.getThreadName() to get some color based on the thread name (odd/even, maybe?).

In order to output color to the console, you'll need to use an ANSI Escape Sequence.

For instance, to output a red text:

  "\u001b["  // Prefix - see [1]
+ "0" // Brightness
+ ";" // Separator
+ "31" // Red foreground
+ "m" // Suffix
+ text // the text to output
+ "\u001b[m " // Prefix + Suffix to reset color

Here some examples:

  • ColoredPatternLayout implementation by Ingo Thon.
  • Colour-coded Console Logging with Log4J blog post.

Just to add, maybe you could also achieve this by setting in the MDC a variable "randColor" with a random ANSI color code, for instance, in a Filter, and using it in the conversionPattern of a standard org.apache.log4j.PatternLayout in your log4j's console appender configuration:

<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="\u001b[0;%X{randColor}m ....... \u001b[m" />
</layout>
</appender>

[1] What does "\u001B[J" represent?

How to colorize Log4j2 output on console in intelliJ?

It seems like some default is broken in 2.10.0. By adding disableAnsi options, I could get the colors back with the last release.

<PatternLayout pattern="%highlight{...}" disableAnsi="false"/>

In the docs, it is said to default to false but it doesn't seem the case.

How to change colour of logging output log4j

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

Is possible to change the forecolor of log4j?

Check this resource : http://marc.info/?l=log4j-user&m=120574713010072

log4j for threads with different output files/configs

You don't need to create one configuration file per thread but you can add an appender programmatically. e.g.:

public static class TestJob implements Job {

private Logger logger;
private RollingFileAppender appender;

private void init() {
logger = Logger.getLogger(TestJob.class);
appender = new RollingFileAppender();
appender.setLayout(new PatternLayout("%d %-5p %c{1}:%L - %m%n"));
appender.setFile("logs/trh1/log1.txt");
appender.setAppend(false);
appender.setMaxFileSize("100MB");
appender.setMaxBackupIndex(10);
appender.activateOptions();
logger.setAdditivity(false);
logger.addAppender(appender);
}

private void destroy() {
logger.removeAppender(appender);
}

public void execute(JobExecutionContext context) throws JobExecutionException {
init();

for (int i = 0; i < 10; i++) {
if (logger.isInfoEnabled()) {
logger.info("This is " + i);
}
}

destroy();
}

}

You may want to see the Short introduction to log4j: Ceki Gülcü, March 2002.

unable to write into different logs for different threads using log4j2

Each of your threads calls LoggerContext#setConfigLocation, which causes a reconfiguration of Log4j2. The reconfiguration removes all the appenders you added programmatically. That is why you end up with a single appender.

Remark that you do not need programmatic configuration to have a different file appender per thread. You can just use the RoutingAppender as in this question.

Edit: If you really want to use programmatic configuration (which is discouraged for compatibility reasons), you also need to use a unique name for each appender, otherwise they will be silently ignored (the Logger#addAppender method you use is not part of the public API, cf. javadoc, so errors are not reported).

Formatting slf4j to log message types with colors

Two solutions come to my mind. They are not colors, but alternative solutions:

  1. In your File Appender configuration, you can configure the pattern to include the log level (error, warn, etc). Then you can grep the file, to filter messages by level.

  2. You can configure two file appenders (for two separate log files) with different level threshold. For instance, one would log all the logs above debug level (so info, warn, error) into let's say logs.txt and the other would log only the errors logs into errors.txt

Hope it helps.



Related Topics



Leave a reply



Submit