Log me now

Forum PHP 2017

Kévin Gomez / @KPhoen

Press [s] to read the speaker notes (will open a new window).

We will talk about logs…

  • … even if it's not “trendy
  • … because they are valuable
  • … and often used inconsistently

How can we write useful logs?

Definition: to log

“to make an official record of things that happen”
Macmillan Dictionary

Logging strategies

  • performance and resources
  • errors
  • user actions

Log, log, log!

Ensure that all the relevant events are logged!

… relevant for who and why?

Log messages

Logs should be explicit.


Order "REF-42" failed: payment gateway unreachable.
                

Chronological logs

Logs should be timestamped.


Order "REF-42" failed on 12/12/17 at 14h30: payment gateway unreachable.
                

Human readable, machine parseable

Logs should be human readable and unambiguously parseable.


[2017-12-12T14:30:51.965Z] Order "REF-42" failed on 12/12/17 at 14h30: payment gateway unreachable.
                

Human readable, machine parseable

Logs should be human readable and unambiguously parseable.


[2017-12-12T14:30:51.965Z] Order "REF-42{order_ref}" failed: payment gateway unreachable.{reason} -- {"order_ref": "REF-42", "reason": "payment gateway unreachable"}
                

Human readable, machine parseable

JSON is fine.

NEVER write multi-line logs.

Context matters


[2017-12-12T14:30:51.965Z] Order {order_ref} failed: {reason} -- {"order_ref": "REF-42", "reason": "payment gateway unreachable", "customer_id": 42, "payment_transaction_id": 4224}
                

Context matters

Never log sensitive data.

Divide and conquer: severity levels

Logs should be categorized by severity level.


[2017-12-12T14:30:51.965Z] [WARNING] Order {order_ref} failed: {reason} -- {"order_ref": "REF-42", "reason": "payment gateway unreachable"}
                

Divide and conquer: severity levels

As defined by the RFC 5424 (Syslog protocol)

debug Detailed debug information
info Interesting events (user logs in, SQL queries, …)
notice Normal but significant events
warning Exceptional occurrences that are not errors
error Runtime errors that do not require immediate action
critical Critical conditions, action must be taken
alert Action must be taken immediately
emergency Emergency: system is unusable.

Divide and conquer: subsystems

Logs should be categorized by subsystem.


[2017-12-12T14:30:51.965Z] [WARNING] [metadata] Export generation failed: {reason} -- {"reason": "export directory not writable", "export_ref": 42, "export_directory": "/home/sftp/exports"}
                

Actionable logs


[2017-12-12T14:30:51.965Z] [WARNING] [metadata] Export generation failed: {reason} -- {"reason": "export directory not writable", "export_ref": 42, "export_directory": "/hom/sftp/exports", "_action": "The export could not be generated because the target directory is not writable. Fix the permissions on the directory and retry the export (`./bin/console tea:export --request=42 --retry`)"}
                

Correlate logs


[2017-12-12T14:30:51.965Z] [WARNING] [metadata] Export generation failed: {reason} -- {"reason": "export directory not writable", "export_ref": 42, "export_directory": "/hom/sftp/exports"} {"request_id": "7d5e8092-a13d-45b8-adb4-b26c18806825"}
                
See Monolog's "processors"

Summary

  • determine a logging strategy: what? for who? why?
  • write explicit and unambiguous messages
  • use standard notations
  • include as much relevant context as you can

Thanks!

TEA is recruiting, join us!

https://joind.in/talk/2d06e

Kévin Gomez / @KPhoen