Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add context to subsequent logs. #37847

Merged
merged 2 commits into from
Jun 28, 2021

Conversation

chasenyc
Copy link
Contributor

@chasenyc chasenyc commented Jun 28, 2021

Description:
This adds the ability to add context to all future logs along with the ability to clear that context if desired.

Use case:
A good example of this of how this might be used in middleware, lets say you wanted to bind a uuid to every request:

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle(Request $request, Closure $next)
{
    $requestId = Str::uuid();

    Log::withContext(['request-id' => $requestId]);

    $response = $next($request);

    $response->header('request-id', $requestId);

    return $response;
}

Then when parsing logs one would be able to trace all logs pertaining to a specific request with ease. Another example where this would be very useful would be something like adding an authenticated user to the context that would be tied to all following logs.

- Update Logger.php to store context in state
- Add test to ensure it works properly
@chasenyc chasenyc force-pushed the chasenyc/add-context-to-all-logs branch from 08d4cb0 to e438b71 Compare June 28, 2021 01:45
@chasenyc chasenyc changed the title Add context to subsequent logs. [8.x] Add context to subsequent logs. Jun 28, 2021
@taylorotwell taylorotwell merged commit b0ae212 into laravel:8.x Jun 28, 2021
Copy link

@shivragjbi shivragjbi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@GrahamCampbell
Copy link
Member

This will not work right with octane, surely, and also with queue jobs. Stuff will bleed across requests.

@taylorotwell
Copy link
Member

@GrahamCampbell we will patch that up.

chu121su12 pushed a commit to chu121su12/framework that referenced this pull request Jul 21, 2021
* Add context to future logs.
- Update Logger.php to store context in state
- Add test to ensure it works properly

* formatting

Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants