語法 |
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 是以下字串/值之一。 |
表>
第二種是使用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.php
透過造訪以下 URL 來執行上述範例。
http://localhost/cakephp4/logex
輸出
執行後,您將收到以下輸出。
日誌將會加入 log/debug.log 檔案 -