#### 1. 新增日志通道配置 --- ``` // 其它日志通道配置 'log' => [ // 日志記錄方式 'type' => app\driver\Log::class, // 日志保存目錄 'path' => app()->getRootPath() . 'log', ], ``` #### 2. 自定義日志驅(qū)動(dòng)類 --- 自定義日志驅(qū)動(dòng),需要實(shí)現(xiàn) `think\contract\LogHandlerInterface` 接口 參考TP6.0官方文檔完全開發(fā)手冊(cè) : [https://www.kancloud.cn/manual/thinkphp6_0/1037616#_377](https://www.kancloud.cn/manual/thinkphp6_0/1037616#_377) ``` interface LogHandlerInterface { /** * 日志寫入接口 * @access public * @param array $log 日志信息 * @return bool */ public function save(array $log): bool; } ``` ``` <?php declare (strict_types = 1); namespace app\driver; use think\facade\Log as LogFacade; use think\contract\LogHandlerInterface; /** * 自定義日志驅(qū)動(dòng) */ class Log implements LogHandlerInterface { /** * 構(gòu)造方法 */ public function __construct() { // 獲取日志通道配置信息 $this->config = LogFacade::getChannelConfig('log'); } /** * 保存日志 * @param array $data */ public function save(array $data): bool { foreach ($data as $type => $item ) { foreach ( $item as $value ) { $this->write($type, ['type' => $type,'value' => $value]); } } return true; } /** * 執(zhí)行寫入文件 * @param [type] $type * @param [type] $data * @return void */ public function write($type, $data) { $path = implode('/', [$this->config['path'], $type . '.log']); if ( ! file_exists(dirname($path)) ) mkdir(dirname($path), 0777, true); $content = '------------ ' . date('Y-m-d H:i:s') . ' ------------' . PHP_EOL . PHP_EOL . var_export($data, true) . PHP_EOL . PHP_EOL; file_put_contents($path, $content, FILE_APPEND); } } ``` #### 3. 寫入日志 --- ``` use think\facade\Log; Log::channel('log')->record('測(cè)試日志信息'); ```