Skip to content

Commit

Permalink
Merge pull request #2 from Ukrator/implement-multiline-push
Browse files Browse the repository at this point in the history
Implement multiline sentry push
  • Loading branch information
mi-schi authored Sep 29, 2016
2 parents 1c78638 + 31d37d1 commit 45c4837
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Command/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progress = new ProgressBar($output, $logFileParser->count());
$progress->start();

foreach ($logFileParser->getEntries() as $entry) {
$sentryPusher->push($entry, $typeToLevel);
$progress->advance();
if (Parser::TYPE_FORMLESS === $input->getOption('logfile-type')) {
$sentryPusher->pushMultiline($logFileParser->getEntries(), $typeToLevel);
}

if (Parser::TYPE_FORMLESS !== $input->getOption('logfile-type')) {
foreach ($logFileParser->getEntries() as $entry) {
$sentryPusher->push($entry, $typeToLevel);
$progress->advance();
}
}

$progress->finish();
Expand Down
45 changes: 43 additions & 2 deletions src/Service/SentryPusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function __construct(\Raven_Client $client)
* @param TypeToLevelInterface $typeToLevel
*
* @return int
* @throws SentryException
*/
public function push($entry, TypeToLevelInterface $typeToLevel)
{
Expand All @@ -38,7 +37,49 @@ public function push($entry, TypeToLevelInterface $typeToLevel)
'level' => $typeToLevel->getLevel($entry->type)
];

$eventId = $this->client->getIdent($this->client->captureMessage($entry->message, [], $options));
$eventId = $this->pushToSentry($entry->message, $options);

return $eventId;
}

/**
* @param array $entries
* @param TypeToLevelInterface $typeToLevel
*
* @return mixed
*/
public function pushMultiline(array $entries, TypeToLevelInterface $typeToLevel)
{
$sentryLog = '';
foreach ($entries as $logEntry) {
$sentryLog .= $logEntry->message;
}

$options = [
'tags' => ['Source' => 'Log'],
'level' => $typeToLevel->getLevel('info'),
'extra' => ['log' => $sentryLog]
];

$now = new \DateTime();
$message = sprintf('Formless Log %s', $now->format('d-m-Y'));

$eventId = $this->pushToSentry($message, $options);

return $eventId;
}

/**
* @param string $sentryLog
* @param array $options
*
* @throws SentryException
*
* @return int
*/
private function pushToSentry($sentryLog, array $options)
{
$eventId = $this->client->getIdent($this->client->captureMessage($sentryLog, [], $options));
$lastError = $this->client->getLastError();

if (null !== $lastError) {
Expand Down

0 comments on commit 45c4837

Please sign in to comment.