一个go 日志插件没用sync.Mutex

自己实现的一个log
至少符合我的习惯。使用简单, 当然别人的使用方式也不难。 它可以非常细腻的控制,定制化。
名字和golang/glog 重复了。这点也不用在意。至少它的风格和我们不符。我也是学习了它的源码,经过自己的构思。
没有用sync.Mutex. 使用可以支持并发写入的环形队列。

https://github.com/slclub/glog

glog

golang logger manager

Summary

Log plug-ins that can be customized appropriately. Support concurrent logging.We used the ring queue and sync.Pool .
and use queue instead of lock(sync.Mutex) write security. Easy to use. Customized. Log files can be customized by size and date. Both supported.

Install

go get github.com/slclub/glog

go mod

Let's starts.

If you don't want set your path, debug, time and so on. It can still be used

  • Import

import "github.com/slclub/glog"

  • API

Like fmt.Pringln

  glog.Info("[HELLO][WORLD]", "MY FIST START", "PID[", int , "]")
  glog.Debug("[HELLO][WORLD]", "MY FIST START", "PID[", int , "]")
  glog.Warn("[HELLO][WORLD]", "MY FIST START", "PID[", int , "]")
  glog.Error("[HELLO][WORLD]", "MY FIST START", "PID[", int , "]")
  glog.Fatal("[HELLO][WORLD]", "MY FIST START", "PID[", int , "]")

  • An example.
func concurrencyLog(send_time int) {
    for i := 0; i < send_time; i++ {
        glog.Debug("testing something.")
        glog.Info("Oh my god. you are so clever.")
        glog.Warnning("an waring log!")
    }   
    fmt.Println("[PRINT][FINISH]")
}

func main() {
    glog.Set("path", "", "mylog")
    go concurrencyLog(1000)
    go concurrencyLog(1000)
    time.Sleep(1800 * time.Second)

}


  • Log default style. before you customized
 2020-05-20 00:20:38 INFO  Oh my god. you are so clever.
 2020-05-20 00:20:38 WARN  an waring log!
 2020-05-20 00:20:38 DEBUG testing something.
 2020-05-20 00:20:38 INFO  Oh my god. you are so clever.
 2020-05-20 00:20:38 WARN  an waring log!
 2020-05-20 00:20:38 DEBUG testing something.
 2020-05-20 00:20:38 INFO  Oh my god. you are so clever.

Customized

It's actually a function

Set(field string, value ...interface{})

In you code maybe like glog.Set(xxx, xxx)

  • Log file path setting.
// log file abs path and relative path. can both set them.
// In fact, the two paths correspond to the project path and its path respectively
// You can choose only one of them to set.
Set("path", "abs_path", "rel_path")
  • Log name prefix
// We offen  concatenated log name  with string and time and random number.
// here you just set the string part of the name. 
Set("name", "kawayi")
  • Log time settings
// Need to hide the time of each log line 
// false : hidden, true: show ;  
// default format : 2020-50-19 00:00:00
Set("show_time", false)
  • Log head info
// Log files are changed. when a new file was created. this string you added will be set to top of file.
Set("head", "auth@kawayi\nBbegin a new log every day\n")
  • Log debug settings.
// false: Hide data printed by a Debug method. true: show all
Set("debug", false)
// The second param also can be a number. The permission setting here is designed by bit calculation
// Add and subtract are supported for these numbers. You can use addition and subtraction to control permissions
// Very delicate to control
Set("debug", int)

const (
    // LEVEL
    LEVEL_INFO     = 1 
    LEVEL_DEBUG    = 2 
    LEVEL_WARNNING = 4 
    LEVEL_ERROR    = 8 
    LEVEL_FATAL    = 16

    TRACE_INFO     = 32
    TRACE_DEBUG    = 64
    TRACE_WARNNING = 128 
    TRACE_ERROR    = 256 
    TRACE_FATAL    = 512 
)

共 0 个回复