Skip to content

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().

Inject the logger into a route, controller or service:

routes.php
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.

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 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'],
]);

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.

The runtime can log every HTTP request itself, after the response is sent. Enable it with the accessLog listener option:

index.php
$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.

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.

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:

Terminal window
$ 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.

Where the lines end up depends on how you run the application:

  • pam dev prints them in your terminal.
  • pam start writes them to its standard error; your process manager (systemd, Docker, Kubernetes) collects them.
  • pam up runs the application in the background and keeps its output in log files. Application logs are on standard error, so pass --errors (or --both for standard output too):
Terminal window
pam logs my-api --errors --lines 50
pam logs my-api --errors --follow
pam logs my-api --errors --query checkout-42

See Process Management for pam up, pam ps and pam logs.

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:

bootstrap/app.php
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),
]));
Terminal window
pam composer require monolog/monolog