The effect of the final implementation is as follows: 650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/6A/wKioL1ei1WrAtwOJAABOZjAIX_4294.png "title=" log4j_ Multiple_color.png "alt=" Wkiol1ei1wratwojaabozjaix_4294.png "/>
|
1. Knowledge Preparation
We're going to talk about a concept of "ANSI escape sequences", what does it really do? "ANSI escape Sequences" is a "special character" embedded in the text to control the formatting and color of the text.
ANSI escape sequences is characters embedded in the text used to control formatting, color, and other output options on V Ideo text terminals. |
For example,
(1) Normal output
$ echo "Hello World"
Will output the Hello World text,
(2) using the "ANSI escape sequences" output
$ ECHO-E "\033[34m Hello Colorful world!"
Will output a color-colored text.
Next, our focus is on the meaning of the following text
"\033[34m Hello Colorful world!"
(1) First, \033[34m is ANSI escape sequence
(2) Then, \033 is escape character, if you look at the ASCII code, you will find that 033 means ESC (escape)
(3) Then, the 34m table shows the foreground color (foreground color)
(4) Finally print out the text message Hello colorful world
Therefore, its syntax should be
"\033[escape-code Your-message"
Reference: ANSI Escape Sequenceshttp://lishicongli.blog.163.com/blog/static/14682590201132151848668/
$ echo- E "\033[34m Hello colorful world!" Hello colorful world! Above Echo statement uses ANSI escape sequence ( \033[34m ), above entire string (i.e. "\033[34m Hello colorful world!" ) is process as follows 1) First \033, is escape character, which causes-take some action 2) Here it set screens foreground color to Blue using [34m escape code. 3) Then it prints we normal message Hello colorful world! in blue color.
|
2. Practice
We talked about the ANSI escape sequence, but the Eclipse console does not support ANSI escape sequence.
The Eclipse console does not a support ANSI escape sequences. |
In order for Eclipse to support ANSI escape sequence, you need to introduce a plugin: ANSI escape in Console.
Eclipse Plugin–ansi Escape in Console http://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/
This Eclipse plugin interprets, the ANSI escape sequences to color, the console output. This can is pretty handy when using something like Jansi
|
Development steps
(1) Install ANSI Escape in Console.
(2) Download the component and add the jar package.
(3) configuration
(4) using the API
2.1. Install ANSI Escape in Console
Help-->eclipse Marketplace ...
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/6A/wKioL1ei3kLSxzrfAAAtJTmmPLQ888.png "title=" Help_ Eclipse marketplace.png "alt=" Wkiol1ei3klsxzrfaaatjtmmplq888.png "/>
Search ANSI Escape in Console for installation
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/6A/wKioL1ei3mmwJYMlAADxZZ5596c037.png "title=" Eclipse_marketplace_ansi_escape_in_console.png "alt=" Wkiol1ei3mmwjymlaadxzz5596c037.png "/>
After the installation is successful, restart the IDE and open window-->preferences view.
On the left side of the preferences window, if Ansi_console is found, the installation is successful.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/6B/wKiom1ei3trzDEtIAABrkTkvZR4493.png "title=" Ansi Console.png "alt=" Wkiom1ei3trzdetiaabrktkvzr4493.png "/>
2.2. Download the component and add the jar package
Jar Package |
|
Color-loggers-1.0.4.1.jar |
Https://github.com/mihnita/java-color-loggers/releases/download/v1.0.4.1/color-loggers-1.0.4.1.jar |
Slf4j-api-1.7.21.jar Slf4j-log4j12-1.7.21.jar Log4j-1.2.17.jar |
Http://www.slf4j.org/download.html
Http://logging.apache.org/log4j/1.2/download.html
|
Reference Java-color-loggers Https://github.com/mihnita/java-color-loggers
Color Console logging for log4j and JDK
|
2.3. Configuration
Add the Log4j.properties file in the SRC directory
Log4j.rootlogger=debug, console, filelog4j.appender.console= com.colorlog.log4j.ansicolorconsoleappenderlog4j.appender.console.layout= org.apache.log4j.enhancedpatternlayoutlog4j.appender.console.layout.conversionpattern=%d%p%c.%M ()-%m% nlog4j.appender.console.fatalcolour={esc}[1;35mlog4j.appender.console.errorcolour={esc}[0; 31mlog4j.appender.console.warncolour ={esc}[0;33mlog4j.appender.console.infocolour ={esc}[0; 30mlog4j.appender.console.debugcolour={esc}[0;32mlog4j.appender.console.tracecolour={esc}[1; 30mlog4j.appender.file=org.apache.log4j.rollingfileappenderlog4j.appender.file.file=./logs/ mylog.loglog4j.appender.file.maxfilesize=5kblog4j.appender.file.maxbackupindex=100log4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%d%p%c.%M ()-%m%n
2.4. Using the API
Package Com.rk.test;import Org.junit.test;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class HelloWorld {@Testpublic void Test () {Logger Logger = Loggerfactory.getlogger (Helloworld.class); Logger.debug ("Debug Info" ); Logger.info ("info"); Logger.warn ("Warn Information"); Logger.error ("error message");}}
Output
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/6B/wKioL1ei5v2BIrL-AABLUgDPyXk573.png "title=" Console_result.png "alt=" Wkiol1ei5v2birl-aablugdpyxk573.png "/>
Reference address Allows Eclipse/myeclipse console and log4j logs to support multiple colors http://blog.csdn.net/javawinner/article/details/41548259
|
Reference ANSI escape sequence
http://ascii-table.com/ansi-escape-sequences.php
ESC[Value;...;Valuem
Set Graphics Mode: Calls The graphics functions specified by the following values. These specified functions remain active until the next occurrence of this escape sequence. Graphics mode changes the colors and attributes of text (such as bold and underline) displayed on the screen.
Foreground Colors Black Red Green Yellow Blue Magenta Approx . Cyan Panax Notoginseng White
Background Colors Black Red Green Yellow Blue Magenta Cyan White Parameters through meet the ISO 6429 standard.
Parameters through meet the ISO 6429 standard.
|
Log Component family: (5) log4j log support for eclipse/myeclipse console multiple colors