doc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Package context provides several utilities for working with
  2. // golang.org/x/net/context in http requests. Primarily, the focus is on
  3. // logging relevant request information but this package is not limited to
  4. // that purpose.
  5. //
  6. // The easiest way to get started is to get the background context:
  7. //
  8. // ctx := context.Background()
  9. //
  10. // The returned context should be passed around your application and be the
  11. // root of all other context instances. If the application has a version, this
  12. // line should be called before anything else:
  13. //
  14. // ctx := context.WithVersion(context.Background(), version)
  15. //
  16. // The above will store the version in the context and will be available to
  17. // the logger.
  18. //
  19. // Logging
  20. //
  21. // The most useful aspect of this package is GetLogger. This function takes
  22. // any context.Context interface and returns the current logger from the
  23. // context. Canonical usage looks like this:
  24. //
  25. // GetLogger(ctx).Infof("something interesting happened")
  26. //
  27. // GetLogger also takes optional key arguments. The keys will be looked up in
  28. // the context and reported with the logger. The following example would
  29. // return a logger that prints the version with each log message:
  30. //
  31. // ctx := context.Context(context.Background(), "version", version)
  32. // GetLogger(ctx, "version").Infof("this log message has a version field")
  33. //
  34. // The above would print out a log message like this:
  35. //
  36. // INFO[0000] this log message has a version field version=v2.0.0-alpha.2.m
  37. //
  38. // When used with WithLogger, we gain the ability to decorate the context with
  39. // loggers that have information from disparate parts of the call stack.
  40. // Following from the version example, we can build a new context with the
  41. // configured logger such that we always print the version field:
  42. //
  43. // ctx = WithLogger(ctx, GetLogger(ctx, "version"))
  44. //
  45. // Since the logger has been pushed to the context, we can now get the version
  46. // field for free with our log messages. Future calls to GetLogger on the new
  47. // context will have the version field:
  48. //
  49. // GetLogger(ctx).Infof("this log message has a version field")
  50. //
  51. // This becomes more powerful when we start stacking loggers. Let's say we
  52. // have the version logger from above but also want a request id. Using the
  53. // context above, in our request scoped function, we place another logger in
  54. // the context:
  55. //
  56. // ctx = context.WithValue(ctx, "http.request.id", "unique id") // called when building request context
  57. // ctx = WithLogger(ctx, GetLogger(ctx, "http.request.id"))
  58. //
  59. // When GetLogger is called on the new context, "http.request.id" will be
  60. // included as a logger field, along with the original "version" field:
  61. //
  62. // INFO[0000] this log message has a version field http.request.id=unique id version=v2.0.0-alpha.2.m
  63. //
  64. // Note that this only affects the new context, the previous context, with the
  65. // version field, can be used independently. Put another way, the new logger,
  66. // added to the request context, is unique to that context and can have
  67. // request scoped varaibles.
  68. //
  69. // HTTP Requests
  70. //
  71. // This package also contains several methods for working with http requests.
  72. // The concepts are very similar to those described above. We simply place the
  73. // request in the context using WithRequest. This makes the request variables
  74. // available. GetRequestLogger can then be called to get request specific
  75. // variables in a log line:
  76. //
  77. // ctx = WithRequest(ctx, req)
  78. // GetRequestLogger(ctx).Infof("request variables")
  79. //
  80. // Like above, if we want to include the request data in all log messages in
  81. // the context, we push the logger to a new context and use that one:
  82. //
  83. // ctx = WithLogger(ctx, GetRequestLogger(ctx))
  84. //
  85. // The concept is fairly powerful and ensures that calls throughout the stack
  86. // can be traced in log messages. Using the fields like "http.request.id", one
  87. // can analyze call flow for a particular request with a simple grep of the
  88. // logs.
  89. package context