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:
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.
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
Mapping List in Yaml to List of Objects in Spring Boot
Differencebetween Lowagie and Itext
How to Use Concurrentlinkedqueue
Copying Text to the Clipboard Using Java
How to Format a Number 0..9 to Display with 2 Digits (It's Not a Date)
Are Java Static Calls More or Less Expensive Than Non-Static Calls
Log4J2 - Assigning File Appender Filename at Runtime
Why Do Consumers Accept Lambdas with Statement Bodies But Not Expression Bodies
Javac Source and Target Options
Chat Client Emoticons Window Java
How to Get the Count of Line in a File in an Efficient Way
How to Get Java Logging Output to Appear on a Single Line
How to Modify JSONnode in Java
How to I Output Org.W3C.Dom.Element to String Format in Java