How it works...
You can either initialize a logger and pass it around using log.NewLogger(), or use the log package level logger to log messages. The log file in this recipe does the former and error does the latter. It also shows when logging might make sense after an error has reached its final destination, otherwise it's likely that you'll log multiple times for one event.
There are a few issues with this approach. For one, you may have additional context in one of the intermediate functions, such as variables you'd like to log. Next, logging a bunch of variables can get messy and is confusing and difficult to read. The next recipe explores structured logging that provides flexibility in logging variables, and a later recipe will explore ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access