您的位置:首页 > 编程语言 > Java开发

日志组件系列:(5)让Eclipse/MyEclipse的控制台的log4j日志支持多种颜色

2016-08-04 14:57 363 查看
最终实现的效果如下:




1、知识准备
我们要谈到一个概念“ANSI escape sequences”,它是什么,究竟有什么作用呢?“ANSI escape sequences”就是嵌入到文本中的“特殊字符”,用来控制文本的格式和颜色。
ANSI escape sequences are characters embedded in the text used to control formatting, color, and other output options on video text terminals.
举例来说,
(1)普通输出
$ echo "Hello World"
会输出Hello World文本,
(2)使用“ANSI escape sequences”输出
$ echo -e "\033[34m   Hello Colorful  World!"
会输出一个带有颜色的文本。

接下来,我们的关注重点是下面这段文本的含义
"\033[34m   Hello Colorful  World!"


(1)首先,\033[34m 是ANSI escape sequence
(2)接着,\033是escape character,如果查看ASCII码,会发现033表示 ESC(escape)
(3)再接着,34m表则示设置前景色(foreground color)
(4)最后打印出文本信息 Hello Colorful World

因此,它的语法应该是
"\033[escape-code    your-message"


参考:

ANSI escape sequences

http://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 follows1) First \033, is escape character, which causes to take some action
2) Here it set screen foreground color to Blue using [34m escape code.
3) Then it prints our normal message Hello Colorful World! in blue color.

2、实践

上面我们讲到了ANSI escape sequence,但是Eclipse的控制台并不支持ANSI escape sequence。
The Eclipse console does not support ANSI escape sequences.
为了让Eclipse支持ANSI escape sequence,需要引入一个插件: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 be pretty handy when using something like jansi


开发步骤
(1)安装ANSI Escape in Console。
(2)下载组件,添加jar包。
(3)配置
(4)使用API

2.1、安装ANSI Escape in Console
Help-->Eclipse Marketplace...



搜索ANSI Escape in Console,进行安装




安装成功后,重启IDE,打开Window-->Preferences查看。
在Preferences窗口的左侧,如果发现Ansi_Console,则表示安装成功。




2.2、下载组件,添加jar包
jar包下载地址
color-loggers-1.0.4.1.jarhttps://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

参考

java-color-loggers

https://github.com/mihnita/java-color-loggers

Color console logging for log4j and jdk

2.3、配置
在src目录下添加log4j.properties文件
log4j.rootLogger=debug, console, file

log4j.appender.console=com.colorlog.log4j.AnsiColorConsoleAppender
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p %c.%M() -%m%n
log4j.appender.console.FatalColour={esc}[1;35m
log4j.appender.console.ErrorColour={esc}[0;31m
log4j.appender.console.WarnColour ={esc}[0;33m
log4j.appender.console.InfoColour ={esc}[0;30m
log4j.appender.console.DebugColour={esc}[0;32m
log4j.appender.console.TraceColour={esc}[1;30m

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= ./logs/MyLog.log
log4j.appender.file.MaxFileSize=5KB
log4j.appender.file.MaxBackupIndex=100
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p %c.%M() -%m%n


2.4、使用API
package com.rk.test;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
@Test
public void test()
{
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.debug("debug信息");
logger.info("info信息");
logger.warn("warn信息");
logger.error("error信息");
}
}


输出




参考地址
让Eclipse/MyEclipse的控制台和log4j日志支持多种颜色
http://blog.csdn.net/javawinner/article/details/41548259

参考
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
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

Parameters 30 through 47 meet the ISO 6429 standard.

Parameters 30 through 47 meet the ISO 6429 standard.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java logging