Laravel 自定义日志目录
第一种方式
1.自定义一个日志类文件
namespace App\Help;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Illuminate\Log\Writer;
class Customer
{
// 所有的LOG都要求在这里注册
const LOG_ERROR = 'error';
private static $loggers = array();
// 获取一个实例
public static function getLogger($type = self::LOG_ERROR, $day = 30)
{
if (empty(self::$loggers[$type])) {
self::$loggers[$type] = new Writer(new Logger($type));
self::$loggers[$type]->useDailyFiles(storage_path().'/logs/'. $type .'.log', $day); //存储位置随便修改
}
$log = self::$loggers[$type];
return $log;
}
}
2.控制器代码
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Help\Customer;
use Log;
class TestController extends Controller
{
//自定义log目录
public function testLog()
{
Customer::getLogger('crontab')->error(date('Y-m-d H:i:s').' searche update ok');
}
}
3.结果展示
第二种方式
1.在config/logging.php 中, 创建自定义频道
2.使用时指定频道