The first time this matters is usually a support ticket. A customer says checkout failed at 2pm, and you need to find what actually happened to them, not to everyone. If your telemetry doesn't know who hit each request, the answer is grep and guesswork. Attaching user identity to every event turns that question into a filter.
Middleware approach
The Nightwatch package, which NightOwl reads, records the authenticated user on its own: the ID, plus a name and username that default to the user's name and email. Those three fields are all it stores. To choose what fills name and username, register a resolver once in AppServiceProvider::boot():
use Laravel\Nightwatch\Facades\Nightwatch;
public function boot(): void
{
Nightwatch::user(fn ($user) => [
'name' => $user->name,
'username' => $user->email,
]);
}Anything else you want next to the user, like a plan or team ID, belongs in your log context. A middleware runs on every authenticated request and can push those fields onto every log line:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class AttachUserTelemetry
{
public function handle($request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
// Push into Laravel's log context (appears on every log line)
Log::withContext([
'user_id' => $user->id,
'user_email' => $user->email,
'user_plan' => $user->subscription_plan ?? null,
]);
}
return $next($request);
}
}Register it on routes that should be tracked:
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\App\Http\Middleware\AttachUserTelemetry::class,
]);
$middleware->api(append: [
\App\Http\Middleware\AttachUserTelemetry::class,
]);
})What to attach (and what not to)
Attach
- User ID (stable, non-secret)
- Email (useful for support)
- Plan or subscription tier
- Team / org ID for B2B apps
- Feature flag variant
Avoid
- Passwords, password hashes
- Payment card info, bank details
- Government IDs, SSNs
- Full addresses, phone numbers
- Any field users haven't consented to share
Frontend → backend correlation
For full visibility, your front end should generate a request ID and pass it via header on every API call. The backend picks it up, adds it to every log line and echoes it back in the response.
Frontend (every fetch)
const requestId = crypto.randomUUID();
fetch('/api/orders', {
headers: {
'X-Request-Id': requestId,
// ...
},
});public function handle($request, Closure $next)
{
$id = $request->header('X-Request-Id') ?? (string) \Illuminate\Support\Str::uuid();
$request->attributes->set('request_id', $id);
Log::withContext(['request_id' => $id]);
$response = $next($request);
$response->headers->set('X-Request-Id', $id);
return $response;
}Two places it quietly breaks
Middleware order. Nightwatch resolves the user itself at the end of the request, so its user field doesn't depend on where your middleware sits. Your log-context middleware does, on routes behind a token guard such as Sanctum: the default guard only switches once the auth middleware has run, so a middleware placed earlier sees Auth::check() return false and attaches nothing. Session-based web routes resolve the user from the session either way. If user fields are missing from logs on API routes but not web routes, check the middleware order on those routes first.
Queued jobs. Nightwatch carries the ID of the user who dispatched a job into that job's records, which is how NightOwl can list a user's queued jobs. Log::withContext doesn't cross that boundary: the job runs later, in another process, with no authenticated user. For log lines inside the job, add the fields with Laravel's Context facade (Context::add()), which is passed into queued jobs, or pass the ID into the job's constructor. Artisan commands and scheduled tasks record no user at all.