10 Logging best practices

 

This chapter covers:

  • Applying log levels to help filter and prioritize actions,
  • Identifying characteristics of good logs,
  • Making operational activities easier with good logs,
  • Impact of legislation upon logging,
  • Coding practices for improved logging,

Irrespective of how log entries are generated, whether that is applications writing to stdout, stderr, OS event frameworks, or logging frameworks they all benefit from some core practices that will make the log event more useful, meaningful, and easier to consume.

We will explore what should and should not be logged, and the implication of logging in terms of security and legislative requirements.

10.1   Audit events vs log events

Figure 10.1 Venn diagram showing the relationship between logging and auditing.
10_01

When is an event an audit event, and when is it a log event? Let’s start with defining what the two events are.

  • Audit events are typically a record of an action, event, or data state that needs to be retained to provide a formal record that may be required at some future point to help resolve an issue of compliance (such as accounting processes or security).
  • Log events are a record of something that has occurred, the log event will be provided for a technical reason which may range from showing how a transaction has been handled to reporting unexpected circumstances to show how code is executing.

10.2   Log levels aka Severities

 
 
 
 

10.2.1    Trace

 
 
 
 

10.2.2    Debug

 
 
 

10.2.3    Info

 
 

10.2.4    Warn

 
 
 

10.2.5    Error

 
 
 

10.2.6    Fatal

 

10.2.7    Extending or creating your own log levels

 
 
 
 

10.3   Clear Language

 
 

10.4   Human and Machine-readable

 

10.5   Context is key

 
 
 
 

10.6   Error Codes

 
 
 
 

10.6.1    Using standard errors

 
 

10.6.2    Codes can be for more than errors

 
 

10.7   Too little logging or too much?

 
 
 

10.7.1    What qualifies as sensitive?

 
 
 

10.7.2    GDPR is only the start …

 

10.8   Log structure and Format

 
 

10.9   Scenario - Making log entries ready for application shipping

 
 
 

10.9.1    Making log entries ready for application shipping – Solution

 
 
 

10.10    Use frameworks if you can

 
 

10.11    Development Practices

 

10.11.1    Rethrowing Exceptions

 

10.11.2    Using Standard Exceptions

 

10.12    Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest