Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Log object?

I can see that Log facade is very useful. In the docs of laravel:

The logger provides the eight logging levels defined in RFC 5424: emergency, alert, critical, error, warning, notice, info and debug.

But, how would I log an instance of a model? like for example:

$user= User::find($user_id);

then, would it be possible to log the $user object?

like image 865
simo Avatar asked Feb 01 '17 11:02

simo


People also ask

How do I print an object in console log?

Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.

How do you log something in JavaScript?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);

How do you alert an object?

alert() method displays a dialog with the specified content. Printing an object using the Window. alert() method will display [object Object] as the output. To get the proper string representation of the object, the idea is to convert the object into a string first using the JSON.


3 Answers

This will work, although logging the entire model will grow your log rather quickly.

Log::info(print_r($user, true));

The true in the second parameter of the print_r() method returns the information instead of printing it, which allows the Log facade to print it like a string.

like image 191
Rob Fonseca Avatar answered Nov 03 '22 11:11

Rob Fonseca


You can log either by print_r or json_encode. json_encode is more readable.

For example:

use Illuminate\Support\Facades\Log;

Log::info(json_encode($user)); 
like image 45
user3785966 Avatar answered Nov 03 '22 10:11

user3785966


I've recently started using Laravel, so this certainly works in 5.3 and 5.4, not sure for earlier versions.

The quickest way I can think of (suits smaller objects) would be to cast object to array:

Log::debug((array) $object);

Yo may wonder how's this possible, first param of debug method (as well as error, notice and other logging methods in Log class) accepts string as first param, and we are passing the array.

So, the answer lays down deep in the log writer class. There is a method that gets called every time to support formatting the messages, and it looks like this:

/**
 * Format the parameters for the logger.
 *
 * @param  mixed  $message
 * @return mixed
 */
protected function formatMessage($message)
{
    if (is_array($message)) {
        return var_export($message, true);
    } elseif ($message instanceof Jsonable) {
        return $message->toJson();
    } elseif ($message instanceof Arrayable) {
        return var_export($message->toArray(), true);
    }

    return $message;
}

Also to clarify things little bit more, you can take a look into: https://github.com/laravel/framework/blob/5.4/src/Illuminate/Log/Writer.php#L199 and you'll see that formateMessage method is formatting the message every time.

like image 30
vkovic Avatar answered Nov 03 '22 10:11

vkovic