PHPPHP17 min read

Logging in PHP (Structured Logs You Can Trust)

Write consistent logs with request IDs and context, so production debugging becomes easier.

Megan Rivera
December 21, 2025
0.0k0

Good logging makes you faster when things break. ## What to log - errors + stack traces - important actions (order created, payment success) - request id, user id (if safe), endpoint, timestamp ## Basic log helper ```php function log_event(string $message, array $context = []): void { $line = date("Y-m-d H:i:s") . " " . $message . " " . json_encode($context) . PHP_EOL; file_put_contents(__DIR__ . "/app.log", $line, FILE_APPEND); } ``` ## Example usage ```php log_event("order_created", ["order_id" => 123, "user_id" => 10]); ``` ## Pro tip In real apps, use a mature logger like Monolog (via Composer). Same idea, better tools. > Next: Environment variables and config management (no hardcoding secrets).

#PHP#DevOps#Advanced