Skip to content

Instantly share code, notes, and snippets.

@phroggyy
Created October 26, 2019 15:37
Show Gist options
  • Select an option

  • Save phroggyy/38877e4dc6739a01b4433e7a9c32cfeb to your computer and use it in GitHub Desktop.

Select an option

Save phroggyy/38877e4dc6739a01b4433e7a9c32cfeb to your computer and use it in GitHub Desktop.

Revisions

  1. phroggyy created this gist Oct 26, 2019.
    68 changes: 68 additions & 0 deletions LogStackVariables.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <?php

    namespace App\Jobs;

    use Decahedron\StickyLogging\StickyContext;
    use Exception;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Http\Kernel;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Http\Request;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Support\Facades\Log;

    class LogStackVariables implements ShouldQueue
    {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
    * @var string
    */
    private $tempFileName;

    /**
    * @var Request
    */
    private $capturedRequest;

    /**
    * @var null
    */
    private $requestId;

    /**
    * Create a new job instance.
    *
    * @param string $tempFileName
    * @param Exception $exception
    * @param Request $capturedRequest
    */
    public function __construct(string $tempFileName, Request $capturedRequest, $requestId = null)
    {
    $this->tempFileName = $tempFileName;
    $this->capturedRequest = $capturedRequest;
    $this->requestId = $requestId;
    }

    /**
    * Execute the job.
    *
    * @return void
    */
    public function handle(Kernel $kernel)
    {
    if ($this->requestId) {
    StickyContext::add('request', $this->requestId);
    }

    require_once $this->tempFileName;
    $this->capturedRequest['__debug_mode__'] = true;
    $kernel->handle($this->capturedRequest);

    unlink($this->tempFileName);

    Log::debug('ran debug request', ['vars' => StickyContext::stack('vars')->all()]);
    }
    }
    13 changes: 13 additions & 0 deletions write.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    $request = $this->container['request'];

    if ($this->container->runningInConsole() || $request['__debug_mode__']) {
    return;
    }

    $lines = file($exception->getFile());
    $prepend = "\\Decahedron\\StickyLogging\\StickyContext::stack('vars')->add('{$exception->getFile()}:{$exception->getLine()}', get_defined_vars());";
    $lines[$exception->getLine()-1] = $prepend . $lines[$exception->getLine()-1];
    $newFile = substr($exception->getFile(), 0, -4) . Str::random() . '.php';
    file_put_contents($newFile, implode("", $lines));

    dispatch(new LogStackVariables($newFile, Request::capture()));