enhance exception + fix

This commit is contained in:
Nicolas Le Goff
2012-01-12 18:50:57 +01:00
parent 9de91e25da
commit a538f1e454

View File

@@ -25,33 +25,50 @@ use Alchemy\Phrasea\Core,
class Monolog extends ServiceAbstract implements ServiceInterface class Monolog extends ServiceAbstract implements ServiceInterface
{ {
const DEFAULT_MAX_DAY = 10; const DEFAULT_MAX_DAY = 10;
protected $handlers = array( protected $handlers = array(
'rotate' => 'RotatingFile' 'rotate' => 'RotatingFile'
,'stream' => 'Stream' , 'stream' => 'Stream'
); );
/** /**
* *
* @var \Monolog\Logger * @var \Monolog\Logger
*/ */
protected $monolog; protected $monolog;
public function __construct($name, Array $options) public function __construct($name, Array $options)
{ {
parent::__construct($name, $options); parent::__construct($name, $options);
if (empty($options))
{
throw new \Exception(sprintf("'%s' service options can not be empty", $this->name));
}
//defaut to main handler //defaut to main handler
$handler = isset($options["handler"]) ? $options["handler"] : false; $handler = isset($options["handler"]) ? $options["handler"] : false;
if (!$handler) if (!$handler)
{ {
throw new \Exception("You must specify at least one monolog handler"); throw new \Exception(sprintf(
"You must specify at least one handler for %s service"
, $this->name
)
);
} }
if (!array_key_exists($handler, $this->handlers)) if (!array_key_exists($handler, $this->handlers))
{ {
throw new \Exception(sprintf('Unknow monolog handler %s'), $handlerType); throw new \Exception(sprintf(
"The handler type '%s' declared in %s %s service is not valid.
Available types are %s."
, $handler
, $this->name
, $this->getScope()
, implode(", ", $this->handler)
)
);
} }
$handlerName = $this->handlers[$handler]; $handlerName = $this->handlers[$handler];
@@ -60,21 +77,31 @@ class Monolog extends ServiceAbstract implements ServiceInterface
if (!class_exists($handlerClassName)) if (!class_exists($handlerClassName))
{ {
throw new \Exception(sprintf('Unknow monolog handler class %s', $handlerClassName)); throw new \Exception(sprintf(
'Unable to log monolog handler ùs looked for class %s'
, $handlerName
, $handlerClassName)
);
} }
if (!isset($options["filename"])) if (!isset($options["filename"]))
{ {
throw new \Exception('you must specify a file to write "filename: my_filename"'); throw new \Exception(sprintf(
"Missing filename option in '%s' service"
, $this->name
)
);
} }
$logPath = __DIR__ . '/../../../../logs'; $logPath = __DIR__ . '/../../../../logs';
$file = sprintf('%s/%s', $logPath, $options["filename"]); $file = sprintf('%s/%s', $logPath, $options["filename"]);
if ($handler == 'rotate') if ($handler == 'rotate')
{ {
$maxDay = isset($options["max_day"]) ? (int) $options["max_day"] : self::DEFAULT_MAX_DAY; $maxDay = isset($options["max_day"]) ?
(int) $options["max_day"] :
self::DEFAULT_MAX_DAY;
$handlerInstance = new $handlerClassName($file, $maxDay); $handlerInstance = new $handlerClassName($file, $maxDay);
} }
@@ -82,26 +109,26 @@ class Monolog extends ServiceAbstract implements ServiceInterface
{ {
$handlerInstance = new $handlerClassName($file); $handlerInstance = new $handlerClassName($file);
} }
$channel = isset($options["channel"]) ? $options["channel"] : false; $channel = isset($options["channel"]) ? $options["channel"] : false;
$monologLogger = new \Monolog\Logger($channel); $monologLogger = new \Monolog\Logger($channel);
$monologLogger->pushHandler($handlerInstance); $monologLogger->pushHandler($handlerInstance);
$this->monolog = $monologLogger; $this->monolog = $monologLogger;
} }
public function getService() public function getService()
{ {
return $this->monolog; return $this->monolog;
} }
public function getType() public function getType()
{ {
return 'monolog'; return 'monolog';
} }
public function getScope() public function getScope()
{ {
return 'log'; return 'log';