John Davidson

php - How can i create custom log file using laravel 4.2 version?

0 comments
Message:


I want to change the log format in the 4.2 version of laravel. But I have no idea about this.
Please help me to find the solution or guide me to solve my problem.


I searched a lot on google but still had no success.


Format


public const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";

Any solution appreciated!



My Answer:
To create a custom log file with a specific format in Laravel 4.2, you can follow these steps:

1. Create a new custom log channel in your `app/config/app.php` configuration file. Add the following code to the `channels` array:

php
'custom' => [
'driver' => 'daily',
'path' => storage_path('logs/custom.log'),
'level' => 'debug',
'days' => 14,
],


2. Create a new custom Monolog formatter in your `app/start/global.php` file. Add the following code:

php
use Monolog\Formatter\LineFormatter;

$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n");

Log::getMonolog()->getHandlers()[0]->setFormatter($formatter);


3. Now you can use the custom log channel in your application by calling the `Log::channel('custom')->info('Your log message here');` method.

This will create a new log file named `custom.log` in the `storage/logs` directory with the specified log format. You can customize the log format by modifying the `LineFormatter` parameters in the `app/start/global.php` file.

I hope this helps! Let me know if you have any further questions.

Rate this post

3 of 5 based on 9054 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us