Yii2 日志处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Yii2 日志处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4243字,纯文字阅读大概需要7分钟。
内容图文
最近开发一个新的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() { //日志格式自定义 回调方法 15if (Yii::$app === null) { 16return ‘‘; 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; 22return "[$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{ 3public$logFile; 4//rotation开关 如果开启,当日志文件大于maxFileSize设定的文件大小之后,就会自动切分日志 5public$enableRotation = true; 6public$maxFileSize = 10240; // in KB 7 //同一个文件名可以切分多少个文件 8public$maxLogFiles = 5; 9public$fileMode; //日志文件权限10public$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*/23public$rotateByCopy = true; 2425/** 26 * Rotates log files. 27*/28protectedfunction rotateFiles() 29 { 30$file = $this->logFile; 31for ($i = $this->maxLogFiles; $i >= 0; --$i) { 32// $i == 0 is the original log file33$rotateFile = $file . ($i === 0 ? ‘‘ : ‘.‘ . $i); 34if (is_file($rotateFile)) { 35// suppress errors because it‘s possible multiple processes enter into this section36if ($i === $this->maxLogFiles) { 37 @unlink($rotateFile); 38 } else { 39if ($this->rotateByCopy) { 40 @copy($rotateFile, $file . ‘.‘ . ($i + 1)); 41if ($fp = @fopen($rotateFile, ‘a‘)) { 42 @ftruncate($fp, 0); 43 @fclose($fp); 44 } 45if ($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{ 3if ($this->prefix !== null) { 4returncall_user_func($this->prefix, $message); 5 } 6 7if (Yii::$app === null) { 8return ‘‘; 9 } 1011$request = Yii::$app->getRequest(); 12$ip = $request instanceof Request ? $request->getUserIP() : ‘-‘; 1314/* @var $user \yii\web\User */15$user = Yii::$app->has(‘user‘, true) ? Yii::$app->get(‘user‘) : null; 16if ($user && ($identity = $user->getIdentity(false))) { 17$userID = $identity->getId(); 18 } else { 19$userID = ‘-‘; 20 } 2122/* @var $session \yii\web\Session */23$session = Yii::$app->has(‘session‘, true) ? Yii::$app->get(‘session‘) : null; 24$sessionID = $session && $session->getIsActive() ? $session->getId() : ‘-‘; 2526return "[$ip][$userID][$sessionID]"; 27 }
如果想要自定义日志格式前缀,可以配置回调函数(note:如果在回调中使用了特定的类需要在文件开头用“use”关键词 引入该类)
原文:http://www.cnblogs.com/guopc/p/6677192.html
内容总结
以上是互联网集市为您收集整理的Yii2 日志处理全部内容,希望文章能够帮你解决Yii2 日志处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。