Zero allocated, structured, leveled and pretty Golang logger
Intro | Benchmarks | Usage | Installation | License
Code:
// Here we are, just creating our new logger with a default 'formatter'.
// We'll talk about formatters letter on Usage section...
logger := kek.NewLogger("logger", "name")
logger.Debug.Println("And that is it!")
Output:
logger.name | debug 2019/7/28 20:54:40.683859000 [4666]: And that is it!
Machine:
Intel Xeon E3-1280 v6
Cores: 4x 3.90 GHz (Single Quad Core)
32 GB RAM
2x 1 TB SATA 7.2k RPM (Software RAID 1)
Ubuntu 18.04 (Bionic Beaver) LTS minimal [64bit]
Uptime 1 day
Results in average from 15 runs:
BenchmarkLogger_PrintlnWithEmptyWriter-8 1530000 760 ns/op 0 B/op 0 allocs/op
BenchmarkLogger_PrintlnWithStdoutWriter-8 590000 2010 ns/op 0 B/op 0 allocs/op
BenchmarkLogger_PrintTMithEmptyWriter-8 1450000 815 ns/op 0 B/op 0 allocs/op
BenchmarkLogger_PrintTMithStdoutWriter-8 570000 2050 ns/op 0 B/op 0 allocs/op
BenchmarkLogger_PrintTMWithEmptyWriter-8 1350000 870 ns/op 0 B/op 0 allocs/op
BenchmarkLogger_PrintTMWithStdoutWriter-8 550000 2100 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/olehan/kek/benchmarks 9s
Aight. If you're staying with me to see more complex usage of this lib - go into the examples folder or keep reading.
These are things that help you to populate logger with a specific configuration that is going to be 'linked' to a logger or just copied, so modification to the factory won't affect on other loggers.
factory := kek.NewFactory(
// First arg is an io.Writer
os.Stdout,
// And the second is the formatter. Don't worry, we've got some
// formatter packed up within the lib. But if you want, you
// can create your formatter and give your logs
// a new life.
sugared.Formatter,
)
// Now let's modify this factory a little bit.
factory.
SetWithColors(true).
SetWithDate(false).
SetWithMutex(true)
// Alright, now our logger have extended factory configurations.
logger := factory.NewLogger()
// And even can modify his own config, not affecting the factory.
logger.SetWithDate(true)
// But for those who wants to link loggers coming out of your
// factory, we've got NewLinkedLogger func.
// It returns the same logger, except its configurations are linked
// to the factory.
linkedLogger := factory.NewLinkedLogger()
// So any change made for the factory is going to affect a linked
// logger.
factory.SetWithNS(false)
The logger is a thing that you use to write logs into a writer.
To use them you need to create a new one from the factory to extend
already specified configs or just use default configs by using the
public NewLogger(name ...string) *Logger
function.
key.NewLogger().Info.Println("yeah")
Oke, now I know that there is a lot of projects that need a specific log format and for that purposes to log stuff, you'll need a formatter that will control every log. There is a set of configurable (or not) default formatters made to save your time.
Code:
minified := kek.NewLogger("minified").SetFormatter(minified.Formatter)
sugared := kek.NewLogger("sugared").SetFormatter(sugared.Formatter)
minified.Debug.Println("yeah")
sugared.Info.Println("YEAAAH")
Output:
minified - debug: yeah
sugared | info 2019/7/28 21:27:27.788431000 [4715]: YEAAAH
Every logger can have a name and a factory can also have a prefix for a logger. This functionality is not necessary, but I should say that is pretty useful when you need to separate loggers to get the context of a process.
Code:
factory := kek.NewFactory("service")
// Oh, and there is also a name tabulation, but it will take affect
// depends on your formatter.
factory.SetNameTabulation(true)
userPostsLogger := factory.NewLogger("user", "posts")
userCommentsLogger := factory.NewLogger("user", "comments")
// If a property 'WithColors' is enabled, you can set a random color
// for the loggers name.
userPostsLogger.SetRandomNameColor()
userPostsLogger.Debug.Println("Post edited")
userCommentsLogger.Warn.Println("Comments are refreshed")
Output:
# Welp, to see the colors - head over to the examples folder.
service/user.posts | debug 2019/7/28 21:27:27.788431000 [4715]: Post edited
service/user.comments | warn 2019/7/28 21:27:27.788521000 [4715]: Comments are refreshed
The printer is the function that handles the job of executing a formatter and pool (π°) management. For a moment of this documentation kek delivers 3 types of printers:
- Base - the only printer that you've seen so far (
Print
,Println
) - Template - a printer that takes a format of a log and inserts values into it
(
PrintT
,PrintTM
,PrintTKV
) - Structured - a printer that takes a message and writes key-value pairs in a specific structured way (
PrintSKV
)
Code:
logger := kek.NewLogger()
logger.Debug.
PrintT("wassup, {} - {}", "boi", 123).
PrintTM("wassup, NewMap{{name}}, Age: {{age}}",
ds.NewMap().
Set("name", "Boi").
Set("age", 17),
).
PrintTM("wassup, Map{{name}}", ds.Map{
"name": "Boi",
}).
PrintTKV("wassup, KeyValue{{name}} {{34}} {{134}} - {{unknown}}",
"name", "Boi",
"34", true,
// Skips the key that is not a string.
134, "ooh",
// Skips the key value pair without a value.
"unknown",
)
logger.Info.
PrintSKV("message for key values",
"key1", "value1",
"key2", 242456246,
"key3", true,
"key4", 135135.13413,
)
Output:
debug 2019/7/28 21:53:20.501202000 [4775]: wassup, boi - 123
debug 2019/7/28 21:53:20.501296000 [4775]: wassup, NewMapBoi, Age: 17
debug 2019/7/28 21:53:20.501300000 [4775]: wassup, MapBoi
debug 2019/7/28 21:53:20.501305000 [4775]: wassup, KeyValueBoi true {{134}} - {{unknown}}
info 2019/7/28 21:56:20.501355000 [4811]: message for key values
> key1: value1
> key2: 242456246
> key3: true
> key4: 135135.13413
go get github.com/olehan/kek
It's MIT. What else would you expect? π