Created
September 4, 2014 06:50
-
-
Save rskuipers/924e8c65c17519cc5898 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Traits; | |
use Symfony\Component\Console\Output\OutputInterface; | |
trait OutputTrait | |
{ | |
/** | |
* @param string $message | |
* @param bool $rewrite | |
*/ | |
protected function output($message, $rewrite = false) | |
{ | |
$logFile = $this->getLogFile(); | |
if (!is_null($logFile)) { | |
if ($rewrite === false) { | |
$timestamp = (new \DateTime())->format('Y-m-d H:i:s'); | |
file_put_contents($logFile, "[{$timestamp}] {$message}\n", FILE_APPEND); | |
} | |
} | |
if (is_null($this->getOutput()) || $this->getOutput()->getVerbosity() <= 0) { | |
return; | |
} | |
echo $message . str_pad($rewrite ? "\r" : "\n", 25, ' ', STR_PAD_LEFT); | |
} | |
/** | |
* @return OutputInterface | |
*/ | |
abstract protected function getOutput(); | |
/** | |
* @return string | |
*/ | |
abstract protected function getLogFile(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment