func main() { // Bind a new logger to a variable. You may create any number of loggers. ctx := kiwi.New() // For starting write ctx records to some writer output should be initialized. out := kiwi.SinkTo(os.Stdout, kiwi.UseLogfmt()).Start() // setup context of the logger ctx.With("userID", 1000, "host", "local", "startedAt", time.Now()) // This record will be supplemented by startedAt value of time.Now().String() ctx.Add("sample", 1).Log() // This record also will be supplemented by the same value of the time. // Because context value evalueted when it was added by ctx.With(). ctx.Add("sample", 2).Log() // You can provide deferred evaluation of context or ctx values if you add them wrapped // with func() interface{}, where interface should be one of scalar golang types. ctx.With("currentTime", func() string { return time.Now().String() }) // Get previously saved context for use in the application. // They were keep as is without conversion to strings. currentContext := ctx.GetContext() fmt.Printf("some of the context values are: %d, %s\n", currentContext["userID"], currentContext["host"]) // These records will be output each its own currentTime value because currentTime will // be evaluated on each Log() call. ctx.Add("sample", 3).Log() ctx.Add("sample", 4).Log() out.Flush() }
func BenchmarkLevelsKiwiTypedHelpers_JSON(b *testing.B) { buf := &bytes.Buffer{} b.ResetTimer() l := kiwi.New() l.With("_n", "bench", "_p", pid) l.WithTimestamp(time.RFC3339) kiwi.LevelName = "l" out := kiwi.SinkTo(buf, kiwi.UseJSON()).Start() for i := 0; i < b.N; i++ { l.AddPairs( kiwi.AsInt("key", 1), kiwi.AsFloat64("key2", 3.141592), kiwi.AsString("key3", "string"), kiwi.AsBool("key4", false)).Debug() l.AddPairs( kiwi.AsInt("key", 1), kiwi.AsFloat64("key2", 3.141592), kiwi.AsString("key3", "string"), kiwi.AsBool("key4", false)).Info() l.AddPairs( kiwi.AsInt("key", 1), kiwi.AsFloat64("key2", 3.141592), kiwi.AsString("key3", "string"), kiwi.AsBool("key4", false)).Warn() l.AddPairs( kiwi.AsInt("key", 1), kiwi.AsFloat64("key2", 3.141592), kiwi.AsString("key3", "string"), kiwi.AsBool("key4", false)).Error() } b.StopTimer() out.Close() }
func BenchmarkLevelsKiwiTypedHelpersComplex_JSON(b *testing.B) { buf := &bytes.Buffer{} b.ResetTimer() l := kiwi.New() l.With("_n", "bench", "_p", pid) l.WithTimestamp(time.RFC3339) kiwi.LevelName = "l" out := kiwi.SinkTo(buf, kiwi.UseJSON()).Start() for i := 0; i < b.N; i++ { l.AddPairs(kiwi.AsInt("key", 1), kiwi.AsStringer("obj", testObject)).Debug() l.AddPairs(kiwi.AsInt("key", 1), kiwi.AsStringer("obj", testObject)).Info() l.AddPairs(kiwi.AsInt("key", 1), kiwi.AsStringer("obj", testObject)).Warn() l.AddPairs(kiwi.AsInt("key", 1), kiwi.AsStringer("obj", testObject)).Error() } b.StopTimer() out.Close() }
func BenchmarkLevelsKiwiComplex_JSON(b *testing.B) { buf := &bytes.Buffer{} b.ResetTimer() l := kiwi.New() l.With("_n", "bench", "_p", pid) l.WithTimestamp(time.RFC3339) kiwi.LevelName = "l" out := kiwi.SinkTo(buf, kiwi.UseJSON()).Start() for i := 0; i < b.N; i++ { l.Debug("key", 1, "obj", testObject) l.Info("key", 1, "obj", testObject) l.Warn("key", 1, "obj", testObject) l.Error("key", 1, "obj", testObject) } b.StopTimer() out.Close() }
func BenchmarkLevelsKiwi_JSON(b *testing.B) { buf := &bytes.Buffer{} b.ResetTimer() l := kiwi.New() l.With("_n", "bench", "_p", pid) l.WithTimestamp(time.RFC3339) kiwi.LevelName = "l" out := kiwi.SinkTo(buf, kiwi.UseJSON()).Start() for i := 0; i < b.N; i++ { l.Debug("key", 1, "key2", 3.141592, "key3", "string", "key4", false) l.Info("key", 1, "key2", 3.141592, "key3", "string", "key4", false) l.Warn("key", 1, "key2", 3.141592, "key3", "string", "key4", false) l.Error("key", 1, "key2", 3.141592, "key3", "string", "key4", false) } b.StopTimer() out.Close() }
func main() { // Bind a new logger to a variable. You may create any number of loggers. log := kiwi.New() tmpFile, _ := os.Create("/tmp/something-completely-different.log") // You can set arbitrary number of outputs. // But they will remain unused until you explicitly start them with Start(). info := kiwi.SinkTo(os.Stdout, kiwi.UseLogfmt()) errors := kiwi.SinkTo(os.Stderr, kiwi.UseLogfmt()) something := kiwi.SinkTo(tmpFile, kiwi.UseLogfmt()) // Each record by default will copied to all outputs. // But until you Start() any output the records will just dropped as the sample record below. log.Add("just something that will lost") // Each output allows filter out any records and write any other. // You specify filter for the keys (key filter). // Each of these keys should be presented in the record. errors.WithKey("error", "msg") // The filter may take into account key values. So only records with levels // ERROR and FATAL will be passed filter and written to stderr. errors.WithValue("level", "ERROR", "FATAL").Start() // Vice versa you can filter out some keys. info.WithoutKey("error") // And define another set of key-val pairs for distinguish outputs. info.WithValue("level", "INFO", "WARNING").Start() // It will output all records from outputs above if they have key "something". // So you can duplicate some records to several log files based on some criteria. something.WithKey("something").Start() // So if you not define any clauses (WithKey/WithoutKey/WithValue/WithoutValues) // then all records will copied to an output. // Let's go! log.Add("level", "INFO", "sample-record", 1, "key", "value") log.Add("level", "INFO", "sample-record", 2, "something").Log() log.Add("level", "ERROR", "msg", "Error description.").Log() log.Add("level", "FATAL").Log() // Until you call Log() records not copied to outputs. log.Log() }
func main() { // Bind a new logger to a variable. You may create any number of loggers. log := kiwi.New() // For starting write log records to some writer output should be initialized. output := kiwi.SinkTo(os.Stdout, kiwi.UseLogfmt()).Start() log.Add("sample-record", 1, "key", "value") log.Log() // Most logger and output operations support chaining. log.Add("sample-record", 2, "key", "value", "key2", 123).Log() // On pause output will drop any incoming records. output.Stop() log.Add("this record will be dropped because single output we declared is on pause") output.Start() // You can explicitly remove output but it will automatically closed on application exit. output.Close() }
func main() { kiwi.SinkTo(os.Stdout, kiwi.UseLogfmt()).Start() l := kiwi.New() l.Add("sample-record", 1).Log() }