Standard Log Format

There isn’t a standard log format that spans all applications. There is a Common Log Format used by web servers, and there are logging frameworks that helps in the mechanics of producing logs. Unfortunately when it comes to composing the actual message that will be written out to log files the developer is left to his own devices.

Here are some best practices that I’ve found useful when adding logs to an application I’ve working on.

  • Each log event should be written to a single line.
  • Each log event should start with an event timestamp, preferably in UTC.
  • Include the log level in the log line.
  • Add a short description about the event that is being logged.
  • Included all pertinent data about the event in name=”value” pairs.
  • Quote all data values.
  • Use and log a transaction id as requests move through your applications.
  • Be consistent about field names used across applications.
  • Add a prefix to the field name to add context, for example for input or output data.
  • Mask out passwords, credit cards, and any other Personal Customer Information (PCI) data.
  • The log file name should include the application identifier and hostname.
  • Log files should be rotated out based on time and/or size.

Leave a Reply