Added to the console so that external logging definitions are pulled in from a loggerconfig.php file if the file exists.

This commit is contained in:
meadsteve 2013-11-02 15:28:24 +00:00
parent 35b3db13d8
commit 0fc91f053a
4 changed files with 67 additions and 2 deletions

3
.gitignore vendored
View file

@ -8,4 +8,5 @@ config.php
.buildpath
.htaccess
PHPCI/config.yml
cache
cache
/loggerconfig.php

View file

@ -0,0 +1,51 @@
<?php
namespace PHPCI\Helper;
use Monolog\Logger;
class LoggerConfig {
private $config;
/**
* The file specified is expected to return an array. Where each key
* 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 $logConfigFilePath
*/
function __construct($logConfigFilePath) {
if (file_exists($logConfigFilePath)) {
$this->config = require_once($logConfigFilePath);
}
else {
$this->config = array();
}
}
/**
* 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 = array();
// They key is expected to either be an array or
// a callable function that returns an array
if (isset($this->config[$name])) {
if (is_callable($this->config[$name])) {
$handlers = call_user_func($this->config[$name]);
}
elseif(is_array($this->config[$name])) {
$handlers = $this->config[$name];
}
}
return new Logger($name, $handlers);
}
}

View file

@ -19,10 +19,14 @@ use PHPCI\Command\InstallCommand;
use PHPCI\Command\DaemonCommand;
use Symfony\Component\Console\Application;
$loggerConfig = new \PHPCI\Helper\LoggerConfig(__DIR__ . "/loggerconfig.php");
$application = new Application();
$application->add(new RunCommand);
$application->add(new RunCommand($loggerConfig->GetFor('RunCommand')));
$application->add(new InstallCommand);
$application->add(new UpdateCommand);
$application->add(new GenerateCommand);
$application->add(new DaemonCommand);
$application->run();

9
loggerconfig.php.example Normal file
View file

@ -0,0 +1,9 @@
<?php
return array(
'RunCommand' => function() {
return array(
new \Monolog\Handler\StreamHandler('path/to/logs/errors', \Monolog\Logger::ERROR),
new \Monolog\Handler\RotatingFileHandler('path/to/logs/everything',3, \Monolog\Logger::INFO),
);
}
);