-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PreformattedEchoHandler
#8930
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -12,7 +18,6 @@ | |
*/ | ||
class MigrateFileTask extends BuildTask | ||
{ | ||
|
||
private static $segment = 'MigrateFileTask'; | ||
|
||
protected $title = 'Migrate File dataobjects from 3.x'; | ||
|
@@ -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; | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we have a prototype specialisation, like
rather than taking the service and mutate its global state in here? |
||
if (Director::is_cli()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might worth having a separate logger class implementation rather than encoding this logic in here |
||
$logger->pushHandler(new StreamHandler('php://stdout')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think this is one of those cases where we squint a bit, and just assume that people will not apply loggers other than Monolog for now. We're effectively using LoggerInterface as a global singleton here, since @bergice other PR on queuedjobs is configuring that singleton with additional handlers, so both of these are assuming to work off the same instance. It's pretty messy, but the answer is not to use log handlers here at all, but rather Symfony Console. |
||
$logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING)); | ||
} else { | ||
$logger->pushHandler(new PreformattedEchoHandler()); | ||
} | ||
} | ||
} | ||
} |
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')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we do that in the constructor rather than the run method?