English 中文(简体)
CakePHP - Logging
  • 时间:2024-12-22

CakePHP - Logging


Previous Page Next Page  

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 pke 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.

Logging Configuration

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.

Programming

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

Writing to Log file

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

The severity level of the message being written. The value must be an integer or string matching a known level.

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 the LogTrait Calpng log() will internally call Log::write()

Example

Make changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php
use CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$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();
});

Create a LogexsController.php file at src/Controller/LogexsController.php. Copy the following code in the controller file.

src/Controller/LogexsController.php

<?php
   namespace AppController;
   use AppControllerAppController;
   use CakeLogLog;
   class LogexsController extends AppController{
      pubpc 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 directory Logexs at src/Template and under that directory create a View file called index.php. Copy the following code in that file.

src/Template/Logexs/index.php

Something is written in log file. Check log file logsdebug.log

Execute the above example by visiting the following URL.

http://localhost/cakephp4/logex

Output

Upon execution, you will receive the following output.

Debugs

The logs will be added to log/debug.log file −

Logfile Advertisements