Skip to content

Commit

Permalink
More verbose file migration logging
Browse files Browse the repository at this point in the history
See https://github.com/silverstripeltd/open-sourcerers/issues/91

* Add `PreformattedEchoHandler`

cherry-picked from 4c3f3e6

* Batch log messages for every 100 file.
Also make logger work for stdout

* Update src/Logging/PreformattedEchoHandler.php

Co-Authored-By: bergice <bergice@users.noreply.github.com>
  • Loading branch information
bergice authored and chillu committed Apr 18, 2019
1 parent c63eecc commit 17f4d5f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Dev/Tasks/MigrateFileTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

namespace SilverStripe\Dev\Tasks;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SilverStripe\AssetAdmin\Helper\ImageThumbnailHelper;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Logging\PreformattedEchoHandler;
use SilverStripe\ORM\DB;
use SilverStripe\Assets\FileMigrationHelper;
use SilverStripe\Dev\BuildTask;
Expand All @@ -12,7 +18,6 @@
*/
class MigrateFileTask extends BuildTask
{

private static $segment = 'MigrateFileTask';

protected $title = 'Migrate File dataobjects from 3.x';
Expand All @@ -23,6 +28,8 @@ class MigrateFileTask extends BuildTask

public function run($request)
{
$this->addLogHandlers();

if (!class_exists(FileMigrationHelper::class)) {
DB::alteration_message("No file migration helper detected", "notice");
return;
Expand All @@ -46,4 +53,22 @@ public function run($request)
}
ImageThumbnailHelper::singleton()->run();
}

/**
* TODO Refactor this whole mess into Symfony Console on a TaskRunner level,
* with a thin wrapper to show coloured console output via a browser:
* https://github.com/silverstripe/silverstripe-framework/issues/5542
* @throws \Exception
*/
protected function addLogHandlers()
{
if ($logger = Injector::inst()->get(LoggerInterface::class)) {
if (Director::is_cli()) {
$logger->pushHandler(new StreamHandler('php://stdout'));
$logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING));
} else {
$logger->pushHandler(new PreformattedEchoHandler());
}
}
}
}
27 changes: 27 additions & 0 deletions src/Logging/PreformattedEchoHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace SilverStripe\Logging;

use Monolog\Handler\AbstractProcessingHandler;

/**
* Echo the output as preformatted HTML, emulating console output in a browser.
* Tiding us over until we can properly decoupled web from CLI output.
* Do not use this API outside of core modules,
* it'll likely be removed as part of a larger refactor.
*
* See https://github.com/silverstripe/silverstripe-framework/issues/5542
*
* @internal
*/
class PreformattedEchoHandler extends AbstractProcessingHandler
{

/**
* @param array $record
*/
protected function write(array $record)
{
echo sprintf('<pre>%s</pre>', htmlspecialchars($record['formatted'], ENT_QUOTES, 'UTF-8'));
}
}

0 comments on commit 17f4d5f

Please sign in to comment.