中文|English
ketty 是一个Golang 开发的简单的日志美化输出 Logger。
$ go install github.com/anqiansong/ketty@latest
默认 console 是输出到控制台的,如需要将文件存储到磁盘,请参考下文日志持久化
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"))
}
直接使用的 Console 实例支持一些默认配置项:
- 使用
frame.WithLineStyle
作为边框 - 默认美化日志
- 不会持久化到文件
func main(){
console.Info("Hello ketty, This is a info log")
console.Debug("Hello ketty, This is a debug log")
console.Warn("Hello ketty, This is a warn log")
console.Error(errors.New("Hello ketty,This is an error"))
}
// 替换默认的边框
plusStyle := text.WithPlusStyle()
c := console.NewConsole(console.WithTextOption(plusStyle))
c.DisableBorder() // 禁用边框
c.DisableColor() // 禁用颜色美化
// 输出 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 =----------------------------------------------------------------------------
你可以通过 WithOutputDir 指定一个日志输出目录,并通过 Flush 进行日志强行写入磁盘,默认情况下, ketty 会每秒钟自动去 Flush 一次。
c := NewConsole(WithOutputDir(dir))
// Don't forget to close it, otherwise the goroutine maybe overflow
defer c.Close()
// 为了防止日志文件急剧增大,可以关闭颜色美化和边框美化,减少不必要的日志输出到文件
c.DisableColor()
c.DisableBorder()
c.Info("It's now: %v", time.Now())
// 手动落盘
c.Flush()
Windows 不支持美化输出。