//2016-07-20
/**
* Register a file log handler.
*
* @param string $path
* @param string $level
* @return void
*/
public function useFiles($path, $level = 'debug')
{
$this->monolog->pushHandler($handler = new StreamHandler($path, $this->parseLevel($level)));
$handler->setFormatter($this->getDefaultFormatter());// set formatter by get the default format
}// Register a file log handler.
/**
* 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')
{
$this->monolog->pushHandler(
$handler = new RotatingFileHandler($path, $days, $this->parseLevel($level))
);
$handler->setFormatter($this->getDefaultFormatter());
}// a daily file file log handler
/**
* Register a Syslog handler.
*
* @param string $name
* @param string $level
* @return \Psr\Log\LoggerInterface
*/
public function useSyslog($name = 'laravel', $level = 'debug')
{
return $this->monolog->pushHandler(new SyslogHandler($name, LOG_USER, $level));
}// get a register Sys log Handler
/**
* Register an error_log handler.
*
* @param string $level
* @param int $messageType
* @return void
*/
public function useErrorLog($level = 'debug', $messageType = ErrorLogHandler::OPERATING_SYSTEM)
{
$this->monolog->pushHandler(
$handler = new ErrorLogHandler($messageType, $this->parseLevel($level))
);
$handler->setFormatter($this->getDefaultFormatter());
}
/**
* Register a new callback handler for when a log event is triggered.
*
* @param \Closure $callback
* @return void
*
* @throws \RuntimeException
*/
public function listen(Closure $callback)
{
if (! isset($this->dispatcher)) {
throw new RuntimeException('Events dispatcher has not been set.');
}
$this->dispatcher->listen('illuminate.log', $callback);
}//listen is a method
// register a new callback handler for when a log event is triggered.
/**
* Fires a log event.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function fireLogEvent($level, $message, array $context = [])
{
// If the event dispatcher is set, we will pass along the parameters to the
// log listeners. These are useful for building profilers or other tools
// that aggregate all of the log messages for a given "request" cycle.
if (isset($this->dispatcher)) {
$this->dispatcher->fire('illuminate.log', compact('level', 'message', 'context'));
}// isset this->dispatcher
// dispatcher fire
}//fire like start it,more use fire log Event
/**
* Format the parameters for the logger.
*
* @param mixed $message
* @return mixed
*/
protected function formatMessage($message)
{
if (is_array($message)) {//determine array ?
return var_export($message, true);// output
} elseif ($message instanceof Jsonable) {// a instanceof this Jsonable
return $message->toJson();//to Json
} elseif ($message instanceof Arrayable) {// instanceof Arrayable
return var_export($message->toArray(), true);// output
}
return $message;// other output message
}//format the parameters for the logger
/**
* Parse the string level into a Monolog constant.
*
* @param string $level
* @return int
*
* @throws \InvalidArgumentException
*/
protected function parseLevel($level)
{
if (isset($this->levels[$level])) {
return $this->levels[$level];
}// isset
throw new InvalidArgumentException('Invalid log level.');//throw Exception
}// change string to int like constant.
/**
* Get the underlying Monolog instance.
*
* @return \Monolog\Logger
*/
public function getMonolog()
{
return $this->monolog;
}//a big get ,magic get
/**
* Get a defaut Monolog formatter instance.
*
* @return \Monolog\Formatter\LineFormatter
*/
protected function getDefaultFormatter()
{
return new LineFormatter(null, null, true, true);
}// a default format is a line Format
/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Contracts\Events\Dispatcher
*/
public function getEventDispatcher()
{
return $this->dispatcher;
}//get dispatcher
/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function setEventDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}// set the dispatcher.
}
本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1828259