-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add log level prefix color #1312
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #1312 +/- ##
===========================================
+ Coverage 69.62% 70.78% +1.15%
===========================================
Files 372 372
Lines 26621 32046 +5425
===========================================
+ Hits 18536 22683 +4147
- Misses 6531 7770 +1239
- Partials 1554 1593 +39
Continue to review full report at Codecov.
|
os/glog/glog_logger_level.go
Outdated
@@ -29,6 +29,19 @@ const ( | |||
LEVEL_FATA // 1024 | |||
) | |||
|
|||
type logColor int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要额外再定义一个类型,简单点使用int
即可,越简单越好。
os/glog/glog_logger.go
Outdated
@@ -43,6 +43,7 @@ const ( | |||
defaultFilePerm = os.FileMode(0666) | |||
defaultFileExpire = time.Minute | |||
pathFilterKey = "/os/glog/glog" | |||
mustWithColor = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要使用额外的常量定义,固定颜色控制规则:
- 终端输出固定会输出颜色,开发者不能控制。
- 文件输出日志依靠开发者提供的配置项控制。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是为了同时兼容文件和控制台输出,所以此处需要有控制选项
os/glog/glog_logger_chaining.go
Outdated
@@ -245,3 +245,14 @@ func (l *Logger) Async(enabled ...bool) *Logger { | |||
} | |||
return logger | |||
} | |||
|
|||
func (l *Logger) Color(color logColor) *Logger { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 方法注释。
- 类型使用
int
。
os/glog/glog_logger_config.go
Outdated
@@ -42,6 +42,8 @@ type Config struct { | |||
RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expire for rotated files, which is 0 in default, means no expiration. | |||
RotateBackupCompress int `json:"rotateBackupCompress"` // Compress level for rotated files using gzip algorithm. It's 0 in default, means no compression. | |||
RotateCheckInterval time.Duration `json:"rotateCheckInterval"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. | |||
FileColor bool `json:"fileColor"` // Logging level prefix with color or not (false in default). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议使用类似FileColorEnabled
见名知意、知类型的属性名称。
os/glog/glog_logger_config.go
Outdated
@@ -42,6 +42,8 @@ type Config struct { | |||
RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expire for rotated files, which is 0 in default, means no expiration. | |||
RotateBackupCompress int `json:"rotateBackupCompress"` // Compress level for rotated files using gzip algorithm. It's 0 in default, means no compression. | |||
RotateCheckInterval time.Duration `json:"rotateCheckInterval"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. | |||
FileColor bool `json:"fileColor"` // Logging level prefix with color or not (false in default). | |||
color logColor `json:"-"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一个简单的color
不太好理解,建议使用currentColor
或者customColor
名称方便维护。
os/glog/glog_logger_config.go
Outdated
@@ -252,3 +254,7 @@ func (l *Logger) SetPrefix(prefix string) { | |||
func (l *Logger) SetHandlers(handlers ...Handler) { | |||
l.config.Handlers = append(handlers, defaultHandler) | |||
} | |||
|
|||
func (l *Logger) SetColor(color logColor) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个方法要删掉,不能全局设置固定颜色。
os/glog/glog_logger_handler.go
Outdated
@@ -42,11 +43,15 @@ func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, s string) { | |||
buffer.WriteString(s) | |||
} | |||
|
|||
func (i *HandlerInput) Buffer() *bytes.Buffer { | |||
func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
固定颜色控制规则,不需要额外参数控制,见上面的review
说明。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是为了同时兼容文件和控制台输出,所以此处需要有控制选项
os/glog/glog_logger_handler.go
Outdated
if i.logger.config.color != 0 { | ||
color = i.logger.config.color | ||
} | ||
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样硬编码似乎不支持windows
平台,建议使用第三方颜色包:https://github.com/fatih/color
* develop-up: (38 commits) revert gerror usage for package gvalid unify error package to gerror add CtxStrict feature for package gdb rename constants of package gpage from skake to upper camel case add context for intlog improve Record/Result converting for package gdb add context for intlog/gsession;improve struct/structs converting for package gconv unify command or enviroment key names for packages improve details for package glog add file configuration support for logger in ghttp.Server version updates add more unit testing cases for package ghttp add automatic fields mapping and filtering for Model.Where statements improve Order feature for package gdb improve function Increment/Decrement for package gdb add auto fields filtering feature for function Scan of package gdb; mark funtcion Struct/Structs deprecated for gdb.Model improve record converting for package gdb improve transaction feature for package gdb improve caller path filtering for package gdebug improve caller path filtering for package gdebug ... # Conflicts: # os/glog/glog_logger.go # os/glog/glog_logger_config.go # os/glog/glog_logger_level.go
COLOR_BLUE | ||
COLOR_MAGENTA | ||
COLOR_CYAN | ||
COLOR_WHITE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
COLOR_YELLOW | ||
COLOR_BLUE | ||
COLOR_MAGENTA | ||
COLOR_CYAN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
COLOR_GREEN | ||
COLOR_YELLOW | ||
COLOR_BLUE | ||
COLOR_MAGENTA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
COLOR_RED | ||
COLOR_GREEN | ||
COLOR_YELLOW | ||
COLOR_BLUE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
COLOR_BLACK = 30 + iota | ||
COLOR_RED | ||
COLOR_GREEN | ||
COLOR_YELLOW |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
const ( | ||
COLOR_BLACK = 30 + iota | ||
COLOR_RED | ||
COLOR_GREEN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
@@ -28,6 +29,17 @@ const ( | |||
LEVEL_FATA // 1024 | |||
) | |||
|
|||
const ( | |||
COLOR_BLACK = 30 + iota | |||
COLOR_RED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
@@ -28,6 +29,17 @@ const ( | |||
LEVEL_FATA // 1024 | |||
) | |||
|
|||
const ( | |||
COLOR_BLACK = 30 + iota |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use ALL_CAPS in Go names; use CamelCase
exported const COLOR_BLACK should have comment (or a comment on this block) or be unexported
No description provided.