Skip to content

Commit 89c97fd

Browse files
dunglasCopilot
andauthored
docs: logging (#2097)
Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 14c7db1 commit 89c97fd

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Go to `https://localhost`, and enjoy!
128128
- [Worker mode](https://frankenphp.dev/docs/worker/)
129129
- [Early Hints support (103 HTTP status code)](https://frankenphp.dev/docs/early-hints/)
130130
- [Real-time](https://frankenphp.dev/docs/mercure/)
131+
- [Logging](https://frankenphp.dev/docs/logging/)
131132
- [Hot reloading](https://frankenphp.dev/docs/hot-reload/)
132133
- [Efficiently Serving Large Static Files](https://frankenphp.dev/docs/x-sendfile/)
133134
- [Configuration](https://frankenphp.dev/docs/config/)

docs/logging.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)