您现在的位置是:网站首页>PHPPHP

Laravel中自定义日志目录

左鹏07-05 10:34:34PHP3,553人已围观

简介https://learnku.com/articles/7125/custom-log-directory-in-laravel   通过以上思路自己写了个公共类如下 <?php // +---------------------------------------------------------------------- // | Laravel // +-------------------------------------------------

https://learnku.com/articles/7125/custom-log-directory-in-laravel

 

通过以上思路自己写了个公共类如下

<?php
// +----------------------------------------------------------------------
// | Laravel
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2020 http://ganhuoche.com All rights reserved.
// +----------------------------------------------------------------------
// | Author     : zuopeng <zuopeng@ganhuoche.com>
// +----------------------------------------------------------------------
// | CreateTime : 2019/7/5 10:46 
// +----------------------------------------------------------------------
// | 使用方式:LogCommon::info('info'); or LogCommon::init(['dir'=>'dir'])->info('info');
// +----------------------------------------------------------------------


namespace App\Services\Admin\Common;


class LogCommon
{
    //根目录
    protected static $pathRoot  = 'logs/';
    //默认目录
    protected static $path      = 'admin/';
    //自定义文件夹
    protected static $dir       = '';

    /**
     * @param array $param
     * @return LogCommon
     */
    public static function init($param = []){
        if(isset($param['path'])){
            self::$path = $param['path'];
        }
        if(isset($param['dir'])){
            self::$dir = $param['dir'];
        }

        return new self();
    }

    private static function initLog(){
        $monolog = \Log::getMonolog();
        $monolog->popHandler();
        \Log::useDailyFiles(storage_path(self::$pathRoot.self::$path.self::$dir.'info.log'));
    }

    public static function info($str=''){
       self::initLog();
        \Log::info($str);
    }

    public static function error($str=''){
        self::initLog();
        \Log::error($str);
    }

    /**
     * @param string $dir
     */
    public static function setDir(string $dir)
    {
        self::$dir = $dir;
    }

    /**
     * @param string $path
     */
    public static function setPath(string $path)
    {
        self::$path = $path;
    }

}

 

文章评论

    请先说点什么
    热门评论
    0人参与,0条评论
    正在载入评论列表...

    站点信息

    • 建站时间:2018-09-18
    • 网站程序:Spring Boot
    • 主题模板:《今夕何夕》
    • 文章统计:104条
    • 微信公众号:扫描二维码,关注我们
    登陆您的账户