小丑路人 2017-10-27 09:34:32 5924次浏览 2条评论 3 1 0

进入 \vendor\yiisoft\yii2\log\FileTarget.php

第一步:定义变量:

只为扩展,这个根据需要,我建议添加,配置你需要对应的格式就好了

/**
 * @var 日志是以日期文件夹区分还是以日期下标开头,默认为日期下标拼接.
 * Log is divided by date folder or date index, and default is date index stitching
 *
 * annotationChinese:这个参数因人而异,我只做扩展,可不用。如果你要写死一种格式,那么不用添加这个变量就好了。
 * annotationEnglish:This parameter varies from person to person, and I just do extensions without using it. 
 *                    If you want to write a dead form, you don't have to add this variable.
 */
public $file_type = 0;

第二步:修改init内置方法为:

public function init(){
    parent::init();
    if ($this->logFile === null) {
        $this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
    } else {
        /**
         * 对日志进行日期文件夹管理 开始
         */
        $arr = explode('/', $this->logFile);
        $logfile_name       =       $arr[count($arr)-1];
        if($this->file_type == 0){ //日期格式开头拼接
            $this->logFile = iconv("UTF-8", "GBK", str_replace($logfile_name, date('Y-m-d').'_'.$logfile_name, $this->logFile));
        }else{ //日期文件夹区分
            $dir = iconv("UTF-8", "GBK", str_replace($logfile_name, date('Y-m-d'), $this->logFile));
            if(!file_exists($dir)) mkdir ($dir,0777,true);
            $this->logFile = $dir.'/'.$logfile_name;    
        }
        unset($logfile_name, $arr);
        /**
         * 对日志进行日期文件夹管理 结束
         */
        $this->logFile = Yii::getAlias($this->logFile);
    }
    $logPath = dirname($this->logFile);
    if (!is_dir($logPath)) FileHelper::createDirectory($logPath, $this->dirMode, true);
    if ($this->maxLogFiles < 1) $this->maxLogFiles = 1;
    if ($this->maxFileSize < 1) $this->maxFileSize = 1;
}

配置完毕!

使用方式:

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning', 'info'],
            'logFile' => '',
            'file_type' =>  0,//设置文件存储的格式就好了 0:日期下划线拼接;1:日期文件夹区分
        ],
    ],      
],
觉得很赞
  • 评论于 2018-02-24 10:42 举报

    本来用个匿名方法就行了,你倒好直接改了框架,还升级吗?

    1 条回复
    评论于 2018-03-05 16:18 回复

    以前随便改的玩

  • 评论于 2019-04-08 11:59 举报

    本来用个匿名方法就行了,你倒好直接改了框架,还升级吗?

您需要登录后才可以评论。登录 | 立即注册