Yii2自带日志记录,但用起来感觉比较不是很顺手,故自己封装了个方法,如下:

  

复制代码
 1 /**
 2      * 记录日志
 3      *
 4      * @param type $msg
 5      * @time 2015年8月31日17:46:20
 6      * @author rentingshuang <tingshuang@rrkd.cn>
 7      */
 8     public static function log($msg = '', $fileName = '', $dir = '') {
 9         $directory = date ( 'Y-m-d' );
10         $fileName = ! empty ( $fileName ) ? ($fileName . '.log') : ($directory . 'app.log');
11         if (! empty ( $dir )) {
12             $directory .= DIRECTORY_SEPARATOR . $dir;
13         }
14         $log = new FileTarget ();
15         if (! empty ( Yii::$app->params ['LogDir'] )) {
16             $log->logFile = Yii::$app->params ['LogDir'] . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . $fileName;
17         } else {
18             $log->logFile = Yii::$app->getRuntimePath () . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . $fileName;
19         }
20         $log->messages [] = [ 
21                 $msg 
22         ];
23         $log->export ();
24     }
复制代码

使用如下:

  XX::log('这是日志内容','文件名','目录');

  Fun::log ( '读取导入文件异常:' . $e->getMessage (), 'importEx', 'importEx' );

posted on   guanglixing16  阅读(671)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示