Skip to content
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

Merged
merged 8 commits into from
Jul 15, 2021
Merged

add log level prefix color #1312

merged 8 commits into from
Jul 15, 2021

Conversation

wangle201210
Copy link
Member

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Jun 27, 2021

Codecov Report

Merging #1312 (30dbccf) into develop (2a350fd) will increase coverage by 1.15%.
The diff coverage is 20.51%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ
os/glog/glog_logger_chaining.go 56.94% <0.00%> (+0.38%) ⬆️
os/glog/glog_logger_config.go 57.84% <ø> (+1.26%) ⬆️
os/glog/glog_logger_level.go 61.90% <ø> (+8.05%) ⬆️
os/glog/glog_logger_handler.go 56.14% <25.00%> (-28.48%) ⬇️
os/glog/glog_logger.go 74.53% <33.33%> (+1.43%) ⬆️
net/ghttp/ghttp_request_view.go 38.88% <0.00%> (-7.27%) ⬇️
os/glog/glog.go 42.85% <0.00%> (-7.15%) ⬇️
os/gsession/gsession_manager.go 68.00% <0.00%> (-5.69%) ⬇️
encoding/gjson/gjson_implements.go 81.81% <0.00%> (-5.69%) ⬇️
os/gsession/gsession_session.go 45.50% <0.00%> (-5.26%) ⬇️
... and 366 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a350fd...30dbccf. Read the comment docs.

@@ -29,6 +29,19 @@ const (
LEVEL_FATA // 1024
)

type logColor int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要额外再定义一个类型,简单点使用int即可,越简单越好。

@@ -43,6 +43,7 @@ const (
defaultFilePerm = os.FileMode(0666)
defaultFileExpire = time.Minute
pathFilterKey = "/os/glog/glog"
mustWithColor = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要使用额外的常量定义,固定颜色控制规则:

  1. 终端输出固定会输出颜色,开发者不能控制。
  2. 文件输出日志依靠开发者提供的配置项控制。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是为了同时兼容文件和控制台输出,所以此处需要有控制选项

@@ -245,3 +245,14 @@ func (l *Logger) Async(enabled ...bool) *Logger {
}
return logger
}

func (l *Logger) Color(color logColor) *Logger {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 方法注释。
  2. 类型使用int

@@ -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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议使用类似FileColorEnabled见名知意、知类型的属性名称。

@@ -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:"-"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个简单的color不太好理解,建议使用currentColor或者customColor名称方便维护。

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法要删掉,不能全局设置固定颜色。

@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

固定颜色控制规则,不需要额外参数控制,见上面的review说明。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是为了同时兼容文件和控制台输出,所以此处需要有控制选项

if i.logger.config.color != 0 {
color = i.logger.config.color
}
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, s)
Copy link
Member

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@gqcn gqcn merged commit 860b22a into gogf:develop Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants