Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Being Able to Find Out What Happened
PHP Development

Being Able to Find Out What Happened

Logging in a PHP application so you can trace what happened: record business events, correlate with a request identifier and keep personal data out.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

Log the business events, not just the errors. Include a request identifier so a single journey can be traced, and keep personal data out of the logs.

Logs are for answering questions later

The question is rarely “what error occurred?” It is “why did this customer's order end up in this state?” Answering that needs business events logged, not just exceptions.

A log full of stack traces and empty of business events tells you what broke and nothing about what happened.

What to log

  1. Business events — order created, payment taken, status changed, who did it
  2. Integration calls — what was sent, what came back, how long it took
  3. Scheduled job runs — start, finish, what was processed
  4. Authorisation failures, which indicate either a bug or an attack
  5. Errors, with enough context to reproduce them

Correlate with a request identifier

Generate an identifier at the start of each request and include it in every log line for that request, including any queued jobs it triggers.

That single practice turns log investigation from guesswork into filtering, and it costs almost nothing to implement.

Keep personal data out

  • Log identifiers, not names and addresses
  • Never log passwords, tokens or card details, even in error handlers
  • Redact before logging rather than after
  • Apply a retention period, automatically
  • Remember logs are in scope for data requests

Make them searchable

NeedRequires
Find one customer's journeyStructured logs with identifiers
Spot a patternAggregation across servers
Alert on somethingMachine-readable format
Investigate last monthRetention long enough
Prove what happenedImmutable storage

A log file that can only be read by tailing it on a server is a log nobody consults.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

How much should we log?

Enough to reconstruct what happened, not so much that finding it is impossible. Business events plus errors is usually the right level.

How long should logs be kept?

Thirty to ninety days for diagnostics; longer for anything serving as an audit record. Decide and apply it automatically.

Do we need a log aggregation service?

Once you have more than one server, effectively yes. Before that, structured files with a retention policy are adequate.

What about performance overhead?

Negligible at sensible volumes. Logging every database query in production is the exception and it is rarely warranted.

Keep reading

More on PHP Development

PHP Development

Why PHP Is Still a Sensible Choice

Is PHP still a good choice for business applications? Where modern, typed PHP fits, where another language is better, and the hosting and hiring arguments.

PHP Development

Improving Old Code Without a Rewrite

Modernising a legacy PHP application without a rewrite: move to a supported PHP version, add tests around what matters, then strangle rather than replace.

Start here

Cannot work out how a record got into that state?

That is a logging gap. Business event logging is a small change that answers most of these questions.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →