PHP code example of tourze / backtrace-helper

1. Go to this page and download the library: Download tourze/backtrace-helper 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/ */

    

tourze / backtrace-helper example snippets




use Tourze\BacktraceHelper\Backtrace;

// Create a backtrace at the current point
$backtrace = Backtrace::create();

// Get as string
echo $backtrace->toString();

// Get as array
$frames = $backtrace->frames();



use Tourze\BacktraceHelper\ExceptionPrinter;

try {
    // Some code that might throw an exception
    throw new \RuntimeException("Database connection failed");
} catch (\Throwable $e) {
    // Get a formatted exception string
    echo ExceptionPrinter::exception($e);
    
    // Or print directly to output
    ExceptionPrinter::print($e);
}



use Tourze\BacktraceHelper\ContextAwareInterface;
use Tourze\BacktraceHelper\ContextAwareTrait;

// Create your own context-aware exception
class BusinessException extends \Exception implements ContextAwareInterface 
{
    use ContextAwareTrait;
}

// Throw with context
throw new BusinessException(
    "User not found", 
    404, 
    null, 
    [
        "user_id" => 123,
        "action" => "profile_view",
        "timestamp" => time()
    ]
);



use Tourze\BacktraceHelper\NameCleaner;

// Clean AOP proxy class names
$clean = NameCleaner::formatClassName('AopProxy\__PM__\App\Service\UserService\Generated27a0b72656b351f2c469b189f98d296a');
// Returns: App\Service\UserService

// Clean Doctrine proxy class names
$clean = NameCleaner::formatClassName('Proxies\__CG__\AppBundle\Entity\User');
// Returns: AppBundle\Entity\User



use Tourze\BacktraceHelper\LogDataInterface;

class User implements LogDataInterface
{
    private int $id;
    private string $email;
    
    public function toLogData(): ?array
    {
        return [
            'id' => $this->id,
            'email' => $this->email,
            'type' => 'user'
        ];
    }
}

// These frames are automatically filtered:
// - Composer autoload internals
// - Symfony EventDispatcher internals
// - Symfony HttpKernel internals
// - Spatie Backtrace internals



use Tourze\BacktraceHelper\ExceptionPrinter;
use Monolog\Logger;

$logger = new Logger('app');

try {
    // Your application code
} catch (\Throwable $e) {
    $logger->error('Application error', [
        'exception' => ExceptionPrinter::exception($e),
        'trace' => Backtrace::create()->toString()
    ]);
}
bash
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/backtrace-helper