How to Log object?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Logging Objects in Laravel: A Comprehensive Guide for Developers
Body:
In the world of Laravel, logging is an essential part of debugging and monitoring your application's behavior. The logger provides a flexible interface to manage logs, allowing developers to choose between multiple levels of importance defined by RFC 5424. While it's clear how to log different types of messages, there might be situations when you need to log objects like instances of models instead of just simple strings or integers.
Let's see two approaches for logging object instances in Laravel. The first one is using the toJson() method on your model instance, which transforms it into a JSON string representation. Here's an example:
```php
$user = User::find($user_id);
Log::debug('User updated: ' . $user->toJson());
```
The above code logs the user object as a debug level message, converting it to JSON beforehand. This ensures that you get a readable and easily parsable representation of your objects in the log file. Note that we're using the debug level so that this information is not considered crucial for daily application running but can still be useful during troubleshooting or monitoring purposes.
Another approach to logging object instances involves using the logger's context feature, which allows you to store additional information in your log entries. You can add variables as keys and values in the context array, including your model instance:
```php
$user = User::find($user_id);
Log::warning('Ban user', ['user' => $user]);
```
The above code logs a warning message with the context 'user' containing the model instance. This method can be more beneficial when you need to log multiple objects or information related to that object, as it allows for easily accessible data in your log files and makes debugging easier.
To view your logs in Laravel, you can use either the built-in logger or an external logging tool like Monolog. The built-in logger offers a simple interface for viewing logs with multiple filters and options:
```php
Log::info('Info message'); // Viewable using artisan tinker log:info
Log::warning('Warning message'); // Viewable using artisan tinker log:warn
Log::error('Error message'); // Viewable using artisan tinker log:error
```
For external logging tools like Monolog, you can set up your configuration in the 'config/logging.php' file and use dedicated methods for each logging level. Here's an example:
```php
$logger = new \Monolog\Logger('my_custom_logger');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('./logs/app.log', \Monolog\Logger::WARNING));
Log::warning('Custom logging with Monolog', ['user' => $user]); // Viewable in ./logs/app.log file
```
In conclusion, logging object instances is a crucial aspect of effective debugging and monitoring in Laravel applications. By using the toJson() method or the logger's context feature, you can ensure that your log files are well-structured, informative, and convenient for troubleshooting purposes. Remember that logging should be well thought out, as it's essential for keeping your application running smoothly and effectively.