|
| 1 | +# Logging |
| 2 | + |
| 3 | +FrankenPHP integrates seamlessly with [Caddy's logging system](https://caddyserver.com/docs/logging). |
| 4 | +You can log messages using standard PHP functions or leverage the dedicated `frankenphp_log()` function for advanced |
| 5 | +structured logging capabilities. |
| 6 | + |
| 7 | +## `frankenphp_log()` |
| 8 | + |
| 9 | +The `frankenphp_log()` function allows you to emit structured logs directly from your PHP application, |
| 10 | +making ingestion into platforms like Datadog, Grafana Loki, or Elastic, as well as OpenTelemetry support, much easier. |
| 11 | + |
| 12 | +Under the hood, `frankenphp_log()` wraps [Go's `log/slog` package](https://pkg.go.dev/log/slog) to provide rich logging |
| 13 | +features. |
| 14 | + |
| 15 | +These logs include the severity level and optional context data. |
| 16 | + |
| 17 | +```php |
| 18 | +function frankenphp_log(string $message, int $level = FRANKENPHP_LOG_LEVEL_INFO, array $context = []): void |
| 19 | +``` |
| 20 | + |
| 21 | +### Parameters |
| 22 | + |
| 23 | +- **`message`**: The log message string. |
| 24 | +- **`level`**: The severity level of the log. Can be any arbitrary integer. Convenience constants are provided for common levels: `FRANKENPHP_LOG_LEVEL_DEBUG` (`-4`), `FRANKENPHP_LOG_LEVEL_INFO` (`0`), `FRANKENPHP_LOG_LEVEL_WARN` (`4`) and `FRANKENPHP_LOG_LEVEL_ERROR` (`8`)). Default is `FRANKENPHP_LOG_LEVEL_INFO`. |
| 25 | +- **`context`**: An associative array of additional data to include in the log entry. |
| 26 | + |
| 27 | +### Example |
| 28 | + |
| 29 | +```php |
| 30 | +<?php |
| 31 | + |
| 32 | +// Log a simple informational message |
| 33 | +frankenphp_log("Hello from FrankenPHP!"); |
| 34 | + |
| 35 | +// Log a warning with context data |
| 36 | +frankenphp_log( |
| 37 | + "Memory usage high", |
| 38 | + FRANKENPHP_LOG_LEVEL_WARN, |
| 39 | + [ |
| 40 | + 'current_usage' => memory_get_usage(), |
| 41 | + 'peak_usage' => memory_get_peak_usage(), |
| 42 | + ], |
| 43 | +); |
| 44 | + |
| 45 | +``` |
| 46 | + |
| 47 | +When viewing the logs (e.g., via `docker compose logs`), the output will appear as structured JSON: |
| 48 | + |
| 49 | +```json |
| 50 | +{"level":"info","ts":1704067200,"logger":"frankenphp","msg":"Hello from FrankenPHP!"} |
| 51 | +{"level":"warn","ts":1704067200,"logger":"frankenphp","msg":"Memory usage high","current_usage":10485760,"peak_usage":12582912} |
| 52 | +``` |
| 53 | + |
| 54 | +## `error_log()` |
| 55 | + |
| 56 | +FrankenPHP also allows logging using the standard `error_log()` function. If the `$message_type` parameter is `4` (SAPI), |
| 57 | +these messages are routed to the Caddy logger. |
| 58 | + |
| 59 | +By default, messages sent via `error_log()` are treated as unstructured text. |
| 60 | +They are useful for compatibility with existing applications or libraries that rely on the standard PHP library. |
| 61 | + |
| 62 | +### Example |
| 63 | + |
| 64 | +```php |
| 65 | +error_log("Database connection failed", 4); |
| 66 | +``` |
| 67 | + |
| 68 | +This will appear in the Caddy logs, often prefixed to indicate it originated from PHP. |
| 69 | + |
| 70 | +> [!TIP] |
| 71 | +> For better observability in production environments, prefer `frankenphp_log()` |
| 72 | +> as it allows you to filter logs by level (Debug, Error, etc.) |
| 73 | +> and query specific fields in your logging infrastructure. |
0 commit comments