GithubHelp home page GithubHelp logo

ketty's Introduction

Ketty

Go codecov Go Report Card License

中文|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"))
}

终端显示

terminal

Goland 显示

idea1 idea1

用法

直接使用

直接使用的 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))

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 =----------------------------------------------------------------------------

日志持久化

你可以通过 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 不支持美化输出。

ketty's People

Contributors

kesonan avatar

Stargazers

文昭 avatar Xinran avatar ChipLin avatar  avatar found avatar 黑小马 avatar mochen avatar leopay avatar 天剑 avatar HuangDongliu avatar Alan avatar 颜洪毅 avatar  avatar Leo LI avatar  avatar xukgo avatar  avatar 侠客老王 avatar ... avatar  avatar

Watchers

 avatar

Forkers

pkafma-aon

ketty's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.