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

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 |
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.log
at
src/Templateand under that directory create a
Viewfile called index.php. Copy the following code in that file.
src/Template/Logexs/index.php

The above is the detailed content of CakePHP Logging. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
