-
-
Notifications
You must be signed in to change notification settings - Fork 191
Add Sentry logs #1000
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
Merged
Add Sentry logs #1000
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
800dd56
Add `dev-logs` base SDK as dependency version
stayallive a378bb2
Flush logs
stayallive 98e9de9
Add logs log channel
stayallive 947b675
Ignore exceptions in logs log channel
stayallive e4091fa
Do not register log channels with static closures
stayallive e985b12
wip
stayallive 59127f7
CS
stayallive 19c6c43
wip
stayallive 2290e27
wip
stayallive a6281e8
No longer needed to flatten log attributes
stayallive b2967fa
Merge branch 'master' into logs
cleptric 0196bf9
Bump PHP SDK
cleptric 352a3a8
Remove unused class
stayallive 22d8340
Bump PHP SDK
stayallive 7e716cb
Add tests
stayallive 33568a5
Use correct method for getting and flushing logs
stayallive ba95cf0
Fix monolog compat
stayallive ecbbf55
Remove broken test
stayallive 9ceeae6
Merge branch 'master' into logs
cleptric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Logs; | ||
|
||
use Monolog\Handler\FingersCrossedHandler; | ||
use Monolog\Logger; | ||
use Illuminate\Log\LogManager; | ||
|
||
class LogChannel extends LogManager | ||
{ | ||
public function __invoke(array $config = []): Logger | ||
{ | ||
$handler = new LogsHandler( | ||
$config['level'] ?? Logger::DEBUG, | ||
$config['bubble'] ?? true | ||
); | ||
|
||
if (isset($config['action_level'])) { | ||
$handler = new FingersCrossedHandler($handler, $config['action_level']); | ||
|
||
// Consume the `action_level` config option since newer Laravel versions also support this option | ||
// and will wrap the handler again in another `FingersCrossedHandler` if we leave the option set | ||
// See: https://github.com/laravel/framework/pull/40305 (release v8.79.0) | ||
unset($config['action_level']); | ||
} | ||
|
||
return new Logger( | ||
$this->parseChannel($config), | ||
[ | ||
$this->prepareHandler($handler, $config), | ||
] | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Logs; | ||
|
||
use Sentry\Logs\LogLevel; | ||
use Monolog\Formatter\LineFormatter; | ||
use Monolog\Formatter\FormatterInterface; | ||
use Monolog\Handler\AbstractProcessingHandler; | ||
use Sentry\Monolog\CompatibilityProcessingHandlerTrait; | ||
use Sentry\Severity; | ||
use Throwable; | ||
|
||
class LogsHandler extends AbstractProcessingHandler | ||
{ | ||
use CompatibilityProcessingHandlerTrait; | ||
|
||
/** | ||
* @var FormatterInterface The formatter to use for the logs generated via handleBatch() | ||
*/ | ||
protected $batchFormatter; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleBatch(array $records): void | ||
{ | ||
$level = $this->level; | ||
|
||
// filter records based on their level | ||
$records = array_filter( | ||
$records, | ||
function ($record) use ($level) { | ||
return $record['level'] >= $level; | ||
} | ||
); | ||
|
||
if (!$records) { | ||
return; | ||
} | ||
|
||
// the record with the highest severity is the "main" one | ||
$record = array_reduce( | ||
$records, | ||
function ($highest, $record) { | ||
if ($highest === null || $record['level'] > $highest['level']) { | ||
return $record; | ||
} | ||
|
||
return $highest; | ||
} | ||
); | ||
|
||
// the other ones are added as a context item | ||
$logs = []; | ||
foreach ($records as $r) { | ||
$logs[] = $this->processRecord($r); | ||
} | ||
|
||
if ($logs) { | ||
$record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs); | ||
} | ||
|
||
$this->handle($record); | ||
} | ||
|
||
/** | ||
* Sets the formatter for the logs generated by handleBatch(). | ||
* | ||
* @param FormatterInterface $formatter | ||
* | ||
* @return \Sentry\Laravel\SentryHandler | ||
*/ | ||
public function setBatchFormatter(FormatterInterface $formatter): self | ||
{ | ||
$this->batchFormatter = $formatter; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the formatter for the logs generated by handleBatch(). | ||
*/ | ||
public function getBatchFormatter(): FormatterInterface | ||
{ | ||
if (!$this->batchFormatter) { | ||
$this->batchFormatter = new LineFormatter(); | ||
} | ||
|
||
return $this->batchFormatter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @suppress PhanTypeMismatchArgument | ||
*/ | ||
protected function doWrite($record): void | ||
{ | ||
$exception = $record['context']['exception'] ?? null; | ||
|
||
if ($exception instanceof Throwable) { | ||
return; | ||
} | ||
|
||
\Sentry\logger()->aggregator()->add( | ||
// This seems a little bit of a roundabout way to get the log level, but this is done for compatibility | ||
self::getLogLevelFromSeverity( | ||
self::getSeverityFromLevel($record['level']) | ||
), | ||
$record['message'], | ||
[], | ||
array_merge($record['context'], $record['extra']) | ||
); | ||
} | ||
|
||
private static function getLogLevelFromSeverity(Severity $severity): LogLevel | ||
{ | ||
switch ($severity) { | ||
case Severity::debug(): | ||
return LogLevel::debug(); | ||
case Severity::warning(): | ||
return LogLevel::warn(); | ||
case Severity::error(): | ||
return LogLevel::error(); | ||
case Severity::fatal(): | ||
return LogLevel::fatal(); | ||
default: | ||
return LogLevel::info(); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Sentry\Features; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Support\Facades\Log; | ||
use Sentry\Laravel\Tests\TestCase; | ||
use Sentry\Logs\LogLevel; | ||
use function Sentry\logger; | ||
|
||
class LogLogsIntegrationTest extends TestCase | ||
{ | ||
protected function defineEnvironment($app): void | ||
{ | ||
parent::defineEnvironment($app); | ||
|
||
tap($app['config'], static function (Repository $config) { | ||
$config->set('sentry.enable_logs', true); | ||
|
||
$config->set('logging.channels.sentry_logs', [ | ||
'driver' => 'sentry_logs', | ||
]); | ||
|
||
$config->set('logging.channels.sentry_logs_error_level', [ | ||
'driver' => 'sentry_logs', | ||
'level' => 'error', | ||
]); | ||
}); | ||
} | ||
|
||
public function testLogChannelIsRegistered(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
Log::channel('sentry_logs'); | ||
} | ||
|
||
/** @define-env envWithoutDsnSet */ | ||
public function testLogChannelIsRegisteredWithoutDsn(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
|
||
Log::channel('sentry_logs'); | ||
} | ||
|
||
public function testLogChannelGeneratesLogs(): void | ||
{ | ||
$logger = Log::channel('sentry_logs'); | ||
|
||
$logger->info('Sentry Laravel info log message'); | ||
|
||
$logs = $this->getAndFlushCapturedLogs(); | ||
|
||
$this->assertCount(1, $logs); | ||
|
||
$log = $logs[0]; | ||
|
||
$this->assertEquals(LogLevel::info(), $log->getLevel()); | ||
$this->assertEquals('Sentry Laravel info log message', $log->getBody()); | ||
} | ||
|
||
public function testLogChannelGeneratesLogsOnlyForConfiguredLevel(): void | ||
{ | ||
$logger = Log::channel('sentry_logs_error_level'); | ||
|
||
$logger->info('Sentry Laravel info log message'); | ||
$logger->warning('Sentry Laravel warning log message'); | ||
$logger->error('Sentry Laravel error log message'); | ||
|
||
$logs = $this->getAndFlushCapturedLogs(); | ||
|
||
$this->assertCount(1, $logs); | ||
|
||
$log = $logs[0]; | ||
|
||
$this->assertEquals(LogLevel::error(), $log->getLevel()); | ||
$this->assertEquals('Sentry Laravel error log message', $log->getBody()); | ||
} | ||
|
||
public function testLogChannelDoesntCaptureExceptions(): void | ||
{ | ||
$logger = Log::channel('sentry_logs'); | ||
|
||
$logger->error('Sentry Laravel error log message', ['exception' => new \Exception('Test exception')]); | ||
|
||
$logs = $this->getAndFlushCapturedLogs(); | ||
|
||
$this->assertCount(0, $logs); | ||
} | ||
|
||
public function testLogChannelAddsContextAsAttributes(): void | ||
{ | ||
$logger = Log::channel('sentry_logs'); | ||
|
||
$logger->info('Sentry Laravel info log message', [ | ||
'foo' => 'bar', | ||
]); | ||
|
||
$logs = $this->getAndFlushCapturedLogs(); | ||
|
||
$this->assertCount(1, $logs); | ||
|
||
$log = $logs[0]; | ||
|
||
$this->assertEquals('bar', $log->attributes()->get('foo')->getValue()); | ||
} | ||
|
||
/** @return \Sentry\Logs\Log[] */ | ||
private function getAndFlushCapturedLogs(): array | ||
{ | ||
$logs = logger()->aggregator()->all(); | ||
|
||
logger()->aggregator()->flush(); | ||
|
||
return $logs; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This test is a little redundant and laravel/framework#55624 broke this test (as it should have before probably), will try to fix at a later point because I can't find a good workaround for now.