phpci/PHPCI/Logging/LoggerConfig.php

71 lines
1.8 KiB
PHP

<?php
namespace PHPCI\Logging;
use Monolog\Logger;
class LoggerConfig {
const KEY_AlwaysLoaded = "_";
private $config;
/**
* The filepath is expected to return an array which will be
* passed to the normal constructor.
*
* @param string $filePath
* @return LoggerConfig
*/
public static function newFromFile($filePath)
{
if (file_exists($filePath)) {
$configArray = require($filePath);
}
else {
$configArray = array();
}
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;
}
/**
* Returns an instance of Monolog with all configured handlers
* added. The Monolog instance will be given $name.
* @param $name
* @return Logger
*/
public function getFor($name) {
$handlers = $this->getHandlers(self::KEY_AlwaysLoaded);
$handlers = array_merge($handlers, $this->getHandlers($name));
return new Logger($name, $handlers);
}
protected function getHandlers($key) {
$handlers = array();
// They key is expected to either be an array or
// a callable function that returns an array
if (isset($this->config[$key])) {
if (is_callable($this->config[$key])) {
$handlers = call_user_func($this->config[$key]);
}
elseif(is_array($this->config[$key])) {
$handlers = $this->config[$key];
}
}
return $handlers;
}
}