laravel 如何记录对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41978290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Log object?
提问by simo
I can see that Log facade is very useful. In the docs of laravel:
我可以看到 Log 门面非常有用。在laravel的文档中:
The logger provides the eight logging levels defined in RFC 5424: emergency, alert, critical, error, warning, notice, info and debug.
记录器提供 RFC 5424 中定义的八个日志记录级别:紧急、警报、严重、错误、警告、通知、信息和调试。
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?
那么,是否可以记录该$user
对象?
回答by Rob Fonseca
This will work, although logging the entire model will grow your log rather quickly.
这会起作用,尽管记录整个模型会很快增加您的日志。
Log::info(print_r($user, true));
The truein 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.
的真正的的print_r()方法的第二个参数返回信息而不是打印它,这允许登录门面打印它像一个字符串。
回答by user3785966
You can log either by print_r or json_encode. json_encode is more readable.
您可以通过 print_r 或 json_encode 进行记录。json_encode 更具可读性。
e.g:
例如:
use Illuminate\Support\Facades\Log;
Log::info(json_encode($user);
回答by GiamPy
No.
不。
The first parameter must be a string (or a string object representation). If you wish to pass any other type of (raw) data or objects, you can always JSON encode them, and push them in the context settings, like so:
第一个参数必须是字符串(或字符串对象表示)。如果您希望传递任何其他类型的(原始)数据或对象,您始终可以对它们进行 JSON 编码,并将它们推送到上下文设置中,如下所示:
<?php
$user = User::find($user_id);
\Log::error("Something happened to User {$user_id}.", ['object' => $user->toJson()]);
Or:
或者:
<?php
// User.php
[...]
class User
{
[...]
public function __toString()
{
return "{$this->id}";
}
}
// [...]
$user = User::find($user_id);
\Log::error("Something happened to User {$user}.", ['object' => $user->toJson()]);
You can find more information about the method signatures here.
您可以在此处找到有关方法签名的更多信息。
回答by Vladimir Kovic
I've recently started using Laravel, so this certainly works in 5.3 and 5.4, not sure for earlier versions.
我最近开始使用 Laravel,所以这肯定适用于 5.3 和 5.4,不确定是否适用于早期版本。
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.
您可能想知道这怎么可能,调试方法(以及 Log 类中的错误、通知和其他日志记录方法)的第一个参数接受字符串作为第一个参数,并且我们正在传递数组。
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#L199and you'll see that formateMessage method is formatting the message every time.
为了进一步澄清事情,您可以查看:https: //github.com/laravel/framework/blob/5.4/src/Illuminate/Log/Writer.php#L199,您将看到该 formateMessage 方法每次都在格式化消息。
回答by Danbass07
This causes "allocated memory size exhausted" exception in some cases. (e.g native exception class) – Gokigooooks
在某些情况下,这会导致“已分配的内存大小耗尽”异常。(例如本机异常类)– Gokigooooks
Had same problem.
有同样的问题。
Log::info(print_r($request->user()->with('groups'), true ) );
Add ->get()
添加 ->get()
Log::info(print_r($request->user()->with('groups')->get(), true ) );