Skip to content

Commit

Permalink
Fixed logging
Browse files Browse the repository at this point in the history
Broke loggers attached by queuedjobs because it wasn't using the global service.
Since the stderr handler was set to bubble=false, those messages weren't picked up by queuedjobs.
Removed preformatted handler since there's no longer an ability to run this stuff via web
  • Loading branch information
chillu committed Jun 5, 2019
1 parent 7301b37 commit 2d4711d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Dev/Tasks/MigrateFileTask.php
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Dev\Tasks;

use Monolog\Handler\FilterHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -243,25 +244,28 @@ protected function validateArgs($args)
*/
protected function addLogHandlers()
{
// Create a default logger with a custom name (less misleading than "error-log")
$logger = Injector::inst()->create(LoggerInterface::class, 'log');
// Using a global service here so other systems can control and redirect log output,
// for example when this task is run as part of a queuedjob
$logger = Injector::inst()->get(LoggerInterface::class)->withName('log');

if (Director::is_cli()) {
$formatter = new ColoredLineFormatter();
$formatter->ignoreEmptyContextAndExtra();

// Don't double process WARNING or higher levels in other handlers (bubble=false)
$errorHandler = new StreamHandler('php://stderr', Logger::ERROR, false);
$errorHandler = new StreamHandler('php://stderr', Logger::ERROR);
$errorHandler->setFormatter($formatter);

$standardHandler = new StreamHandler('php://stdout');
$standardHandler->setFormatter($formatter);

$logger->pushHandler($standardHandler);
// Avoid double logging of errors
$standardFilterHandler = new FilterHandler(
$standardHandler,
Logger::DEBUG,
Logger::WARNING
);

$logger->pushHandler($standardFilterHandler);
$logger->pushHandler($errorHandler);
} else {
$logger->pushHandler(new PreformattedEchoHandler());
}

$this->logger = $logger;
}
Expand Down

0 comments on commit 2d4711d

Please sign in to comment.