关于golang:Golang-美化日志输出Ketty

43次阅读

共计 3801 个字符,预计需要花费 10 分钟才能阅读完成。

Ketty

ketty 是一个 Golang 开发的简略的日志丑化输入 Logger。

Github

https://github.com/anqiansong/ketty

装置

$ go install github.com/anqiansong/ketty@latest

疾速开始

func main(){
    console.Info(`
    {
        "name":"Hello Ketty",
        "description":"a color logger",
        "author":"anqiansong",
        "category":"console",
        "github":"https://github.com/anqiansong/ketty",
        "useage":[
            "info",
            "debug"
        ]
    }`)
    console.Debug("Hello Ketty")
    console.Warn("Hello Ketty")
    console.Error(errors.New("error test"))
}

终端显示

Goland 显示


用法

间接应用

间接应用的 Console 实例反对一些默认配置项:

  • 应用 frame.WithLineStyle 作为边框
  • 默认丑化日志
func main(){console.Info("Hello ketty, This is info log")
    console.Debug("Hello ketty, This debug log")
    console.Warn("Hello ketty, This warn log")
    console.Error(errors.New("Hello ketty,This is an error"))
}

初始化

    // 替换默认的边框
    plusStyle := text.WithPlusStyle()
    c := console.NewConsole(console.WithTextOption(plusStyle))

Console 配置

    c.DisableBorder() // 禁用边框
    c.DisableColor() // 禁用色彩丑化 

打印 log

    // 输入 info 日志
    c.Info("Hello Ketty, It's now %q", time.Now())

边框款式

预设款式

  • WithLineStyle 默认款式

    [INFO] 2021-11-26 22:29:14.508 ┌────────────────────────────────────────────────────────────────────────────
    [INFO] 2021-11-26 22:29:14.508 │  Hello Ketty, It's now"2021-11-26 22:29:14.508085 +0800 CST m=+0.000229190"
    [INFO] 2021-11-26 22:29:14.508 └────────────────────────────────────────────────────────────────────────────
  • WithDotStyle

    [INFO] 2021-11-26 22:30:22.913 .............................................................................
    [INFO] 2021-11-26 22:30:22.913 .  Hello Ketty, It's now"2021-11-26 22:30:22.913678 +0800 CST m=+0.000199931"
    [INFO] 2021-11-26 22:30:22.913 .............................................................................
  • WithStarStyle

    [INFO] 2021-11-26 22:31:00.699 *****************************************************************************
    [INFO] 2021-11-26 22:31:00.699 *  Hello Ketty, It's now"2021-11-26 22:31:00.699094 +0800 CST m=+0.000186578"
    [INFO] 2021-11-26 22:31:00.699 *****************************************************************************
  • WithPlusStyle

    [INFO] 2021-11-26 22:31:26.952 +----------------------------------------------------------------------------
    [INFO] 2021-11-26 22:31:26.952 |  Hello Ketty, It's now"2021-11-26 22:31:26.952376 +0800 CST m=+0.000168647"
    [INFO] 2021-11-26 22:31:26.952 +----------------------------------------------------------------------------
  • WithFivePointedStarStyle

    [INFO] 2021-11-26 22:31:58.146 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    [INFO] 2021-11-26 22:31:58.146 ★  Hello Ketty, It's now"2021-11-26 22:31:58.146534 +0800 CST m=+0.000171850"
    [INFO] 2021-11-26 22:31:58.146 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  • WithDoubleLine

    [INFO] 2021-11-26 22:32:21.574 ╔════════════════════════════════════════════════════════════════════════════
    [INFO] 2021-11-26 22:32:21.574 ║  Hello Ketty, It's now"2021-11-26 22:32:21.573911 +0800 CST m=+0.000152572"
    [INFO] 2021-11-26 22:32:21.574 ╚════════════════════════════════════════════════════════════════════════════
  • DisableBorder

    [INFO] 2021-11-26 22:33:01.695   Hello Ketty, It's now"2021-11-26 22:33:01.695338 +0800 CST m=+0.000156150"

自定义款式

  • WithCommonBorder

    // 边框横向、众项、拐角均为一种符号
    plusStyle := text.WithCommonBorder("x")
    [INFO] 2021-11-26 22:34:01.437 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [INFO] 2021-11-26 22:34:01.437 x  Hello Ketty, It's now"2021-11-26 22:34:01.437286 +0800 CST m=+0.000153825"
    [INFO] 2021-11-26 22:34:01.437 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • WithBorder

      // arg1: 左上角符号
      // arg2: 左下角符号
      // arg3: 横向边框符号
      // arg4: 垂直边框符号
      plusStyle := text.WithBorder("=","=","-","|")
      c := console.NewConsole(console.WithTextOption(plusStyle))
    [INFO] 2021-11-26 22:37:38.409 =----------------------------------------------------------------------------
    [INFO] 2021-11-26 22:37:38.409 |  Hello Ketty, It's now"2021-11-26 22:37:38.408952 +0800 CST m=+0.000155037"
    [INFO] 2021-11-26 22:37:38.409 =----------------------------------------------------------------------------

日志长久化

TODO

注意事项

Windows 不反对丑化输入。

正文完
 0