github.com/zooyer/miskit@v1.0.71/log/README.md (about) 1 # The Go Log 2 3 4 ![](https://travis-ci.org/boennemann/badges.svg?branch=master) ![](https://img.shields.io/badge/license-MIT-blue.svg) ![](https://img.shields.io/badge/godoc-reference-blue.svg) 5 6 Go Log is customizable modular log. 7 8 ![](https://github.com/golang/go/blob/master/doc/gopher/fiveyears.jpg?raw=true) 9 10 11 #### Download and Install 12 13 ```shell 14 go get github.com/zooyer/log 15 ``` 16 17 #### Features 18 19 - zero config, out of the box feature 20 - customizable modular 21 - high performance 22 23 ##### Example: [example/example.go](example/example.go) 24 25 1. zero config 26 27 ```go 28 package main 29 30 import ( 31 "github.com/zooyer/log" 32 ) 33 34 func main() { 35 // 0 configs 36 log.D("debug message") // 2019-10-28 23:07:56.369 DEBUG "debug message" 37 log.I("info message") // 2019-10-28 23:07:56.369 INFO "info message" 38 log.W("warning message") // 2019-10-28 23:07:56.369 WARNING "warning message" 39 log.E("error message") // 2019-10-28 23:07:56.369 ERROR "error message" 40 } 41 ``` 42 43 output: 44 45 ```shell 46 2019-10-28 23:07:56.369 DEBUG "debug message" 47 2019-10-28 23:07:56.369 INFO "info message" 48 2019-10-28 23:07:56.369 WARNING "warning message" 49 2019-10-28 23:07:56.369 ERROR "error message" 50 ``` 51 52 53 54 2. custom 55 56 ```go 57 package main 58 59 import ( 60 "time" 61 62 "github.com/zooyer/log" 63 ) 64 65 func main() { 66 // 1. create file rotating(default: size/time) 67 rotating := log.NewFileCountRotating(1024, 10) 68 69 // 2. create file recorder(default: terminal/file/network), custom formatter: json/text 70 recorder, err := log.NewFileRecorder("example.log", log.FormatJson, rotating) 71 if err != nil { 72 panic(err) 73 } 74 defer recorder.Close() 75 76 // 3. create logger, each level can be mapped to different recorder 77 logger := log.NewLogger() 78 logger.SetRecorder(recorder, "DEBUG", "INFO") 79 logger.SetDefault(recorder) 80 81 // 4. create log, logger's wrap 82 l := log.NewLog(logger) 83 l.Tag("id", "1001").Tag("type", "test").Debug("custom debug log") 84 l.Error("custom error log") 85 86 // 5. custom 87 var record = new(log.Record) 88 record.Time = time.Now() 89 record.Level = "record" 90 record.Message = "custom log" 91 record.Tag = make(log.Tag) 92 record.Tag["id"] = "1001" 93 record.Tag["type"] = "test" 94 logger.Record(record) 95 } 96 ``` 97 98 output: 99 100 ```shell 101 cat example.log 102 {"level":"DEBUG","message":"custom debug log","tag":{"id":"1001","type":"test"},"time":"2019-10-28 23:41:34.385"} 103 {"level":"ERROR","message":"custom error log","time":"2019-10-28 23:41:34.386"} 104 {"level":"record","message":"custom log","tag":{"id":"1001","type":"test"},"time":"2019-10-28 23:41:34.386"} 105 ``` 106 107