Logging
PAM logs are structured: every line is one JSON object, written to the worker’s standard error. Each line carries the request id and trace context of the request that wrote it, and the id of the worker, so you can follow one request across your own messages, the runtime’s access log and error reports without any extra setup.
You write logs through any PSR-3 logger. In a Nimbus application, type-hint Psr\Log\LoggerInterface and you receive Pam\Observability\Logger, which forwards to the runtime’s Telemetry::log().
Writing Log Messages
Section titled “Writing Log Messages”Inject the logger into a route, controller or service:
use Psr\Log\LoggerInterface;
$router->post('/orders/{id}/ship', function (string $id, LoggerInterface $logger): array { $logger->info('Order {id} shipped', ['id' => (int) $id, 'carrier' => 'acme-post']);
return ['shipped' => true];});The line written to stderr:
{"timestamp":1796236923980,"level":"info","message":"Order 1001 shipped","context":{"id":1001,"carrier":"acme-post"},"requestId":"standalone-1029282-26","traceparent":"","worker":"standalone"}| Field | Description |
|---|---|
timestamp |
Milliseconds since the Unix epoch. |
level |
The PSR-3 level, lower-case. |
message |
The message, with {placeholders} replaced by scalar context values. |
context |
The full context array. |
requestId |
The request’s PAM_REQUEST_ID, or null outside a request. |
traceparent |
The W3C trace context of the request. |
worker |
The worker id under pam start, standalone otherwise. |
All PSR-3 methods are available: emergency, alert, critical, error, warning, notice, info and debug.
Without a Framework
Section titled “Without a Framework”The runtime API is available in any PAM script, framework or not:
use Pam\Observability\Telemetry;
Telemetry::log('warning', 'Payment provider slow: {ms} ms', ['ms' => 1840, 'provider' => 'acme-pay']);Pam\Observability\Logger is the PSR-3 adapter over the same call. It needs psr/log, which Nimbus already installs.
Context and Placeholders
Section titled “Context and Placeholders”Context values that are scalars or Stringable replace {key} placeholders in the message; every value, including arrays, is kept in context for your log pipeline to index:
$logger->error('Import of {file} failed after {rows} rows', [ 'file' => 'customers.csv', 'rows' => 1200, 'errors' => ['line 1201: invalid e-mail'],]);Measuring Operations
Section titled “Measuring Operations”Telemetry::span() runs a callable and logs its duration at the debug level. When the OpenTelemetry API is installed (open-telemetry/api), it also records a real span with the attributes you pass:
use Pam\Observability\Telemetry;
$rates = Telemetry::span('fetch-exchange-rates', fn () => $client->get('https://api.example.com/rates'), [ 'provider' => 'example',]);See Observability & Diagnostics for traces, metrics and OTLP export.
Access Logs
Section titled “Access Logs”The runtime can log every HTTP request itself, after the response is sent. Enable it with the accessLog listener option:
$app->listen((int) (getenv('PAM_PORT') ?: 3000), '0.0.0.0', [ 'accessLog' => true, 'accessLogSampleRate' => 1, // log 1 request in N]);{"durationMs":1.332368,"event":"http.request","level":"info","method":"GET","requestId":"my-trace-1","status":200,"target":"/server","timestamp":1796236969519,"traceparent":"","worker":"standalone"}Responses with a 5xx status are always logged at the error level, even when accessLog is off or the request falls outside the sample. That way every failure has an access log line with the same requestId as its exception log.
Exceptions
Section titled “Exceptions”Unhandled exceptions are logged for you, with the class, message, file and line, before the client receives a generic 500:
{"timestamp":1796236918878,"level":"error","message":"Unhandled Nimbus exception","context":{"exception":"RuntimeException","message":"Database password is hunter2","file":"/srv/app/routes.php","line":46},"requestId":"standalone-1029282-13","traceparent":"","worker":"standalone"}If you register your own onError() handler, log inside it. See Error Handling.
Correlating Requests
Section titled “Correlating Requests”Every log line written during a request carries its requestId. When a client or proxy sends X-Request-Id, PAM uses that value as the label, so an id generated at your load balancer appears in your PHP logs too:
$ curl -H 'x-request-id: checkout-42' http://127.0.0.1:3000/server{"durationMs":1.33,"event":"http.request","level":"info","method":"GET","requestId":"checkout-42","status":200,"target":"/server","timestamp":1796236969519,"traceparent":"","worker":"standalone"}The same id is available to your code as $_SERVER['PAM_REQUEST_ID'], for example to include in an error page or a support ticket.
Reading Logs
Section titled “Reading Logs”Where the lines end up depends on how you run the application:
pam devprints them in your terminal.pam startwrites them to its standard error; your process manager (systemd, Docker, Kubernetes) collects them.pam upruns the application in the background and keeps its output in log files. Application logs are on standard error, so pass--errors(or--bothfor standard output too):
pam logs my-api --errors --lines 50pam logs my-api --errors --followpam logs my-api --errors --query checkout-42See Process Management for pam up, pam ps and pam logs.
Log Levels and Custom Loggers
Section titled “Log Levels and Custom Loggers”PAM writes every level; it does not filter. Filter by level in your log pipeline, or bind your own PSR-3 logger in the container. Bindings registered at boot replace the default:
use Monolog\Handler\StreamHandler;use Monolog\Level;use Monolog\Logger;use Psr\Log\LoggerInterface;
$app->container()->singleton(LoggerInterface::class, fn (): LoggerInterface => new Logger('app', [ new StreamHandler('php://stderr', Level::Warning),]));pam composer require monolog/monolog