Home >Backend Development >PHP Tutorial >CakePHP Logging
Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provided by the LogTrait, which is the common ancestor for almost all CakePHP classes.
We can configure the log in file config/app.php. There is a log section in the file, where you can configure logging options as shown in the following screenshot.
By default, you will see two log levels − error and debug already configured for you. Each will handle different level of messages.
CakePHP supports various logging levels as shown below −
Emergency − System is unusable
Alert − Action must be taken immediately
Critical − Critical conditions
Error − Error conditions
Warning − Warning conditions
Notice − Normal but significant condition
Info − Informational messages
Debug − Debug-level messages
There are two ways by which, we can write in a Log file.
The first is to use the static write() method. The following is the syntax of the static write() method.
Syntax | write( integer|string $level, mixed $message, string|array $context [] ) | ||||||||
---|---|---|---|---|---|---|---|---|---|
Parameters |
Message content to log. Additional data to be used for logging the message. The special scope key can be passed to be used for further filtering of the log engines to be used. If a string or a numerically index array is passed, it will be treated as the scope key. See CakeLogLog::config() for more information on logging scopes. |
||||||||
Returns | boolean | ||||||||
Description | Writes the given message and type to all of the configured log adapters. Configured adapters are passed both the $level and $message variables. $level is one of the following strings/values. |
The second is to use the log() shortcut
function available on any using theLogTrait Calling log() will internally call Log::write()
−<?php use Cake\Http\Middleware\CsrfProtectionMiddleware; use Cake\Routing\Route\DashedRoute; use Cake\Routing\RouteBuilder; $routes->setRouteClass(DashedRoute::class); $routes->scope('/', function (RouteBuilder $builder) { $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([ 'httpOnly' => true, ])); $builder->applyMiddleware('csrf'); //$builder->connect('/pages', ['controller'=>'Pages','action'=>'display', 'home']); $builder->connect('logex',['controller'=>'Logexs','action'=>'index']); $builder->fallbacks(); });
Example Make changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php namespace App\Controller; use App\Controller\AppController; use Cake\Log\Log; class LogexsController extends AppController{ public function index(){ /*The first way to write to log file.*/ Log::write('debug',"Something didn't work."); /*The second way to write to log file.*/ $this->log("Something didn't work.",'debug'); } } ?>
Create a LogexsController.php file at src/Controller/LogexsController.php. Copy the following code in the controller file.
src/Controller/LogexsController.phpCreate a directory
Something is written in log file. Check log file logs\debug.logLogexs
at
src/Templateand under that directory create a
ViewExecute the above example by visiting the following URL. http://localhost/cakephp4/logex Output Upon execution, you will receive the following output. The logs will be added to log/debug.log file −
The above is the detailed content of CakePHP Logging. For more information, please follow other related articles on the PHP Chinese website!