greg_sudderth's avatar

Laravel Request Lifecycle: One Life To Live? (Log Facade)

Hiya, I have another question, but I have to ask this question first:

Does Laravel fire up, handle a single request, and exit?

It matters because I want Log:: to be more specific to my telecom-style logging needs.

If I had some global variable(s) I could set out of the request, and then make a modification to Log:: such that it could preface those variables onto the log statement, without making me do this, it would be great:

$sid = $request->input('CallSid'); //... several others

Log::debug("Cannot connect call at this time");

....and the output looks like:

(normal Log time prefix):SID=ae737726df721: Cannot connect call at this time

....or would sub-classing Log with some once-per-request-setup be cleaner? I am going to have a lot of REST traffic and debugging a particular fail with lots of log statements going on all intermixed will be madness.

Would they be...if Laravel is doing one request at a time?

TIA... A converted large-java-web-apps-are-sad-fail convert!

0 likes
3 replies
lostdreamer_nl's avatar
Level 53

"Does Laravel fire up, handle a single request, and exit?"

That's how PHP works. The webserver will hand over 1 request to the PHP process, and after the PHP script ran, it dies.

The next request will start clean (booting laravel first, then running the code, then die'ing again)

If you want to change what is going into the log you can simply set it up with '$app->configureMonologUsing' in /bootstrap/app.php

$app->configureMonologUsing(function ($monolog) use ($app) {
    $sid = request()->get('CallSid');
    
    $logStreamHandler = new \Monolog\Handler\StreamHandler(storage_path('logs/laravel.log'), \Monolog\Logger::DEBUG);
    
    // Formatting
    $formatter = new Monolog\Formatter\LineFormatter("[%datetime%] SID=". $sid ." %channel%.%level_name%: %message% %context% %extra%");
    $logStreamHandler->setFormatter($formatter);
    $monolog->pushHandler($logStreamHandler);
});

After that, when you do :

\Log::debug('test');

It will add a line like:

[2018-02-22 15:44:33] SID=ae737726df721 local.DEBUG: test 
1 like
greg_sudderth's avatar

Wow! That's like a 5$ and a 20$ answer right together! Thanks again! Wow!

greg_sudderth's avatar

Hi following up on your cool mod above...I'm getting this type of message:

[2018-03-17 08:08:38] SID=SMX7fb6b10888f1453f7c315f793b401  local.DEBUG: SMS: frp,=+14155551212 to=+14155551213 sid=SMX7fb6b10888f1453f7c315f793b401 status=received body='Toast' [] [][2018-03-17 08:08:38] SID=SMX7fb6b10888f1453f7c315f793b401 local.DEBUG:     SMS: Existing [] []

I've got two questions:

  1. why the []'s ... context/extra are empty?
  2. why no newlines (wrapping)?

I changed the one line to:

    $formatter = new Monolog\Formatter\LineFormatter("[%datetime%] SID=". $sid ." %channel%.%level_name%: %message%\n");

Thanks in advance!

Please or to participate in this conversation.