一个go 日志插件

slclub · · 1254 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

自己实现的一个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 ```go 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. ```go 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 ```go 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. ```go // 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 ```go // 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 ```go // 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 ```go // 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. ```go // 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 ) ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1254 次点击  
加入收藏 微博
3 回复  |  直到 2020-06-01 17:29:28
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传