码迷,mamicode.com
首页 > Web开发 > 详细

[李景山php]每天laravel-20160919|Writer-1

时间:2016-07-20 13:46:18      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:php

<?php

namespace Illuminate\Log;

use Closure;
use RuntimeException;
use InvalidArgumentException;
use Monolog\Handler\SyslogHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger as MonologLogger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Psr\Log\LoggerInterface as PsrLoggerInterface;
use Illuminate\Contracts\Logging\Log as LogContract;
// more namespace use as this
class Writer implements LogContract, PsrLoggerInterface
{// class writer implements LogContract, and PsrLoggerInterface
    /**
     * The Monolog logger instance.
     *
     * @var \Monolog\Logger
     */
    protected $monolog;// The Monolog logger instance. a

    /**
     * The event dispatcher instance.
     *
     * @var \Illuminate\Contracts\Events\Dispatcher
     */
    protected $dispatcher;//The event dispatcher instance.
   // a instance about this dispatcher

    /**
     * The Log levels.
     *
     * @var array
     */
    protected $levels = [
        ‘debug‘     => MonologLogger::DEBUG,
        ‘info‘      => MonologLogger::INFO,
        ‘notice‘    => MonologLogger::NOTICE,
        ‘warning‘   => MonologLogger::WARNING,
        ‘error‘     => MonologLogger::ERROR,
        ‘critical‘  => MonologLogger::CRITICAL,
        ‘alert‘     => MonologLogger::ALERT,
        ‘emergency‘ => MonologLogger::EMERGENCY,
    ];// a log levels ,use a log

    /**
     * Create a new log writer instance.
     *
     * @param  \Monolog\Logger  $monolog
     * @param  \Illuminate\Contracts\Events\Dispatcher  $dispatcher
     * @return void
     */
    public function __construct(MonologLogger $monolog, Dispatcher $dispatcher = null)
    {
        $this->monolog = $monolog;// set this monolog, set dispatcher

        if (isset($dispatcher)) {// determine this dispatcher
            $this->dispatcher = $dispatcher;// set this dispatcher
        }
    }//Create a new log writer instance.

    /**
     * Log an emergency message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function emergency($message, array $context = [])
    {// write log
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//Log an emergency message to the logs. use function name message and context

    /**
     * Log an alert message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function alert($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }// ditto

    /**
     * Log a critical message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function critical($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log an error message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function error($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log a warning message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function warning($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log a notice to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function notice($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log an informational message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function info($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log a debug message to the logs.
     *
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function debug($message, array $context = [])
    {
        return $this->writeLog(__FUNCTION__, $message, $context);
    }//ditto

    /**
     * Log a message to the logs.
     *
     * @param  string  $level
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function log($level, $message, array $context = [])
    {
        return $this->writeLog($level, $message, $context);
    }//ditto

    /**
     * Dynamically pass log calls into the writer.
     *
     * @param  string  $level
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    public function write($level, $message, array $context = [])
    {
        return $this->writeLog($level, $message, $context);
    }//ditto

    /**
     * Write a message to Monolog.
     *
     * @param  string  $level
     * @param  string  $message
     * @param  array  $context
     * @return void
     */
    protected function writeLog($level, $message, $context)
    {
        $this->fireLogEvent($level, $message = $this->formatMessage($message), $context);
       // start event ,like fire Log Event with levels message and context
       // get a right message format

        $this->monolog->{$level}($message, $context);//use this self function
    }


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1827951

[李景山php]每天laravel-20160919|Writer-1

标签:php

原文地址:http://jingshanls.blog.51cto.com/3357095/1827951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!