这是一个后续行动 How to optimize script for colorizing log files
这是我的规格:
[Level][Date and time][Module] - [Interesting part]
Level
,Date and time
和Module
Level
:
ERROR
- 红色的白色Interesting part
Module
:
DataSourceImpl
- 使用Interesting part
DbConnectionImpl
- 白色的颜色Interesting part
Handler
- 品红色的Interesting part
适用于:
BEGIN {
magenta = "\x1b[35m"; white = "\x1b[37m"; BLACK = "\x1b[30;1m"; onred = "\x1b[41m";
reset = "\x1b[0m"
color["DataSourceImpl"] = reset
color["DbConnectionImpl"] = white
color["Handler"] = magenta
}
match($0, /\[(.*)\]\[(.*)\]\[(.*)\] - \[(.*)\]/, a) {
if (a[1] == "ERROR" || a[1] == "WARN ")
{
print BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" reset white onred a[4] reset BLACK "]" reset
next
}
else if (a[3] == "DataSourceImpl" || a[3] == "DbConnectionImpl" || a[3] == "Handler")
$0 = BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" reset color[a[3]] a[4] reset BLACK "]" reset
else
$0 = BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" a[4] "]" reset
}
// {print $0}
在45 MB的日志文件中:
cat server.log | colorlog
需要15.277秒cat server.log
需要14.007 所以,它甚至足够快!几乎没有明显的差异......
现在,额外的规格:
突出显示SQL构造
SELECT .. FROM .. WHERE
黄色INSERT INTO ..
DELETE FROM ..
表格名称应突出显示在"粗体"颜色。注意 - SELECT
构造中使用的INSERT TO
个关键字(而不是VALUES
)
也可以用黄色突出显示。没有例外。
以青色突出显示字符串(引号之间)。
更新了脚本:
BEGIN {
magenta = "\x1b[35m"; white = "\x1b[37m"; BLACK = "\x1b[30;1m"; onred = "\x1b[41m";
yellow="\x1b[33m"; YELLOW="\x1b[33;1m"; cyan="\x1b[36m"
reset = "\x1b[0m"
color["DataSourceImpl"] = reset
color["DbConnectionImpl"] = white
color["Handler"] = magenta
}
match($0, /\[(.*)\]\[(.*)\]\[(.*)\] - \[(.*)\]/, a) {
if (a[1] == "ERROR" || a[1] == "WARN ")
{
print BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" reset white onred a[4] reset BLACK "]" reset
next
}
else if (a[3] == "DataSourceImpl" || a[3] == "DbConnectionImpl" || a[3] == "Handler")
$0 = BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" reset color[a[3]] a[4] reset BLACK "]" reset
else
$0 = BLACK "[" a[1] "][" a[2] "][" a[3] "] - [" a[4] "]" reset
}
match($0, /(.*)(SELECT|select)(.*)((FROM|from) ([^ )]*))([^\]]*)(WHERE|where)(.*)/, a) {
$0 = a[1] yellow a[2] reset a[3] yellow a[5] reset " " YELLOW a[6] reset yellow a[7] yellow a[8] reset a[9]
}
// { gsub(/'[^']*'/, cyan "&" reset); }
// {print $0}
这个不完整的脚本已经导致以下问题:
SELECT .. FROM .. WHERE
构造突出显示 - 和
我不知道如何避免这种情况(不像我为突出字符串所做的那样)
引用之间。SELECT
之后,colum
名称用默认前景色着色,不再是白色
(当模块为DbConnectionImpl
时)。这是因为"重置"颜色;
但我不知道如何添加颜色,但保留以前的颜色。有什么方法可以解决这些问题?