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

Intercept logs before view #292

Open
AmraniCh opened this issue Nov 4, 2023 · 1 comment
Open

Intercept logs before view #292

AmraniCh opened this issue Nov 4, 2023 · 1 comment

Comments

@AmraniCh
Copy link

AmraniCh commented Nov 4, 2023

Hey, thanks for the package!
I want to know if it possible to intercept logs before they passed to the view ? I want to filter the logs based on some criteria related to my app business logic, is there any thing I can do to achieve that ?

@Clinton594
Copy link

`
// Initialize response class
$Response = new Response;
$response = $Response::get();

// Get request parameters for pagination
$page = (int) $request->input('page') ?: 1;
$limit = (int) $request->input('limit') ?: 1;

try {
  $logger  = new LaravelLogViewer;

  // Get all error logs
  $data = $logger->all();

  // Extract the only the keys that i need
  $data = array_map(fn ($error) => array_extract($error, ['level',  'level_class', 'level_img', 'date', 'text'], false), $data);

  // Filter by error type
  if ($request->level) {
    $data = array_filter($data, fn ($error) => $error['level'] === $request->level);
  }

  // Search the error
  if ($request->search) {
    $data = array_filter($data, fn ($error) => stripos($error['text'], $request->search) > -1);
  }

  // reindex the array keys after filtering
  $data = array_values($data);

  // Build pagination data on it
  $files = collect($data);
  $slice = $files->slice(($page - 1) * $limit, $limit);
  $paginator = new \Illuminate\Pagination\LengthAwarePaginator($slice, $files->count(), $limit);

  // Build response
  $response = $Response::set(['data' => $paginator], true);
  //code...
} catch (\Throwable $th) {
  $response = $Response::set(['message' => $th->getMessage(), 'code' => getExceptionCode($th)]);
}
return $response;`

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

No branches or pull requests

2 participants