Log class let's you write logs into custom log files.
Cake\Log\Log class:To write log data into custom file with default CakePHP Cake\Log\Log class feels like repeating ourselves.
Log::config('custom_file', [
'className' => 'File',
'path' => LOGS,
'levels' => ['debug'],
'file' => 'my_file.log',
]);
2. Write logs into file:
```php
Log::write('debug', "Something didn't work!");
With above approach, we have multiple issues:
custom_file.log file but also it will log into level specific $level.log file too, so we end up duplicating the log data.Or you hack it with doing something like setting configurations in bootstrap.php and use scope to log data. But each time you start new project you have to remember to copy paste that config and use in your project in order to write data into custom log files.
Tools\Utility\FileLog class:You can directly pass data to log and filename to write the data into.
use Tools\Utility\FileLog;
FileLog::write("Something didn't work!", 'my_file');
// Somewhere else in any file
FileLog::write([
'user' => [
'id' => '1',
'name' => 'John',
'email' => 'john@example.com',
]
], 'user_data');
That's it! Above will create two separate files in log/ directory named my_file.log and user_data.log store data into which we passed in first argument. By default if you don't pass the $filename in second param in FileLog::write method, it will create custom_log.log file.
You can write string, array, objects, etc into log files. It will pretty print your array/object so it's more readable. Also, it will not duplicate records into $level.log file.