Ben Waine’s (@bwaine) talk at PHPNW12 on application logging. As usual, these are my thoughts, misunderstandings and mishearings…corrections welcome in the comments.
So, can logging be cool?
Why log? Patterns and trends, information after the fact.
tail -f FTW
About the Skybet application log:
- Signposts everything when it happens. Good way to learn the path through the layers.
- Good visible errors.
- Logs the start and end of request, including memory consumed, so you can spot waiting and memory trends.
Moving on to methods of logging
error_log. (Can be sent to stdout for situations when running in the CLI):
Pros: native
Cons: ini can be overwritten
Various logging libraries, ZendLog and monolog. Prefer: Monolog. Well maintained, good feature set.
Fingers crossed handler: logs to memory, but only to disk if condition X occurs.
Allows different levels of logging: error, notice, info, etc.
Different log writers: file, email, firebug, etc, etc, can write your own.
Your application logger
Handy to set a unique request ID. Can spot an issue, and grep the log for all activity on that request.
Logger is passed MLogger on construction.
Different logs: application log and security log. Different information logged (cleary).
Event logging
Things which happen (in your system) get logged.
Graphite + statsd – Statsd has buckets which periodically get sent to Graphite. Your application logger can have a method to increment statsd, statsd sometimes sends to Graphite and graphs appear magically.
Can get more efficient than sending data from your app.
Strategy
- Choose a consistent logging style
- Centrally deal with formatting
- Consistency, consistency, consistency
Log:
- Request start and end
- Transitions between layers
- DB queries
- Biz events (separate log)
- Message queue activity
- Errors and notices from PHP
Avoid leaving crazy debug lines in that others have to filter out. Make sure content is greppable.
Log aggregation: rsyslog, logstash.net (really handy, can also send to statsd lots of inputs, filters, outputs).
Hosted: splunk, loggly, new relic.
The only question: Is an event based logger a good plan? Yes, avoids dependencies on classes.