Combine error timestamps, request IDs and safe stack context to isolate production faults.
What It Does and When to Use It
Correlate recent application and FPM errors with the incident window. Direct PHP code exposes the underlying operation clearly; a framework can supply lifecycle hooks, dependency wiring and retries.
Requirements, Inputs and Expected Behavior
Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Do not expose log files through the document root or paste secrets into tickets.
Example 1: Basic Operation
Correlate recent application and FPM errors with the incident window.
sudo tail -n 100 /var/log/php/app-error.log
sudo journalctl -u php8.4-fpm --since '15 minutes ago' --no-pager
Example 2: Behavior and Validation
Use stable correlation identifiers without recording request secrets.
<?php
$requestId=bin2hex(random_bytes(8));
error_log(json_encode(['request_id'=>$requestId,'type'=>'dependency_timeout'],JSON_THROW_ON_ERROR));
Practical Example: Application Use
Log only safe diagnostic classes while hiding stack traces from visitors.
<?php
set_exception_handler(static function(Throwable $e): void {
error_log('Unhandled '.get_class($e)); http_response_code(500); echo 'Internal error';
});
Important Behavior and Edge Cases
The second example highlights: Use stable correlation identifiers without recording request secrets. The practical example adds: Log only safe diagnostic classes while hiding stack traces from visitors.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Direct operation versus framework-managed integration | Direct PHP code exposes the underlying operation clearly; a framework can supply lifecycle hooks, dependency wiring and retries. |
| Choosing safely | Choose based on deployment requirements and verify actual behavior rather than assuming defaults. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming successful execution proves the result is usable | Check the return value, required extension, and observable output of the specific operation. |
| Ignoring runtime and environment differences | Verify PHP version, SAPI, operating system, service access, and permissions before deployment. |
| Skipping operational constraints | Do not expose log files through the document root or paste secrets into tickets. |
Key Takeaways
- Combine error timestamps, request IDs and safe stack context to isolate production faults.
- Log only safe diagnostic classes while hiding stack traces from visitors.
- Choose based on deployment requirements and verify actual behavior rather than assuming defaults.