twn39 / code

:memo: 代码笔记,通过 issue 的方式记录日常遇到的问题和学习笔记
13 stars 1 forks source link

Laravel 日志 MongoDB 存储 #48

Open twn39 opened 9 years ago

twn39 commented 9 years ago

将日志存储到mongodb中,可用于日志分析。

twn39 commented 9 years ago
<?php namespace App\Http\Controllers;

use Monolog\Handler\MongoDBHandler;
class HomeController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Home Controller
    |--------------------------------------------------------------------------
    |
    | This controller renders your application's "dashboard" for users that
    | are authenticated. Of course, you are free to change or remove the
    | controller as you wish. It is just here to get your app started!
    |
    */
    private $log;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->log = \Log::getMonolog();
        $logHandler = new MongoDBHandler(new \MongoClient(), 'laravel', 'logs');

        $this->log->pushHandler($logHandler);
    }

    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {
        $this->log->addInfo('mongodb log test');
        return view('home');
    }

}