Logging
Atom's Logging module gives your application a file-backed logger. Routes and services can inject LoggerInterface to record application events and failures. Each call takes a message and optional context, such as a report name or request ID that helps you find related entries in the log.
Logging Module
The module binds Atom\Logging\LoggerInterface to FileLogger and configures the destination file.
The file logger appends one entry per call with a timestamp, level, and message. When you pass context, it appends that context as JSON. If a request throws an unhandled exception, Atom's error handler can use the same logger to record the failure.
Import Atom\Modules\Logging\Logging in app/Application.php. Add this line inside the existing modules() hook:
$modules->add(Logging::file(dirname(__DIR__) . "/storage/logs/app.log"));
Atom creates the log directory on the first write. To set the destination per environment, register Logging::module() and set LOG_PATH to an absolute filename in .env.
Logger API
Both methods take a message and an optional array of context fields.
| Method | When to use it |
|---|---|
$logger->info($message, $context) |
Record a normal step or result, such as starting a report. |
$logger->error($message, $context) |
Record a failure your code handles, such as a report export you catch and skip. |
$logger->info("Loading daily totals", ["report" => "daily"]);
$logger->error("Could not export report", ["report" => "daily"]);
Trace a failed request
In the application's bootstrap() hook, register this route. Import Atom\Http\Request, Atom\Logging\LoggerInterface, and Atom\Router\Route at the top of the file. Atom injects the current request and the configured logger into the handler.
Route::get("/reports/daily", static function (Request $request, LoggerInterface $logger): never {
$logger->info("Loading daily totals", [
"request_id" => $request->headers()->get("X-Request-Id"),
"report" => "daily",
]);
throw new \RuntimeException("Daily totals are unavailable");
})->name("reports.daily");
With the app running, make a request that includes an ID you can look up:
curl -i -H "X-Request-Id: report-demo-42" http://127.0.0.1:8020/reports/daily
The example deliberately returns HTTP 500. Open storage/logs/app.log and search for report-demo-42. The INFO entry shows what the handler was doing. The ERROR entry that Atom writes for the unhandled exception identifies the named route (reports.daily) and includes the exception type, message, file, and line.