搜尋
首頁後端開發php教程CakePHP 日誌記錄

CakePHP 日誌記錄

Sep 10, 2024 pm 05:26 PM
phpcakephpPHP framework

登入 CakePHP 是一項非常簡單的任務。您只需使用一項功能即可。您可以記錄任何後台程序(如 cronjob)的錯誤、異常、使用者活動、使用者採取的操作。在 CakePHP 中記錄資料很容易。 log() 函數由 LogTrait 提供,它幾乎是所有 CakePHP 類別的共同祖先。

日誌配置

我們可以在檔案config/app.php中設定日誌。 檔案中有一個日誌部分,您可以在其中配置日誌選項,如下面的螢幕截圖所示。

CakePHP 日誌記錄

預設情況下,您將看到兩個日誌等級 - 錯誤偵錯 已為您設定。每個將處理不同層級的訊息。

CakePHP 支援各種日誌級別,如下所示 -

  • 緊急 - 系統無法使用

  • 警報 - 必須立即採取行動

  • 嚴重 - 嚴重狀況

  • 錯誤 - 錯誤條件

  • 警告 - 警告條件

  • 注意 - 正常但重要的情況

  • 訊息 - 資訊性訊息

  • 偵錯 - 偵錯等級訊息

寫入日誌檔

我們可以用兩種方式寫入日誌檔案。

第一個是使用靜態 write() 方法。以下是靜態 write() 方法的語法。

表>

第二種是使用log() 捷徑

函數,任何使用

LogTrait 的函數都可用,呼叫log() 將在內部呼叫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();
});

範例 config/routes.php 檔案中進行更改,如下列程式所示。

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');
      }
   }
?>

src/Controller/LogexsController.php 建立 LogexsController.php 檔案。 將以下程式碼複製到控制器檔案中。

src/Controller/LogexsController.php

Something is written in log file. Check log file logs\debug.log
src/Template

處建立目錄

Logexs

並在該目錄下建立一個名為 index.php 的

View

檔案。將以下程式碼複製到該文件中。

src/Template/Logexs/index.phpCakePHP 日誌記錄

CakePHP 日誌記錄透過造訪以下 URL 來執行上述範例。 http://localhost/cakephp4/logex 輸出 執行後,您將收到以下輸出。 日誌將會加入 log/debug.log 檔案 -
語法 write( 整數|字串 $level, 混合 $message, 字串|陣列 $context [] )
參數
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.

正在寫入的訊息的嚴重程度。該值必須是與已知等級相符的整數或字串。

要記錄的訊息內容。 用於記錄訊息的附加資料。可以傳遞特殊範圍鍵以用於進一步過濾要使用的日誌引擎。如果傳遞字串或數字索引數組,它將被視為範圍鍵。有關日誌記錄範圍的更多信息,請參閱 CakeLogLog::config()

回傳 布林值
描述

將給定的訊息和類型寫入所有配置的日誌適配器。配置的適配器會傳遞 $level 和 $message 變數。 $level 是以下字串/值之一。

以上是CakePHP 日誌記錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

您如何從PHP會話中檢索數據?您如何從PHP會話中檢索數據?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

您如何使用會議來實施購物車?您如何使用會議來實施購物車?May 01, 2025 am 12:10 AM

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

您如何在PHP中創建和使用接口?您如何在PHP中創建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解釋瞭如何創建,實施和使用PHP中的接口,重點關注其對代碼組織和可維護性的好處。

crypt()和password_hash()有什麼區別?crypt()和password_hash()有什麼區別?Apr 30, 2025 pm 03:39 PM

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

如何防止PHP中的跨站點腳本(XSS)?如何防止PHP中的跨站點腳本(XSS)?Apr 30, 2025 pm 03:38 PM

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器