Skip to content
Permalink
Browse files

Refactored log to an option

With this commit I have removed the automatic function of phpDocumentor to generate log files and replaced
it with the 'log' option. This option will safe the log to its final destination based on the verbosity of the
 command.
  • Loading branch information...
mvriel committed Sep 2, 2013
1 parent 75ae047 commit 7a0df0161bfdf9fae3a665888f05b682054d2b5a
@@ -23,11 +23,7 @@
<target>output</target>
</transformer>
<logging>
<level>info</level>
<paths>
<default>phpdoc-{DATE}.log</default>
<errors>phpdoc-{DATE}.errors.log</errors>
</paths>
<level>error</level>
</logging>
<transformations>
<template name="clean"/>
@@ -14,16 +14,17 @@
use Cilex\Application as Cilex;
use Cilex\Provider\MonologServiceProvider;
use Cilex\Provider\ValidatorServiceProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use Monolog\ErrorHandler;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use phpDocumentor\Command\Helper\LoggerHelper;
use phpDocumentor\Console\Input\ArgvInput;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Helper\ProgressHelper;
use Symfony\Component\Console\Shell;
use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use Zend\Config\Factory;
/**
@@ -52,14 +53,18 @@ public function __construct()
parent::__construct('phpDocumentor', self::$VERSION);
$this['kernel.timer.start'] = time();
$this->addAutoloader();
$this->addConfiguration();
$this->addLogging();
$this->setTimezone();
$this->addEventDispatcher();
$this->addTranslator();
$this['console']->getHelperSet()->set(new ProgressHelper());
/** @var ConsoleApplication $console */
$console = $this['console'];
$console->getHelperSet()->set(new LoggerHelper());
$this['translator.locale'] = 'en';
$this['translator'] = $this->share(
@@ -139,11 +144,10 @@ function ($log) use ($app) {
* @param integer $level The minimum level that will be written to the normal logfile; matches one of the
* constants in {@see \Monolog\Logger}.
* @param string $logPath The full path where the normal log file needs to be written.
* @param string $debugLogPath The full path where the log file containing debug information needs to be written.
*
* @return void
*/
public function configureLogger($logger, $level, $logPath = null, $debugLogPath = null)
public function configureLogger($logger, $level, $logPath = null)
{
/** @var Logger $monolog */
$monolog = $logger;
@@ -183,19 +187,11 @@ public function configureLogger($logger, $level, $logPath = null, $debugLogPath
if ($logPath) {
$logPath = str_replace(
array('{APP_ROOT}', '{DATE}'),
array(realpath(__DIR__.'/../..'), time()),
array(realpath(__DIR__.'/../..'), $this['kernel.timer.start']),
$logPath
);
$this['monolog.logfile'] = $logPath;
}
if ($debugLogPath) {
$debugLogPath = str_replace(
array('{APP_ROOT}', '{DATE}'),
array(realpath(__DIR__.'/../..'), time()),
$debugLogPath
);
$this['monolog.debugfile'] = $debugLogPath;
}
// remove all handlers from the stack
try {
@@ -214,9 +210,8 @@ public function configureLogger($logger, $level, $logPath = null, $debugLogPath
// set our new handlers
if ($logPath) {
$monolog->pushHandler(new StreamHandler($logPath, $level));
}
if ($debugLogPath) {
$monolog->pushHandler(new StreamHandler($debugLogPath, Logger::DEBUG));
} else {
$monolog->pushHandler(new StreamHandler('php://stdout', $level));
}
}
@@ -280,27 +275,27 @@ function () {
}
);
}
/**
* Adds the message translator to phpDocumentor's container.
*
*
* @return void
*/
protected function addTranslator()
{
$config = $this['config']->toArray();
$this['translator.locale'] = isset($config['translator']['locale']) ? $config['translator']['locale'] : 'en';
$this['translator'] = $this->share(
function ($app) {
$translator = new Translator();
$translator->setLocale($this['translator.locale']);
return $translator;
}
);
}
/**
* Adds the command to phpDocumentor that belong to the Project namespace.
*
@@ -9,17 +9,16 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor\Command;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\ProgressHelper;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Zend\I18n\Translator\Translator;
use phpDocumentor\Event\DebugEvent;
use phpDocumentor\Event\LogEvent;
use phpDocumentor\Parser\Event\PreFileEvent;
/**
* Base command for phpDocumentor commands.
@@ -29,12 +28,11 @@
*/
class Command extends \Cilex\Command\Command
{
/**
* Adds the option to output to a log file or suppress it by omitting.
*/
protected function configure()
public function setHelperSet(HelperSet $helperSet)
{
$this->addOption('log-text', null, InputOption::VALUE_OPTIONAL, 'Log file to write to');
parent::setHelperSet($helperSet);
$this->getHelper('phpdocumentor_logger')->addOptions($this);
}
/**
@@ -44,8 +42,7 @@ protected function configure()
*
* @author Michael Wallner <mike@php.net>
*
* @link http://pear.php.net/package/File_Util/docs/latest/File/File_Util/
* File_Util.html#methodisAbsolute
* @link http://pear.php.net/package/File_Util/docs/latest/File/File_Util/File_Util.html#methodisAbsolute
*
* @todo consider moving this method to a more logical place
*
@@ -68,8 +65,7 @@ protected function isAbsolute($path)
/**
* Returns the Progress bar helper.
*
* With this helper it is possible to display a progress bar and make it
* fill.
* With this helper it is possible to display a progress bar and make it fill.
*
* @param InputInterface $input
*
@@ -83,99 +79,4 @@ protected function getProgressBar(InputInterface $input)
return $this->getHelperSet()->get('progress');
}
/**
* Connect the logging events to the output object of Symfony Console.
*
* @param OutputInterface $output
*
* @return void
*/
protected function connectOutputToLogging(OutputInterface $output)
{
static $already_connected = false;
// ignore any second or later invocations of this method
if ($already_connected) {
return;
}
/** @var \phpDocumentor\Event\Dispatcher $event_dispatcher */
$event_dispatcher = $this->getService('event_dispatcher');
/** @var Command $command */
$command = $this;
$event_dispatcher->addListener(
'parser.file.pre',
function (PreFileEvent $event) use ($output) {
$output->writeln('Parsing <info>'.$event->getFile().'</info>');
}
);
$event_dispatcher->addListener(
'system.log',
function (LogEvent $event) use ($command, $output) {
$command->logEvent($output, $event);
}
);
$event_dispatcher->addListener(
'system.debug',
function (DebugEvent $event) use ($command, $output) {
$command->logEvent($output, $event);
}
);
$already_connected = true;
}
/**
* Logs an event with the output.
*
* This method will also colorize the message based on priority and withhold
* certain logging in case of verbosity or not.
*
* @param OutputInterface $output
* @param LogEvent $event
*
* @return void
*/
public function logEvent(OutputInterface $output, LogEvent $event)
{
$numericErrors = array(
LogLevel::DEBUG => 0,
LogLevel::NOTICE => 1,
LogLevel::INFO => 2,
LogLevel::WARNING => 3,
LogLevel::ERROR => 4,
LogLevel::ALERT => 5,
LogLevel::CRITICAL => 6,
LogLevel::EMERGENCY => 7,
);
$threshold = LogLevel::ERROR;
if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) {
$threshold = LogLevel::DEBUG;
}
if ($numericErrors[$event->getPriority()] >= $numericErrors[$threshold]) {
/** @var Translator $translator */
$translator = $this->getContainer()->offsetGet('translator');
$message = vsprintf($translator->translate($event->getMessage()), $event->getContext());
switch ($event->getPriority()) {
case LogLevel::WARNING:
$message = '<comment>' . $message . '</comment>';
break;
case LogLevel::EMERGENCY:
case LogLevel::ALERT:
case LogLevel::CRITICAL:
case LogLevel::ERROR:
$message = '<error>' . $message . '</error>';
break;
}
$output->writeln(' ' . $message);
}
}
}
@@ -11,9 +11,9 @@
*/
namespace phpDocumentor\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
use \Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Base class for commands that may make use of the configuration.
@@ -36,6 +36,8 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
'Location of a custom configuration file'
);
parent::configure();
}
/**
@@ -58,46 +60,34 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config_file = $input->getOption('config');
if ($config_file && $config_file !== 'none') {
$config_file = realpath($config_file);
$configFile = $input->getOption('config');
if ($configFile && $configFile !== 'none') {
$configFile = realpath($configFile);
// all relative paths mentioned in the configuration file should
// be relative to the configuration file.
// This means that if we provide an alternate configuration file
// that we need to go to that directory first so that paths can be
// calculated from there.
chdir(dirname($config_file));
chdir(dirname($configFile));
}
if ($config_file) {
$container = $this->getContainer();
$container = $this->getContainer();
if ($configFile) {
$container['config'] = $container->share(
function () use ($config_file) {
function () use ($configFile) {
$files = array(__DIR__ . '/../../../data/phpdoc.tpl.xml');
if ($config_file !== 'none') {
$files[] = $config_file;
if ($configFile !== 'none') {
$files[] = $configFile;
}
return \Zend\Config\Factory::fromFiles($files, true);
}
);
if (isset($container['config']->logging)) {
$level = (string)$container['config']->logging->level;
// null means the default is used
$logPath = isset($container['config']->logging->paths->default)
? (string) $container['config']->logging->paths->default
: null;
// null means the default is used
$debugPath = isset($container['config']->logging->paths->errors)
? (string) $container['config']->logging->paths->errors
: null;
$container->configureLogger($container['monolog'], $level, $logPath, $debugPath);
}
}
$this->getHelper('phpdocumentor_logger')->reconfigureLogger($input, $output, $this);
}
/**

0 comments on commit 7a0df01

Please sign in to comment.
You can’t perform that action at this time.