Sentry logger for Yii2
composer require asminog/yii2-sentry
Add target class in the application config:
return [
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'asminog\yii2sentry\SentryTarget',
'levels' => ['error', 'warning'],
'dsn' => 'https://[email protected]/1',
// release option for project, default: null. Use "auto" to get it from git exec('git log --pretty="%H" -n1 HEAD')
'release' => '[email protected]',
// Options for sentry client
'options' => [],
// Collect additional context from $_GLOBALS, default: ['_SESSION', 'argv']. To switch off set false.
/* @see https://docs.sentry.io/enriching-error-data/context/?platform=php#extra-context
'collectContext' => ['_SERVER', '_COOKIE', '_SESSION', 'argv'],
// user attributes to collect, default: ['id', 'username', 'email']. To switch off set false.
/* @see https://docs.sentry.io/enriching-error-data/context/?platform=php#capturing-the-user */
'collectUserAttributes' => ['userId', 'userName', 'email'],
// add something to extra using extraCallback, default: null
'extraCallback' => function ($message, $extra) {
$extra['YII_ENV'] = YII_ENV;
return $extra;
}
],
],
],
],
];
Writing simple message:
\Yii::error('message', 'category');
Writing messages with extra data:
\Yii::warning([
'msg' => 'message',
'extra' => 'value',
], 'category');
Writing messages with additional tags. If need to add additional tags for event, add tags
key in message. Tags are various key/value pairs that get assigned to an event, and can later be used as a breakdown or quick access to finding related events.
Example:
\Yii::warning([
'msg' => 'message',
'extra' => 'value',
'tags' => [
'extraTagKey' => 'extraTagValue',
]
], 'category');
More about tags see https://docs.sentry.io/learn/context/#tagging-events
Inspired by notamedia/yii2-sentry