2013-11-02 16:28:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2013-12-13 17:10:54 +01:00
|
|
|
namespace PHPCI\Logging;
|
2013-11-02 16:28:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
|
|
|
class LoggerConfig {
|
|
|
|
|
2013-11-02 16:52:21 +01:00
|
|
|
const KEY_AlwaysLoaded = "_";
|
|
|
|
|
2013-11-02 16:28:24 +01:00
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
2013-11-28 22:04:27 +01:00
|
|
|
* The filepath is expected to return an array which will be
|
|
|
|
* passed to the normal constructor.
|
|
|
|
*
|
|
|
|
* @param string $filePath
|
|
|
|
* @return LoggerConfig
|
2013-11-02 16:28:24 +01:00
|
|
|
*/
|
2013-11-28 22:04:27 +01:00
|
|
|
public static function newFromFile($filePath)
|
|
|
|
{
|
|
|
|
if (file_exists($filePath)) {
|
|
|
|
$configArray = require($filePath);
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-11-28 22:04:27 +01:00
|
|
|
$configArray = array();
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
2013-11-28 22:04:27 +01:00
|
|
|
return new self($configArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each key of the array is the name of a logger. The value of
|
|
|
|
* each key should be an array or a function that returns an
|
|
|
|
* array of LogHandlers.
|
|
|
|
* @param array $configArray
|
|
|
|
*/
|
|
|
|
function __construct(array $configArray = array()) {
|
|
|
|
$this->config = $configArray;
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of Monolog with all configured handlers
|
|
|
|
* added. The Monolog instance will be given $name.
|
|
|
|
* @param $name
|
|
|
|
* @return Logger
|
|
|
|
*/
|
2013-11-28 22:18:11 +01:00
|
|
|
public function getFor($name) {
|
2013-11-02 16:52:21 +01:00
|
|
|
$handlers = $this->getHandlers(self::KEY_AlwaysLoaded);
|
|
|
|
$handlers = array_merge($handlers, $this->getHandlers($name));
|
|
|
|
return new Logger($name, $handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getHandlers($key) {
|
2013-11-02 16:28:24 +01:00
|
|
|
$handlers = array();
|
|
|
|
|
|
|
|
// They key is expected to either be an array or
|
|
|
|
// a callable function that returns an array
|
2013-11-02 16:52:21 +01:00
|
|
|
if (isset($this->config[$key])) {
|
|
|
|
if (is_callable($this->config[$key])) {
|
|
|
|
$handlers = call_user_func($this->config[$key]);
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
2013-11-02 16:52:21 +01:00
|
|
|
elseif(is_array($this->config[$key])) {
|
|
|
|
$handlers = $this->config[$key];
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-02 16:52:21 +01:00
|
|
|
return $handlers;
|
2013-11-02 16:28:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|