Laravel log class

Description of the problem and improved methods

  1. Laravel any original log facade Log

    We all know that in the Laravel Log, its use is

    use Log;
    Log::info();
    

    But the existence of a problem, the print log is printed by Log facade to a Log file, is not conducive to log monitoring.

  2. Improved log facade KLog

    Facade KLog log by adding a custom print to different log files, and automatically log cut by the day.

Log achieve facade KLog

The so-called facade facade, simply summarized as static syntax to call the bottom of the class. Can refer to a detailed explanation of the facade of the core concept - facade (Facades)

In Laravel applications, the facade is to provide a type of access mode for objects in the container. The principle of the mechanism implemented by the Facade class. Laravel comes facade, as well as custom facade we create will inherit from IlluminateSupportFacadesFacade base class.

Facade class need only implement a method: getFacadeAccessor. It getFacadeAccessor method defines what parsed from the container, and then Facade base class uses magic method __callStatic () call to resolve objects from your facade in.

Thus Facade class implementation follows KLog

<?php
// filepath:app/Http/Facades/KLog.php
namespace AppHttpFacades;

use IlluminateSupportFacadesFacade;


/**
 * @see APPHttpLogicKLogLogic
 */
class KLog extends Facade
{
    protected static function getFacadeAccessor()
    {
       return 'KLog';//该方法的工作就是返回服务容器绑定类的别名
    }
}

KLog facade Facade inherit the base class and defines getFacadeAccessor method, the working method is to return the container alias service binding class when a static method KLog any class of user references, Laravel resolve KLog binding from the service container, and then parse All requests to call a method on the object out. getFacadeAccessorReturns aliases in config / app.php alias.

Mentioned earlier 门面就是一个为容器中对象提供访问方式的类, what service container is it? You can refer to the core concept - service container . As mentioned service container, it will inevitably mention the large column  Laravel log class rel = "noopener noreferrer"> service provider. Laravel application service provider is to start in the center of your own applications as well as all Laravel core services are initiated by the service provider.

KLog class service providers to achieve the following:

<?php
// filepath:app/Providers/KLogProvider.php
namespace AppProviders;

use IlluminateSupportServiceProvider;

class KLogProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('KLog', function () {
            return new AppHttpLogicKLogLogic();
        });
    }
}

In the register method, the only thing you have to do is to tie things service container.

Since service providers and facade classes have been created, then the next we need to be configured in config/app.phpthe following configurations:

a. aliases increased KLog configuration,
B. Providers increased KLogProvider

'aliases' => [
    ...
    'View' => IlluminateSupportFacadesView::class,
    'KLog' => AppHttpFacadesKLog::class,

],
'providers' => [
    ...
    AppProvidersRouteServiceProvider::class,
    AppProvidersKLogProvider::class,
],

Of course, we forgot one important thing, and that is KLogLogic class implementation, this is the real underlying class facade KLog call.

<?php
namespace AppHttpLogic;

use MonologLogger;
use MonologHandlerStreamHandler;
use IlluminateLogWriter;

class KLogLogic 
{
    private static $writers = null;
    const MAX_FILES = 5; // 最多保留MAX_FILES天的日志

    public function getBizProc()
    {
        return $this->getLogger('biz_proc');
    }

    public function getBizProcErr()
    {
        return $this->getLogger('biz_proc_err');
    }

    private function getLogger($channel_name)
    {
        if (isset(self::$writers[$channel_name])) {
            return self::$writers[$channel_name];
        }

        $new_writer = new Writer(new Logger($channel_name));
        $log_file_path = storage_path() . '/logs/' . $channel_name . '.log';
        $new_writer->useDailyFiles($log_file_path, self::MAX_FILES);

        self::$writers[$channel_name] = $new_writer;
        return self::$writers[$channel_name];
    }
}

When that is done, how we use it KLog facade? Very simple, notice of and behind the facade Log info parameters the same, because the interface is called class Writer

use KLog;

KLog::getBizProcErr()->info($msg);
KLog::getBizProcErr()->notice($msg);

If you want to add a new log, so you can add new methods in KLogLogic in.

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11874525.html