最近开发一个新的PHP项目,终于脱离了某框架的魔爪(之前被折磨的不轻),选用了江湖中如雷贯耳的Yii2框架。每个项目代码的运行,日志是必不可少的,在开发中踩了一遍Yii2日志管理的坑,看过很多网上对Yii2日志的配置介绍,今天总结一下Yii2对日志的处理分享给大家。
1.首先看一下log配置:
1 return [ 2 ‘traceLevel‘ => YII_DEBUG ? 3 : 0, 3 ‘targets‘ => [ //可以配置多个log 4 [ 5 ‘class‘ => ‘yii\log\FileTarget‘, //Yii2处理日志的类 6 ‘levels‘ => [‘error‘, ‘warning‘, ‘info‘], //设置日志记录的级别 7 ‘categories‘ => [‘user‘], //自定义日志分类 8 ‘maxFileSize‘ => 1024 * 20, //设置文件大小,以k为单位 9 ‘logFile‘ => ‘@runtime/../logs/user‘.date(‘Ymd‘), //自定义文件路径 (一般项目的日志会打到服务器的其他路径,需要修改相应目录的权限哦~) 10 ‘logVars‘ => [‘_POST‘], //捕获请求参数 11 ‘fileMode‘ => 0775, //设置日志文件权限 12 ‘maxLogFiles‘ => 100, //同个文件名最大数量 13 ‘rotateByCopy‘ => false, //是否以复制的方式rotate 14 ‘prefix‘ => function() { //日志格式自定义 回调方法 15 if (Yii::$app === null) { 16 return ‘‘; 17 } 18 $request = Yii::$app->getRequest(); 19 $ip = $request instanceof Request ? $request->getUserIP() : ‘-‘; 20 $controller = Yii::$app->controller->id; 21 $action = Yii::$app->controller->action->id; 22 return "[$ip][$controller-$action]"; 23 }, 24 ], 25 ];
2.日志记录
Yii::trace():记录一条消息去跟踪一段代码是怎样运行的。这主要在开发的时候使用。
Yii::info():记录一条消息来传达一些有用的信息。
Yii::warning():记录一个警告消息用来指示一些已经发生的意外。
Yii::error():记录一个致命的错误,这个错误应该尽快被检查。
eg: Yii::info(‘the log content‘, ‘user‘);
第二个参数可以是自定义的日志分类,对应配置文件中categories字段。
3.日志组件调用
log配置通过web.php(基础模板web.php 高级模板main.php)以component方式加载到应用对象Yii::$app中。
4.日志切分
./vendor/yiisoft/yii2/log/FileTarget.php
1 class FileTarget extends Target 2 { 3 public $logFile; 4 //rotation开关 如果开启,当日志文件大于maxFileSize设定的文件大小之后,就会自动切分日志 5 public $enableRotation = true; 6 public $maxFileSize = 10240; // in KB 7 //同一个文件名可以切分多少个文件 8 public $maxLogFiles = 5; 9 public $fileMode; //日志文件权限 10 public $dirMode = 0775; 11 /** 12 * @var bool Whether to rotate log files by copy and truncate in contrast to rotation by 13 * renaming files. Defaults to `true` to be more compatible with log tailers and is windows 14 * systems which do not play well with rename on open files. Rotation by renaming however is 15 * a bit faster. 16 * 17 * The problem with windows systems where the [rename()](http://www.php.net/manual/en/function.rename.php) 18 * function does not work with files that are opened by some process is described in a 19 * [comment by Martin Pelletier](http://www.php.net/manual/en/function.rename.php#102274) in 20 * the PHP documentation. By setting rotateByCopy to `true` you can work 21 * around this problem. 22 */ 23 public $rotateByCopy = true; 24 25 /** 26 * Rotates log files. 27 */ 28 protected function rotateFiles() 29 { 30 $file = $this->logFile; 31 for ($i = $this->maxLogFiles; $i >= 0; --$i) { 32 // $i == 0 is the original log file 33 $rotateFile = $file . ($i === 0 ? ‘‘ : ‘.‘ . $i); 34 if (is_file($rotateFile)) { 35 // suppress errors because it‘s possible multiple processes enter into this section 36 if ($i === $this->maxLogFiles) { 37 @unlink($rotateFile); 38 } else { 39 if ($this->rotateByCopy) { 40 @copy($rotateFile, $file . ‘.‘ . ($i + 1)); 41 if ($fp = @fopen($rotateFile, ‘a‘)) { 42 @ftruncate($fp, 0); 43 @fclose($fp); 44 } 45 if ($this->fileMode !== null) { 46 @chmod($file . ‘.‘ . ($i + 1), $this->fileMode); 47 } 48 } else { 49 // linux下用rename方式 50 @rename($rotateFile, $file . ‘.‘ . ($i + 1)); 51 } 52 } 53 } 54 } 55 } 56 }
5.日志前缀
prefix:如果没有配置,默认调用./vendor/yiisoft/yii2/log/Target.php
1 public function getMessagePrefix($message) 2 { 3 if ($this->prefix !== null) { 4 return call_user_func($this->prefix, $message); 5 } 6 7 if (Yii::$app === null) { 8 return ‘‘; 9 } 10 11 $request = Yii::$app->getRequest(); 12 $ip = $request instanceof Request ? $request->getUserIP() : ‘-‘; 13 14 /* @var $user \yii\web\User */ 15 $user = Yii::$app->has(‘user‘, true) ? Yii::$app->get(‘user‘) : null; 16 if ($user && ($identity = $user->getIdentity(false))) { 17 $userID = $identity->getId(); 18 } else { 19 $userID = ‘-‘; 20 } 21 22 /* @var $session \yii\web\Session */ 23 $session = Yii::$app->has(‘session‘, true) ? Yii::$app->get(‘session‘) : null; 24 $sessionID = $session && $session->getIsActive() ? $session->getId() : ‘-‘; 25 26 return "[$ip][$userID][$sessionID]"; 27 }
如果想要自定义日志格式前缀,可以配置回调函数(note:如果在回调中使用了特定的类需要在文件开头用“use”关键词 引入该类)
时间: 2024-10-06 00:46:38