Golang 日志美化输出 - ketty - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
KesonAn
V2EX    Go 编程语言

Golang 日志美化输出 - ketty

  •  1
     
  •   KesonAn 2021-11-26 23:28:46 +08:00 2632 次点击
    这是一个创建于 1420 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    终端显示

    terminal

    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 23:24:51.826 ┌──────────── [INFO] 2021-11-26 23:24:51.826 │ Hello Ketty [INFO] 2021-11-26 23:24:51.826 └──────────── 
    • WithDotStyle
    [INFO] 2021-11-26 23:25:16.794 ............. [INFO] 2021-11-26 23:25:16.794 . Hello Ketty [INFO] 2021-11-26 23:25:16.794 ............. 
    • WithStarStyle
    [INFO] 2021-11-26 23:25:30.461 ************* [INFO] 2021-11-26 23:25:30.461 * Hello Ketty [INFO] 2021-11-26 23:25:30.461 ************* 
    • WithPlusStyle
    [INFO] 2021-11-26 23:25:45.736 +------------ [INFO] 2021-11-26 23:25:45.736 | Hello Ketty [INFO] 2021-11-26 23:25:45.736 +------------ 
    • WithFivePointedStarStyle
    [INFO] 2021-11-26 23:26:02.382 ★★★★★★★★★★★★★ [INFO] 2021-11-26 23:26:02.382 ★ Hello Ketty [INFO] 2021-11-26 23:26:02.382 ★★★★★★★★★★★★★ 
    • WithDoubleLine
    [INFO] 2021-11-26 23:26:18.008 [INFO] 2021-11-26 23:26:18.008 Hello Ketty [INFO] 2021-11-26 23:26:18.008 
    • 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 23:26:44.162 xxxxxxxxxxxxx [INFO] 2021-11-26 23:26:44.162 x Hello Ketty [INFO] 2021-11-26 23:26:44.162 xxxxxxxxxxxxx 
    • WithBorder
     // arg1: 左上角符号 // arg2: 左下角符号 // arg3: 横向边框符号 // arg4: 垂直边框符号 plusStyle := text.WithBorder("=","=","-","|") c := console.NewConsole(console.WithTextOption(plusStyle)) 
    [INFO] 2021-11-26 23:26:59.321 =------------ [INFO] 2021-11-26 23:26:59.321 | Hello Ketty [INFO] 2021-11-26 23:26:59.321 =------------ 

    日志持久化

    TODO

    注意事项

    Windows 不支持美化输出。

    8 条回复    2021-11-27 12:08:37 +08:00
    tiedan
        1
    tiedan  
       2021-11-27 00:34:43 +08:00
    实用性略差
    EvaCcino
        2
    EvaCcino  
       2021-11-27 01:00:05 +08:00
    日志系统杀手……
    jasonkayzk
        3
    jasonkayzk  
       2021-11-27 09:27:51 +08:00
    这种打到 elk 有些行不就是 ★★★★★★★★★★★★★
    joesonw
        4
    joesonw  
       2021-11-27 10:44:49 +08:00 via iPhone
    建议可以兼容不同的其他 logger 。这种格式毕竟也还是开发阶段用的多,线上不太适合采集分析。
    codeMore
        5
    codeMore  
       2021-11-27 11:37:10 +08:00
    日志量是不是扩大了一倍?
    KesonAn
        6
    KesonAn  
    OP
       2021-11-27 12:05:22 +08:00
    到线上可以禁用美化输出,这个的初衷主要是本地集成调式或者一些工具开发使用
    KesonAn
        7
    KesonAn  
    OP
       2021-11-27 12:08:05 +08:00
    @tiedan 本地调试可以用用
    KesonAn
        8
    KesonAn  
    OP
       2021-11-27 12:08:37 +08:00
    @joesonw Right!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1210 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 23:47 PVG 07:47 LAX 16:47 JFK 19:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86