log4j Console log information, according to the different levels, to display in color

Source: Internet
Author: User
Tags log4j

Let the console log color highlighted below, is divided into two situations.


One. In the Eclipse console

At this point, the simpler way of implementation. No other tool classes are required.

The warn,error,fatal level is displayed in red. Debug,info, default color


Log4j.xml


<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Log4j.dtd" > <log4j:configuration "http://jakarta.apache.org/log4j/" > <!--appenders--> <appender name= "CONSOLE. ERR "class=" Org.apache.log4j.ConsoleAppender "> <param name=" target "value=" System.err "/> <layout" class= " Org.apache.log4j.PatternLayout "> <param name=" Conversionpattern "value="%-5p:%c-%m%n "/> </layout&
		Gt <filter class= "Org.apache.log4j.varia.LevelRangeFilter" > <param name= "levelmin" value= "Warn"/> <para M name= "Levelmax" value= "fatal"/> <param name= "Acceptonmatch" value= "false"/> </filter> </append er> <appender name= "CONSOLE. Out "class=" Org.apache.log4j.ConsoleAppender "> <param name=" target "value=" System.out "/> <layout" class= " Org.apache.log4j.PatternLayout "> <param name=" Conversionpattern "value="%-5P:%c-%m%n "/> </layout> <filter class=" Org.apache.log4j.varia.LevelRangeFilter "> <param name=" Levelmin "value=" Debug "/> <param name=" Levelmax "value=" info "/> <param name=" Acceptonmatch "value=" FAL " Se "/> </filter> </appender> <logger name=" Com.errout "> <level value=" Debug "/> </l ogger> <!--Root Logger--> <root> <priority value= "warn"/> <appender-ref ref= "CONSOLE. ERR "/> <appender-ref ref=" CONSOLE.
 Out "/> </root> </log4j:configuration>

Package com.errout;

Import Org.apache.log4j.Logger;

public class Testerrout {

	private static final Logger Logger = Logger.getlogger (testerrout.class);
	
	public static void Main (string[] args) {
		Logger.debug (' This is DEBUG!!! ');
		Logger.info ("This is info!!!");
		Logger.warn ("This is warn!!!");
		Logger.error ("This is Error!!!");
		Logger.fatal ("This is Fatal!!!");
	}




Two. Using Jcabi-log-0.2.1.jar

Official website http://www.jcabi.com/jcabi-log/multicolor.html


Log4j.xml


<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE log4j:configuration Public "-//apache//dtd log4j 1.2//en" "Log4j.dtd" >
<log4j:configuration xmlns: log4j= "http://jakarta.apache.org/log4j/" >


	<appender name= "Multicolorconsole" Org.apache.log4j.ConsoleAppender ">
    <layout class=" Com.jcabi.log.MulticolorLayout ">
        <param Name= "Conversionpattern" value= "[%color{%-5p}]%c:%m%n"/>
    </layout>
	</appender>

	< Logger name= "Com.jcabi" >
		<level value= "Debug"/>
	</logger>
	
	<!--Root logger-->
	<root>
		<priority value= "warn"/>
		<appender-ref ref= "Multicolorconsole"
	/> </root>

</log4j:configuration>

Package Com.jcabi;

Import Org.apache.log4j.Logger;

/**
 * Use Jcabi to let log4j console color display
 * @author Lena Yang * * */Public
class Testjcabi {
	
	private static Final Logger Logger = Logger.getlogger (testjcabi.class);
	
	
	public static void Main (String arg[]) {
		Logger.debug (' This is DEBUG!!! ');
		Logger.info ("This is info!!!");
		Logger.warn ("This is warn!!!");
		Logger.error ("This is Error!!!");
		Logger.fatal ("This is Fatal!!!");
	}



This, in eclipse, has no effect. It only works in Linux.


Jcabi jar Package Download:

http://download.csdn.net/detail/hardwin/4633978


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.