Chapter 17. Monitoring and troubleshooting errors with logging
This chapter covers
- Understanding the components of a log message
- Writing logs to multiple output locations
- Controlling log verbosity in different environments using filtering
- Using structured logging to make logs searchable
Logging is one of those topics that seems unnecessary, right up until the point when you desperately need it! There’s nothing more frustrating than finding an issue that you can only reproduce in production, and then discovering there are no logs to help you debug it.
Logging is the process of recording events or activities in an app and often involves writing a record to a console, a file, the Windows Event Log, or some other system. You can record anything in a log message, though there are generally two different types of message:
- Informational messages— A standard event occurred: a user logged in, a product was placed in a shopping cart, or a new post was created on a blogging app.
- Warning and errors— An error or unexpected condition occurred: a user had a negative total in the shopping cart, or an exception occurred.
Historically, a common problem with logging in larger applications was that each library and framework would generate logs in a slightly different format, if at all. When an error occurred in your app and you were trying to diagnose it, this inconsistency made it harder to connect the dots in your app to get the full picture and understand the problem.