CakePHP 日誌

王林
發布: 2024-08-29 12:58:22
原創
348 人瀏覽過

CakePHP 框架為開發人員提供不同類型的功能,並且是開源工具。例如,從開發角度來看,日誌記錄非常重要;使用日誌可以輕鬆發現錯誤、異常、不同使用者活動、操作過程中的使用者操作以及任何後台活動等方面的問題。通常,實施日誌記錄是一項非常簡單且節省時間的活動。在 CakePHP 中,我們有一個 log() 函數來記錄不同的活動、錯誤和異常。對於Log的實現,我們需要根據自己的需求配置不同的參數。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

如何建立CakePHP日誌?

現在讓我們看看如何建立登入 CakePHP,如下所示。

在 CakePHP 中記錄資訊很簡單 - log() 工作由 LogTrait 給出,它是某些 CakePHP 類別的正常前身。如果設定是 CakePHP 類別(控制器、元件和檢視),您可以記錄您的資訊。您也可以直接使用 Log::write()。請參閱寫入日誌。

日誌流監督器對於您的應用程式或模組的一部分至關重要。例如,如果您有一個名為 DatabaseLog 的資料庫伐木工人作為您的應用程式的元件;它將被設定在

application/Lib/Log/Engine/DatabaseLog.php.

假設您有一個資訊庫 lumberjack 作為模組元件,它將被放置在 application/Plugin/LoggingPack/Lib/Log/Engine/DatabaseLog.php 中。安排好後,CakeLog 會盡力堆疊配置日誌流,這是透過呼叫 CakeLog::config() 來完成的。安排我們的資料庫日誌類似:

App::uses('BaseLog', 'Log/Engine');
class DatabaseLog extends BaseLog {
public function __construct($options = array()) {
parent::__construct($options);
// ...
}
public function write($specified type, $required message) {
// write to the database.
}
}
登入後複製

雖然 CakePHP 除了應該執行組合技術之外對日誌流沒有任何先決條件,但擴展 BaseLog 類別有幾個優點:

它因此處理投影的程度和類型爭用。最後,它執行 config() 技術,預期使仔細的日誌記錄工作。

每個記錄器的撰寫技術都應該接受兩個邊界:$type 和 $message(按特定順序)。 $type 是記錄訊息的字串排序;基本信念是錯誤、警告、資訊和故障排除。此外,您可以在呼叫 CakeLog::write 時利用它們來表徵您的類型。

如何在CakePHP中設定登入?

現在讓我們看看如何設定登入 CakePHP,如下所示。

首先,我們需要設定app.php檔案;我們可以使用不同的選項來設定日誌部分。

'Log' => [
'debug'=>[
'className'=> 'Cake\Log\Engine\FileLog',
'path' => LOGS,
'file'=> 'debug',
'levels' => ['specified notice', 'required info', 'debug'],
'url' => env('Specified URL', null),
],
'error' => [
'className'=> 'Cake\Log\Engine\FileLog',
'path' => LOGS,
'file'=> 'error',
'levels' => ['specified warning', 'error', 'critical', 'alert'],
'url' => env('Specified URL', null),
],
},
登入後複製

說明

在上面的程式碼中,我們可以看到預設;我們有兩個日誌等級:我們已經配置的錯誤和偵錯,它處理不同層級的訊息。所以基本上,CakePHP 支援不同等級的日誌記錄,如下所示。

  • 緊急:用於顯示系統無法使用。
  • 警報:它顯示我們需要立即採取什麼行動。
  • 臨界:用於臨界條件。
  • Error: 用來顯示錯誤狀況。
  • 警告:用於顯示警告條件。
  • 注意:用來顯示執行的重要條件。
  • 訊息:用於根據要求顯示資訊性訊息。
  • 偵錯:用於顯示所有訊息的偵錯等級。

CakePHP 日誌檔

現在讓我們看看CakePHP中的日誌檔案如下。

我們可以用兩種不同的方式寫入日誌記錄。

第一個是利用靜態 write() 技術。接下來是靜態 write() 技術的語言結構。

文法:

write(integer|string $level, blended $message, string|array $context [])
登入後複製

參數:

所撰寫訊息的嚴重程度。值應該是與已知等級相符的整數或字串。

要記錄的訊息內容。

用來記錄訊息的額外資訊。可以傳遞唯一的擴展密鑰以用於對日誌馬達進行額外的分離。如果傳遞了一個字串或一個數學記錄,它將被視為度數鍵。有關日誌記錄程度的更多數據,請參閱 CakeLogLog::config()。

回傳: 布爾值

描述:將給定的訊息和類型寫入所有設計的日誌連接器。排列好的連接器透過 $level 和 $message 因子傳遞。 $level 是隨附的字串/值之一。第二種是使用 log() 簡單的路線工作,任何使用 LogTrait 的人都可以呼叫 log() 將在內部呼叫 Log::write()。

CakePHP Log Example

Now let’s see an example of a log file as follows.

First, we need to make the changes in the routes.php file as follows.

<?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 message', 'home page']);
$builder->connect('logex',['controller'=>'Logexs','action'=>'index']);
$builder->fallbacks();
});
登入後複製

Now we need to create controller.php and write the following code as follows.

<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Log\Log;
class LogexsController extends AppController{
public function index(){
/*First way to write log file.*/
Log::write('debug', "Something went wrong during execution.");
/* Second way to write log file.*/
$this->log("Something went wrong during execution.", 'debug');
}
}
?>
登入後複製

Create an index.php file under the view directory and write the following code.

Something went wrong during execution. Need to check log file\debug.log

Now we need to execute this file in localhost, and after execution, we will do some output that we illustrated by using the following screenshot.

CakePHP 日誌

After execution debug log was added to the debug.log file, as shown in the following screenshot.

CakePHP 日誌

Conclusion

We hope from this article you learn more about the CakePHP log. From the above article, we have taken in the essential idea of the CakePHP log and see the representation and example of the CakePHP log. Furthermore, this article taught us how and when to use the CakePHP log.

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

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!