| 1 | mlog |
| 2 | ==== |
| 3 | |
| 4 | [](https://github.com/cactus/mlog/actions) |
| 5 | [](https://godoc.org/github.com/cactus/mlog) |
| 6 | [](https://goreportcard.com/report/cactus/mlog) |
| 7 | [](https://github.com/cactus/mlog/blob/master/LICENSE.md) |
| 8 | |
| 9 | ## About |
| 10 | |
| 11 | A purposefully basic logging library for Go (`>= 1.16`). |
| 12 | |
| 13 | mlog only has 3 logging levels: Debug, Info, and Fatal. |
| 14 | |
| 15 | ### Why only 3 levels? |
| 16 | |
| 17 | Dave Cheney [wrote a great post][1], that made me rethink my own approach to |
| 18 | logging, and prompted me to start writing mlog. |
| 19 | |
| 20 | ### How does it work? |
| 21 | |
| 22 | Logging methods are: |
| 23 | |
| 24 | * `Debug` - conditionally (if debug is enabled) logs message at level |
| 25 | "debug". |
| 26 | * `Debugf` - similar to `Debug`, but supports printf formatting. |
| 27 | * `Debugm` - similar to `Debug`, but logs an mlog.Map as extra data. |
| 28 | * `Info` - logs message at level "info". `Print` is an alias for `Info`. |
| 29 | * `Infof` - similar to `Info`, but supports printf formatting. `Printf` is an |
| 30 | alias for `Infof`. |
| 31 | * `Infom` - similar to `Info`, but logs an mlog.Map as extra data. `Printm` |
| 32 | is an alias for `Infom`. |
| 33 | * `Fatal` - logs message at level "fata", then calls `os.Exit(1)`. |
| 34 | * `Fatalf` - similar to `Fatal`, but supports printf formatting. |
| 35 | * `Fatalm` - similar to `Fatal`, but logs an mlog.Map as extra data. |
| 36 | |
| 37 | That's it! |
| 38 | |
| 39 | For more info, check out the [docs][3]. |
| 40 | |
| 41 | ## Usage |
| 42 | |
| 43 | ``` go |
| 44 | import ( |
| 45 | "bytes" |
| 46 | |
| 47 | "github.com/cactus/mlog" |
| 48 | ) |
| 49 | |
| 50 | func main() { |
| 51 | mlog.Info("this is a log") |
| 52 | |
| 53 | mlog.Infom("this is a log with more data", mlog.Map{ |
| 54 | "interesting": "data", |
| 55 | "something": 42, |
| 56 | }) |
| 57 | |
| 58 | thing := mlog.Map( |
| 59 | map[string]interface{}{ |
| 60 | "what‽": "yup", |
| 61 | "this-works?": "as long as it is a mlog.Map", |
| 62 | }, |
| 63 | ) |
| 64 | |
| 65 | mlog.Infom("this is also a log with more data", thing) |
| 66 | |
| 67 | mlog.Debug("this won't print") |
| 68 | |
| 69 | // set flags for the default logger |
| 70 | // alternatively, you can create your own logger |
| 71 | // and supply flags at creation time |
| 72 | mlog.SetFlags(mlog.Ltimestamp | mlog.Ldebug) |
| 73 | |
| 74 | mlog.Debug("now this will print!") |
| 75 | |
| 76 | mlog.Debugm("can it print?", mlog.Map{ |
| 77 | "how_fancy": []byte{'v', 'e', 'r', 'y', '!'}, |
| 78 | "this_too": bytes.NewBuffer([]byte("if fmt.Print can print it!")), |
| 79 | }) |
| 80 | |
| 81 | // you can use a more classical Printf type log method too. |
| 82 | mlog.Debugf("a printf style debug log: %s", "here!") |
| 83 | mlog.Infof("a printf style info log: %s", "here!") |
| 84 | |
| 85 | // how about logging in json? |
| 86 | mlog.SetEmitter(&mlog.FormatWriterJSON{}) |
| 87 | mlog.Infom("something", mlog.Map{ |
| 88 | "one": "two", |
| 89 | "three": 3, |
| 90 | }) |
| 91 | |
| 92 | mlog.Fatalm("time for a nap", mlog.Map{"cleanup": false}) |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | Output: |
| 97 | |
| 98 | ``` |
| 99 | time="2016-04-29T19:59:11.474362716-07:00" level="I" msg="this is a log" |
| 100 | time="2016-04-29T19:59:11.474506079-07:00" level="I" msg="this is a log with more data" interesting="data" something="42" |
| 101 | time="2016-04-29T19:59:11.474523514-07:00" level="I" msg="this is also a log with more data" this-works?="as long as it is a mlog.Map" what‽="yup" |
| 102 | time="2016-04-29T19:59:11.474535676-07:00" msg="now this will print!" |
| 103 | time="2016-04-29T19:59:11.474542467-07:00" msg="can it print?" how_fancy="[118 101 114 121 33]" this_too="if fmt.Print can print it!" |
| 104 | time="2016-04-29T19:59:11.474551625-07:00" msg="a printf style debug log: here!" |
| 105 | time="2016-04-29T19:59:11.474578991-07:00" msg="a printf style info log: here!" |
| 106 | {"time": "2016-04-29T19:59:11.474583762-07:00", "msg": "something" "extra": {"one": "two", "three": "3"}} |
| 107 | {"time": "2016-04-29T19:59:11.474604928-07:00", "msg": "time for a nap" "extra": {"cleanup": "false"}} |
| 108 | exit status 1 |
| 109 | ``` |
| 110 | |
| 111 | ## License |
| 112 | |
| 113 | Released under the [ISC license][2]. See `LICENSE.md` file for details. |
| 114 | |
| 115 | [1]: http://dave.cheney.net/2015/11/05/lets-talk-about-logging |
| 116 | [2]: https://choosealicense.com/licenses/isc/ |
| 117 | [3]: https://godoc.org/github.com/cactus/mlog |