update v 1.0.7.5
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
<?php namespace Illuminate\Log;
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Log;
|
||||
|
||||
use Closure;
|
||||
use RuntimeException;
|
||||
@@ -15,369 +17,359 @@ use Illuminate\Contracts\Support\Arrayable;
|
||||
use Psr\Log\LoggerInterface as PsrLoggerInterface;
|
||||
use Illuminate\Contracts\Logging\Log as LogContract;
|
||||
|
||||
class Writer implements LogContract, PsrLoggerInterface {
|
||||
class Writer implements LogContract, PsrLoggerInterface
|
||||
{
|
||||
/**
|
||||
* The Monolog logger instance.
|
||||
*
|
||||
* @var \Monolog\Logger
|
||||
*/
|
||||
protected $monolog;
|
||||
|
||||
/**
|
||||
* The Monolog logger instance.
|
||||
*
|
||||
* @var \Monolog\Logger
|
||||
*/
|
||||
protected $monolog;
|
||||
/**
|
||||
* The event dispatcher instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* The event dispatcher instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
protected $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,
|
||||
];
|
||||
|
||||
/**
|
||||
* 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,
|
||||
];
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
if (isset($dispatcher)) {
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($dispatcher))
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Log an emergency message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an emergency message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an alert message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a critical message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a warning message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a notice to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an informational message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a debug message to the logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
return $this->writeLog(__FUNCTION__, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message to the logs.
|
||||
*
|
||||
* @param string $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
return $this->writeLog($level, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = array())
|
||||
{
|
||||
return $this->writeLog($level, $message, $context);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
$this->monolog->{$level}($message, $context);
|
||||
}
|
||||
|
||||
$this->monolog->{$level}($message, $context);
|
||||
}
|
||||
/**
|
||||
* 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)));
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
$handler->setFormatter($this->getDefaultFormatter());
|
||||
}
|
||||
/**
|
||||
* 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))
|
||||
);
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
$handler->setFormatter($this->getDefaultFormatter());
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a Syslog handler.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $level
|
||||
* @return void
|
||||
*/
|
||||
public function useSyslog($name = 'laravel', $level = 'debug')
|
||||
{
|
||||
return $this->monolog->pushHandler(new SyslogHandler($name, LOG_USER, $level));
|
||||
}
|
||||
/**
|
||||
* 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))
|
||||
);
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
$this->dispatcher->listen('illuminate.log', $callback);
|
||||
}
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires a log event.
|
||||
*
|
||||
* @param string $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @return void
|
||||
*/
|
||||
protected function fireLogEvent($level, $message, array $context = array())
|
||||
{
|
||||
// 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'));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Format the parameters for the logger.
|
||||
*
|
||||
* @param mixed $message
|
||||
* @return mixed
|
||||
*/
|
||||
protected function formatMessage($message)
|
||||
{
|
||||
if (is_array($message)) {
|
||||
return var_export($message, true);
|
||||
} elseif ($message instanceof Jsonable) {
|
||||
return $message->toJson();
|
||||
} elseif ($message instanceof Arrayable) {
|
||||
return var_export($message->toArray(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the parameters for the logger.
|
||||
*
|
||||
* @param mixed $message
|
||||
* @return void
|
||||
*/
|
||||
protected function formatMessage($message)
|
||||
{
|
||||
if (is_array($message))
|
||||
{
|
||||
return var_export($message, true);
|
||||
}
|
||||
elseif ($message instanceof Jsonable)
|
||||
{
|
||||
return $message->toJson();
|
||||
}
|
||||
elseif ($message instanceof Arrayable)
|
||||
{
|
||||
return var_export($message->toArray(), true);
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
throw new InvalidArgumentException('Invalid log level.');
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException("Invalid log level.");
|
||||
}
|
||||
/**
|
||||
* Get the underlying Monolog instance.
|
||||
*
|
||||
* @return \Monolog\Logger
|
||||
*/
|
||||
public function getMonolog()
|
||||
{
|
||||
return $this->monolog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying Monolog instance.
|
||||
*
|
||||
* @return \Monolog\Logger
|
||||
*/
|
||||
public function getMonolog()
|
||||
{
|
||||
return $this->monolog;
|
||||
}
|
||||
/**
|
||||
* Get a default Monolog formatter instance.
|
||||
*
|
||||
* @return \Monolog\Formatter\LineFormatter
|
||||
*/
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new LineFormatter(null, null, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a defaut Monolog formatter instance.
|
||||
*
|
||||
* @return \Monolog\Formatter\LineFormatter
|
||||
*/
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new LineFormatter(null, null, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event dispatcher instance.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
public function getEventDispatcher()
|
||||
{
|
||||
return $this->dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the event dispatcher instance.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
||||
* @return void
|
||||
*/
|
||||
public function setEventDispatcher(Dispatcher $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
/**
|
||||
* Get the event dispatcher instance.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Events\Dispatcher
|
||||
*/
|
||||
public function getEventDispatcher()
|
||||
{
|
||||
return $this->dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the event dispatcher instance.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
||||
* @return void
|
||||
*/
|
||||
public function setEventDispatcher(Dispatcher $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/contracts": "5.0.*",
|
||||
"illuminate/support": "5.0.*",
|
||||
"php": ">=5.5.9",
|
||||
"illuminate/contracts": "5.2.*",
|
||||
"illuminate/support": "5.2.*",
|
||||
"monolog/monolog": "~1.11"
|
||||
},
|
||||
"autoload": {
|
||||
@@ -26,7 +26,7 @@
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "5.0-dev"
|
||||
"dev-master": "5.2-dev"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
|
||||
Reference in New Issue
Block a user