phplaravel-4

Laravel : Log:info in repository not printing values from Input


New to Laravel, hence want to understand this.

Is Log::info() not allowed to print some of the information ? It dies silently, hence wondering if this is some Laravel rule I've yet to learn..

My HTML form is of this kind :

<form>
   <input type="hidden" name="val[name]">
   <input type="hidden" name="val[address]">
</form>

My Ajax function just creates a formData and sends it to the backend.

$.ajax({
            url : baseUrl + 'update/' + id,
            type : 'POST',
            data : new FormData(this),
            contentType : false,
            cache : false,
            processData : false,
            success : function(
            }
        });

My Controller prints the /Input::get('val') without an issue.

My Repository

public function update user($id, $val)
{
    Log::info('reached forward() in the repository');

    $post = $this->findById($postID);
    Log::info('Going to print the values now');
    Log::info('Val : ', $val);
    //Log::info('Post-ID : ', $id);
    Log::info('-----------------');

    Log::info('Extracting the VAL to regular variables');


    $expected = [
        'name' => '',
        'address' => []
    ];

    /**
     * @var $name
     * @var $address
     */
    extract($val = array_merge($expected, $val)); // create $name & $address

    Log::info('After extraction'); // Gets printed

    if($name == 'Tim')
    {
        Log::info('Name entered is in fact Tim'); // This does get printed
    }
    Log::info('Name sent is : ', $name); // **This is never printed**
    Log::info('Printed Name'); // **This is never printed**
}

Am I doing something wrong, or is Laravel's Log function not allowed to print them ?
BTW, I don't see any error in the laravel.log or the php error log.


Solution

  • No, there are no special rules about how logging works - it logs whatever string is passed as the first argument + the context array that might be passed as second argument.

    Let's have a look at your code:

    Log::info('Name entered is in fact Tim'); // This does get printed
    Log::info('Name sent is : ', $name); // **This is never printed**
    Log::info('Printed Name'); // **This is never printed**
    

    The first line gets printed, because a string is passed to info() method. The second call is invalid, as you're passing 2 string arguments. If you meant to concatenate Name sent is: and $name, you need to use a dot instead of comma:

    Log::info('Name sent is : ' . $name); // Now this will get printed
    

    The reason why the third line is not printed is because the second one causes PHP to crash because of invalid type of second argument. It expects an array, but it gets string.