Logging with the context package
This recipe will demonstrate a way to pass log fields between various functions. The Go pkg/context package is an excellent way to pass additional variables and cancelation between functions. This recipe will explore using this functionality to distribute variables between functions for logging purposes.
This style can be adapted to logrus or apex from the previous recipe. We'll use apex for this recipe.
Getting ready
Configure your environment according to these steps:
- Refer to the Getting ready section of the Handling errors and the Error interface recipe.
- Run the
go get github.com/apex/logcommand.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application create and navigate to the
chapter4/contextdirectory. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter4/context or use this as an exercise to write some of your own code.
- Create a file called
log.gowith the following content:
...