Color Printing

It can enhance the visibility of logs. When printing logs, the error level text is highlighted by adding font colors.

Effect Example

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func main() {
  7. ctx := context.TODO()
  8. g.Log().Debug(ctx, "Debug")
  9. g.Log().Info(ctx, "Info")
  10. g.Log().Notice(ctx, "Notice")
  11. g.Log().Warning(ctx, "Warning")
  12. g.Log().Error(ctx, "Error")
  13. }

Logging - Color Printing - 图1

Using Configuration

The console will naturally have color output, and file logs do not have colors by default.

If you need logs in files to also have colors, you can add configuration in the configuration file.

  1. logger:
  2. stdoutColorDisabled: false # Whether to disable terminal color printing. Default is no, which means terminal color output is enabled.
  3. writerColorEnable: false # Whether to enable Writer color printing. Default is no, which means no color output to custom Writers or files.

You can also add it in the code

  1. g.Log().SetWriterColorEnable(true)

The effect is as follows (the red box shows the effect after adding file log color, while the other part shows the effect when it is not enabled)

Logging - Color Printing - 图2

Default Color Correspondence Table

By default, different log levels correspond to the following colors:

  1. // \v2\os\glog\glog_logger_color.go
  2. var defaultLevelColor = map[int]int{
  3. LEVEL_DEBU: COLOR_YELLOW,
  4. LEVEL_INFO: COLOR_GREEN,
  5. LEVEL_NOTI: COLOR_CYAN,
  6. LEVEL_WARN: COLOR_MAGENTA,
  7. LEVEL_ERRO: COLOR_RED,
  8. LEVEL_CRIT: COLOR_HI_RED,
  9. LEVEL_PANI: COLOR_HI_RED,
  10. LEVEL_FATA: COLOR_HI_RED,
  11. }