example_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package log_test
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. )
  6. // This example will logging a text to log file.
  7. func ExampleInfo() {
  8. fc := &log.Config{
  9. Family: "test-log",
  10. Dir: "/data/log/test",
  11. }
  12. log.Init(fc)
  13. defer log.Close()
  14. log.Info("test %s", "file log")
  15. ac := &log.Config{
  16. Family: "test-log",
  17. Agent: &log.AgentConfig{
  18. TaskID: "000003",
  19. Addr: "172.16.0.204:514",
  20. Proto: "tcp",
  21. Chan: 1024,
  22. },
  23. }
  24. log.Init(ac)
  25. defer log.Close()
  26. log.Info("test %s", "agent log")
  27. }
  28. // This example will logging a structured text to log agent.
  29. func ExampleInfov() {
  30. ac := &log.Config{
  31. Family: "test-log",
  32. Agent: &log.AgentConfig{
  33. TaskID: "000003",
  34. Addr: "172.16.0.204:514",
  35. Proto: "tcp",
  36. Chan: 1024,
  37. },
  38. }
  39. log.Init(ac)
  40. defer log.Close()
  41. log.Infov(context.TODO(), log.KV("key1", "val1"), log.KV("key2", "val2"))
  42. }
  43. // This example will set log format
  44. func ExampleSetFormat() {
  45. log.SetFormat("%L %T %f %M")
  46. log.Info("hello")
  47. // log output:
  48. // INFO 2018-06-28T12:15:48.713784 main.main:8 hello
  49. }