Skip to content

Files

Latest commit

ae31795 · Aug 9, 2018

History

History
83 lines (67 loc) · 2.01 KB

2018-08-09-log-color-in-go.md

File metadata and controls

83 lines (67 loc) · 2.01 KB

2018-08-09

来源:《Go 夜读》微信群

时间:2018-08-09

问题:Log 的颜色能设置吗?

答:可以的。

func TestLogColor(t *testing.T) {
	fmt.Println("")

	// | 前景 | 背景 | 颜色 |
	// |----|----|----|
	// | 30 | 40 | 黑色 |
	// | 31 | 41 | 红色 |
	// | 33 | 42 | 绿色 |
	// | 33 | 43 | 黄色 |
	// | 34 | 44 | 蓝色 |
	// | 35 | 45 | 紫红色 |
	// | 36 | 46 | 青蓝色 |
	// | 37 | 47 | 白色 |
	//
	// | 代码 | 意义 |
	// |----|----|
	// | 0 | 终端默认设置 |
	// | 1 | 高亮显示 |
	// | 4 | 使用下划线 |
	// | 5 | 闪烁 |
	// | 7 | 反白显示 |
	// | 8 | 不可见 |

	for b:= 40; b<=47; b++ {
		for f:= 30; f <= 37; f++ {
			for d:= range []int{0, 1, 4, 5, 7, 8} {
				fmt.Printf(" %c[%d;%d;%dm%s(f=%d,b=%d,d=%d)%c[0m ", 0x1B, d, b, f, "", f, b, d, 0x1B)
			}
			fmt.Println("")
		}
		fmt.Println("")
	}

	fmt.Printf("%c[1;40;32m%s%c[0m", 0x1B, "testPrintColor", 0x1B)
	fmt.Println()
	fmt.Printf("%s\n", "testPrintColor")
}
$ go test -v log_color_test.go

对于以上代码的解释:

在其中 \x1b[ 实现 CSI: 转换前景色为黑色,使用 \x1b[30m 转换为红色,使用 \x1b[31m 如果使用加粗参数,灰色写作 \x1b[30;1m 获取红色加粗,使用 \x1b[31;1m 重置颜色为缺省值,使用 \x1b[39;49m (或者使用 \x1b[0m 重置所有属性)

\033[0m 重置为正常 
\033[1m 设置高亮度或加粗 
\033[4m 下划线 
\033[5m 闪烁 
\033[7m 反显 
\033[8m 消隐 
\033[30m -- /33[37m 设置前景色 
\033[40m -- /33[47m 设置背景色 

控制符 ESC 的常用表示方法 \e\x1b(\x1B)\033 都可以 \e 指代 Escape,对应八进制 \033,对应十六进制 \x1b

摘自于教你写一个color日志库,不止有代码还有原理。

参考

  1. https://en.wikipedia.org/wiki/ANSI_escape_code
  2. https://github.com/fatih/color