logger_test.go 610 B

12345678910111213141516171819202122232425
  1. package logger
  2. import (
  3. "context"
  4. "testing"
  5. )
  6. func TestLogger(t *testing.T) {
  7. l := NewLogger(WithLevel(TraceLevel), WithName("test"))
  8. h1 := NewHelper(l).WithFields(map[string]interface{}{"key1": "val1"})
  9. h1.Trace("trace_msg1")
  10. h1.Warn("warn_msg1")
  11. h2 := NewHelper(l).WithFields(map[string]interface{}{"key2": "val2"})
  12. h2.Trace("trace_msg2")
  13. h2.Warn("warn_msg2")
  14. h3 := NewHelper(l).WithFields(map[string]interface{}{"key3": "val4"})
  15. h3.Info("test_msg")
  16. ctx := context.TODO()
  17. ctx = context.WithValue(ctx, &loggerKey{}, h3)
  18. v := ctx.Value(&loggerKey{})
  19. ll := v.(*Helper)
  20. ll.Info("test_msg")
  21. }