You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.2 KiB

  1. # Table Of Contents
  2. 1. [Logging](#logging)
  3. ## Logging
  4. - Avoid print formatting messages on loggers, prefer to use fields on the logger to encapsulate additional information
  5. ```go
  6. // Avoid This Style, it bakes too much information into an unformatted message
  7. log.Debugf("something special %s happened", somethingSpecial)
  8. // Prefer this style with fields
  9. log.WithFields(log.Fields{
  10. "somethingSpecial": somethingSpecial
  11. }).Debug("something special happened")
  12. ```
  13. - Pass loggers through the function chain as they become more specialized. This keeps extra fields on the loggers from
  14. higher contexts and preserves additional information captured during the process of getting to where the current
  15. line is being logged. The logger should be one of the first arguments defined for helper functions. Public functions
  16. should not require a logger to be passed, but may offer a similar method with a logger input.
  17. ```go
  18. func doSomething(logger *log.Entry, ...) {
  19. //does something
  20. logger.Debug("Something")
  21. specialLogger = logger.WithFields(log.Fields{
  22. "specialField", value
  23. })
  24. doSubSomething(specialLogger, ...)
  25. }
  26. func doSubSomething(logger *log.Entry, ...) {}
  27. ```