Giter Club home page Giter Club logo

yii2-sentry's Introduction

Sentry logger for Yii2

Latest Stable Version Total Downloads License

Installation

composer require nohnaimer/yii2-sentry

Add target class in the application config:

return [
    'components' => [
        'sentry' => [
            'class' => 'nohnaimer\sentry\Component',
            'dsn' => 'http://2682ybvhbs347:[email protected]/1',
            // Additional options for `Sentry\init`:
            'clientOptions' => [
                'release' => '[email protected]',
                //Performance Monitoring
                'traces_sample_rate' => 1.0,
            ],
            //collect JavaScript errors, default false
            'jsNotifier' => true,
            //Collect javascript errors to different project
            'jsDsn' => 'http://[email protected]/2',
            // Additional options for javascript `Sentry\init`:
            'jsClientOptions' => [
                'release' => '[email protected]',
                //Performance Monitoring
                'integrations' => '[new Sentry.Integrations.BrowserTracing()]',
                'tracesSampleRate' => 0.2,
            ],
            // Write the context information (the default is true):
            'context' => true,
            //add Environment application
            'environment' => 'test',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'nohnaimer\sentry\Target',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
];

Usage

Writing simple message:

\Yii::error('message', 'category');

Writing messages with extra data:

\Yii::warning([
    'msg' => 'message',
    'extra' => 'value',
], 'category');

Extra callback

extraCallback property can modify extra's data as callable function:

    'targets' => [
        [
            'class' => 'nohnaimer\sentry\Target',
            'levels' => ['error', 'warning'],
            'extraCallback' => function ($message, $extra) {
                // some manipulation with data
                $extra['some_data'] = \Yii::$app->someComponent->someMethod();
                return $extra;
            }
        ],
    ],

Tags

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

Additional context

You can add additional context (such as user information, fingerprint, etc) by calling \Sentry\configureScope() before logging. For example in main configuration on beforeAction event (real place will dependant on your project):

return [
    // ...
    'on beforeAction' => function (\yii\base\ActionEvent $event) {
        /** @var \yii\web\User $user */
        $user = Yii::$app->has('user', true) ? Yii::$app->get('user', false) : null;
        if ($user && ($identity = $user->getIdentity(false))) {
            \Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($identity) {
                $scope->setUser([
                    // User ID and IP will be added by logger automatically
                    'username' => $identity->username,
                    'email' => $identity->email,
                ]);
            });
        }
    
        return $event->isValid;
    },
    // ...
];

Log levels

Yii2 log levels converts to Sentry levels:

\yii\log\Logger::LEVEL_ERROR => 'error',
\yii\log\Logger::LEVEL_WARNING => 'warning',
\yii\log\Logger::LEVEL_INFO => 'info',
\yii\log\Logger::LEVEL_TRACE => 'debug',
\yii\log\Logger::LEVEL_PROFILE_BEGIN => 'debug',
\yii\log\Logger::LEVEL_PROFILE_END => 'debug',

yii2-sentry's People

Contributors

niksamokhvalov avatar nohnaimer avatar cookieseater avatar nixmen avatar dmhaiduchonak avatar dawidrylko avatar i-sevostyanov avatar julian-b90 avatar shmakov avatar mitrm avatar orionchikby avatar perfectio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.