How to print messages on console in Laravel?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: A Comprehensive Guide on Printing Messages to Console in Laravel Introduction: When working with Laravel framework, it becomes crucial to efficiently communicate your application's status and events through output messages. In this blog post, we will explore various options for printing messages directly to the console while running PHP Artisan commands, including Log channels and custom solutions. So, let's dive into the world of Laravel console outputs! 1. Using Laravel Built-in Logging Channel: Laravel has a built-in logging facility that can be used for displaying messages to the console screen. The `Log::info` method is one of the most commonly used methods within the `Log` facade, which allows you to log information and debugging messages. For example: ```php Log::info('This is an info message'); ``` Alternatively, there are other logging channels such as `error`, `warning`, `debug`, and so on. You can use these methods to log different levels of severity for your application. Below examples demonstrate each method: - `Log::error('This is an error message')` - `Log::warning('This is a warning message')` - `Log::debug('This is a debugging message')` These messages will be displayed on the screen when you run PHP Artisan commands like "php artisan serve" or "php artisan migrate". 2. Using Laravel's Eloquent Model Events: If you want to print specific details of your model (for instance, user's details), you can use `Log` methods when saving updates through events: ```php User::observe(new UserObserver); class UserObserver extends Observer { public function updating(User $user) { Log::info('User name updated to ' . $user->name); } } ``` This approach is helpful when you need to log specific events related to database models without impacting the application's performance. 3. Using Custom Console Commands: Sometimes, you may require to print custom output messages or perform certain tasks while running a specific PHP Artisan command. In such cases, creating your own console commands and custom classes can be helpful: - Create a new command using `php artisan make:command MyCommand` - Override the `handle` method to add your custom logic, including printing messages as needed For instance: ```php info('This is a custom message from your command'); } } ``` Running the command `php artisan mycommand` will now display the provided output message. This approach enables more control over console messages and can be useful in cases where you need to print detailed information or perform advanced tasks. 4. Using Laravel Artisan Output Channels: Laravel's built-in logging channel is useful for simple messages, but sometimes you might want to add custom formatting, colors, or more complex output structures. In these cases, you can create your own output channels and extend the functionality of `Log::channel()`: ```php class CustomOutputChannel extends Channel { public function getMessages($level, $message) { // Perform custom logic to format/color/structure messages as needed return ['Message for custom channel']; } } // Use your custom output channel with the Log facade Log::channel('custom_output')->info('Testing custom channel'); ``` This approach gives you complete control over how your messages are displayed in the console, allowing for advanced formatting and styling. Conclusion: Printing messages to console in Laravel can be achieved using several methods depending on your requirements. You can use built-in logging channels, Eloquent model events, custom console commands, or create your own output channels. With these options at hand, you have the flexibility to showcase your application's status and events effectively. Employing appropriate strategies will undoubtedly optimize your Laravel applications for better usability and performance.