Laravel Logging Extension

I am again having more problems with Laravel, as I have problems with understanding.

Again, I'm trying to create a package for my own logging. After doing some additional reading and looking at the main code and trying other approaches, I came to the conclusion that all I need to do is expand the main functions of laravel logging so that it is logged in a different way using a custom formatter.

I created my package. Here is my class of service provider:   

use Illuminate\Log\LogServiceProvider;

class VmlogServiceProvider extends LogServiceProvider {


    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        App::bind('log', function()
        {
            return new Vm\Vmlog\Vmlog;
        });     
        parent::boot();

    }

}

?>

Here is the VmLog class

<?php namespace Vm\Vmlog;

use App;
use Illuminate\Support\ServiceProvider;
use Log;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;

class Vmlog extends \Illuminate\Log\Writer {


    protected $path;
    protected $formatter;
    protected $stream;
    protected $rotatingStream;

    public function __construct() {

        $output = APP_HOST."|%datetime%|%level%|%level_name%|".__METHOD__."|%message%|%context%".PHP_EOL;
        $this->path = VM_LOGPATH.APP_VLCODE."/".APP_VLCODE."_".APP_INSTANCE.".log";
        $this->formatter = new LineFormatter($output, $dateFormat);

        parent::__construct();
    }

    /**
     * Register a file log handler.
     *
     * @param  string  $path
     * @param  string  $level
     * @return void
     */
    public function useFiles($path, $level = 'debug')
    {
        $level = $this->parseLevel($level);

        $this->stream = new StreamHandler($this->path, $level);
        $this->stream->setFormatter($this->formatter);

        $this->monolog->pushHandler($this->stream);
    }

    /**
     * Register a daily file log handler.
     *
     * @param  string  $path
     * @param  int     $days
     * @param  string  $level
     * @return void
     */
    public function useDailyFiles($path, $days = 0, $level = 'debug')
    {
        $level = $this->parseLevel($level);
        $this->rotatingStream = new RotatingFileHandler($this->path, $days, $level);
        $this->rotatingStream->setFormatter($this->formatter);

        $this->monolog->pushHandler($this->rotatingStream);
    }

}

?>

I commented on LogServiceProvider in app.php and added it to my VmlogServiceProvider in this place.

However, when I try to run things, I get the following error.

Call the undefined method Illuminate \ Support \ Facades \ Log :: useDailyFiles ()

, . LogServiceProvider, , ( ). ?

+3
2

?

Laravel

, register boot ?

, , . ? , boot "log", register . (, ? , ).

, Laravel.

LogServiceProvider:

<?php namespace Fideloper\Log;

use Illuminate\Support\ServiceProvider;

class LogServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $logger = new Writer(new \Monolog\Logger('my-custom-log'), $this->app['events']);

        $logFile = 'my-custom-log.txt';

        $logger->useDailyFiles(storage_path().'/logs/'.$logFile);

        $this->app->instance('fideloper.log', $logger);

        $this->app->bind('Fideloper\Log\Writer', function($app)
        {
            return $app->make('fideloper.log');
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('fideloper.log');
    }

}

( , ):

<?php namespace Fideloper\Log;

use Illuminate\Log\Writer as BaseWriter;

class Writer extends BaseWriter {}

, , .

A con , Log . Log::whatever() - Laravel. Fideloper\Log\Writer, - Laravel .

$log = App::make('fideloper.log');

// Or get auto-created by laravel by making it a dependency
//   in a controller, for example:
<?php

use Fideloper\Log\Writer

class SomeController extends BaseController {

    public function __construct(Writer $log)
    {
        $this->log = $log;

        //Later
        $this->log->error('SOME CUSTOM ERROR');
    }

}
+6

, .

LogServiceProvider, Laravel Writer Vmlog, . , , , . Laravel Log app.php.

ServiceProvider.

<?php namespace vm\Vmlog;

use Monolog\Logger;
use Illuminate\Log\LogServiceProvider;
use Illuminate\Support\ServiceProvider;

class VmlogServiceProvider extends LogServiceProvider {

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        $this->package('vm/vmlog');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $logger = new Vmlog(new Logger('log'), $this->app['events']);

        $this->app->instance('log', $logger);

        if (isset($this->app['log.setup']))
        {
            call_user_func($this->app['log.setup'], $logger);
        }
    }
}

?>

Vmlog, Writer.

<?php namespace vm\Vmlog;

use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Events\Dispatcher;
use Monolog\Logger as MonologLogger;


class Vmlog extends \Illuminate\Log\Writer {


    protected $path;
    protected $dateFormat;
    protected $output;

    public function __construct(MonologLogger $monolog, Dispatcher $dispatcher = null) 
    {
        // Do stuff
        $this->dateFormat = 'Y-m-d\TH:i:s';
        $this->output = "|%datetime%|%level%|%level_name%|%message%|%context%".PHP_EOL;

        parent::__construct($monolog, $dispatcher);
    }

    /**
     * Register a file log handler.
     *
     * @param  string  $path
     * @param  string  $level
     * @return void
     */
    public function useFiles($path, $level = 'debug')
    {
        $level = $this->parseLevel($level);
        $this->path = VM_LOGPATH.APP_VLCODE."/".APP_VLCODE."_".APP_INSTANCE.".log";
        $formatter = new LineFormatter(APP_HOST.$this->output, $this->dateFormat);
        $stream = new StreamHandler($this->path, $level);
        $stream->setFormatter($formatter);

        $this->monolog->pushHandler($stream);
    }

    /**
     * Register a daily file log handler.
     *
     * @param  string  $path
     * @param  int     $days
     * @param  string  $level
     * @return void
     */
    public function useDailyFiles($path, $days = 0, $level = 'debug')
    {
        $level = $this->parseLevel($level);
        $this->path = VM_LOGPATH.APP_VLCODE."/".APP_VLCODE."_".APP_INSTANCE.".log";
        $formatter = new LineFormatter(APP_HOST.$this->output, $this->dateFormat);
        $stream = new RotatingFileHandler($this->path, $days, $level);
        $stream->setFormatter($formatter);

        $this->monolog->pushHandler($stream);
    }

}

?>

, , .

+1

Source: https://habr.com/ru/post/1606369/


All Articles