PHP code example of baraja-core / tracy-sentry-bridge

1. Go to this page and download the library: Download baraja-core/tracy-sentry-bridge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

baraja-core / tracy-sentry-bridge example snippets


use Baraja\TracySentryBridge\SentryLogger;

public static function boot(): Configurator
{
    $configurator = new Configurator;

    // Initialize Sentry first
    if (\function_exists('\Sentry\init')) {
        \Sentry\init([
            'dsn' => 'https://[email protected]/project-id',
            'attach_stacktrace' => true,
        ]);
    }

    // Register the Tracy-Sentry bridge
    SentryLogger::register();

    return $configurator;
}

use Tracy\Debugger;
use Baraja\TracySentryBridge\SentryLogger;

// Enable Tracy
Debugger::enable();

// Initialize Sentry
\Sentry\init([
    'dsn' => 'https://[email protected]/project-id',
]);

// Register the bridge
SentryLogger::register();

// Now all Tracy logs will be sent to Sentry
Debugger::log('Something happened', Debugger::WARNING);

use Tracy\Debugger;
use Baraja\TracySentryBridge\SentryLogger;

$originalLogger = Debugger::getLogger();
$sentryLogger = new SentryLogger($originalLogger);
Debugger::setLogger($sentryLogger);

// Example of how errors are handled internally
public function log(mixed $value, mixed $level = self::INFO): void
{
    // Always log to fallback first (guaranteed to work)
    $this->fallback->log($value, $level);

    try {
        // Then attempt Sentry logging
        $this->logToSentry($value, $level);
    } catch (\Throwable $e) {
        // If Sentry fails, log the error locally
        $this->fallback->log($e, ILogger::CRITICAL);
    }
}

\Sentry\init([
    'dsn' => 'https://[email protected]/project-id',
    'attach_stacktrace' => true,
    'environment' => 'production',
    'release' => '[email protected]',
    'sample_rate' => 1.0,
    'traces_sample_rate' => 0.2,
    'send_default_pii' => false,
]);

if (getenv('APP_ENV') === 'production' && \function_exists('\Sentry\init')) {
    \Sentry\init(['dsn' => getenv('SENTRY_DSN')]);
    SentryLogger::register();
}

// Exceptions are captured with full stack trace
try {
    throw new \RuntimeException('Something went wrong');
} catch (\Throwable $e) {
    Debugger::log($e, Debugger::EXCEPTION);
}

// String messages are captured as messages
Debugger::log('User login failed', Debugger::WARNING);

// Arrays and objects are serialized
Debugger::log(['user_id' => 123, 'action' => 'failed_login'], Debugger::INFO);