Hello Geek ,In Laravel log files are created automatically when an error occurs. However, sometimes you may want to create a custom log file to track specific events or processes in your application. Here’s how you can create a custom log file in Laravel.
- Create a new channel in your ‘
config/logging.php
‘ file
The first step is to create a new channel in your ‘config/logging.php
‘ file. A channel is a specific target for your logs, such as a file or a database. To create a new channel, add a new key to the ‘channels
‘array in your ‘logging.php
‘ file. The key should be the name of your new channel, and the value should be an array that specifies the target for your logs.
Here’s an example of a new channel that writes logs to a ‘custom.log
‘ file:
'custom' => [
'driver' => 'single',
'path' => storage_path('logs/custom.log'),
'level' => 'debug',
],
In this example, the ‘driver
‘key specifies that we want to write our logs to a single file. The ‘path
‘key specifies the path to the file where we want to store our logs. In this case, we’re using the ‘storage_path()
‘ helper function to get the path to the ‘logs
‘directory in our application’s storage directory. The ‘level
‘key specifies the minimum level of severity for the logs that we want to write to this channel. In this case, we’re writing all logs with a severity of ‘debug
‘or higher.
- Use your new channel to write logs in your application
Now that we’ve created our new channel, we can use it to write logs in our application. To write a log to our new channel, we’ll use the ‘Log
‘facade. The ‘Log
‘facade provides several methods that we can use to write logs at different levels of severity, such as’ debug()
‘, ‘info()
‘, ‘warning()
‘, and ‘error()
‘. We can specify our new channel as the first parameter to these methods to write logs to our custom log file.
Here’s an example of writing a debug log to our custom channel:
use Illuminate\Support\Facades\Log;
Log::channel('custom')->debug('This is a debug log for our custom channel.');
In this example, we’re using the ‘Log::channel('custom')
‘ method to specify that we want to write our log to our custom channel. We’re then calling the ‘debug()
‘ method on the ‘Log
‘facade and passing in the log message as a parameter.
- Test your custom log file
Once you’ve written some logs to your custom log file, you can test that they’re being written correctly. You can find your custom log file in the path that you specified in your ‘logging.php
‘ file. In our example, the log file would be located at ‘storage/logs/custom.log
.‘
You can open the log file in a text editor to view the logs. You should see the log messages that you wrote to the file, along with some additional information such as the timestamp and the severity level.
- Use your custom log file in your application
Now that you’ve created your custom log file and tested that it’s working correctly, you can use it in your application to track specific events or processes. You can write logs to your custom channel whenever you want to record something important that happens in your application.
For example, you could write a log to your custom channel whenever a user logs in to your application:
use Illuminate\Support\Facades\Log;
public function login(Request $request)
{
// perform login logic here
Log::channel('custom')->info('User ' . $request->email . ' logged
All the best nerd!