A PHP library for advanced stack trace handling and debugging.

0.2.0 2025-03-15 17:33 UTC

This package is auto-updated.

Last update: 2025-03-15 19:25:48 UTC


README

GitHub Workflow Status (master) Total Downloads Latest Version License

# Tracer

A PHP library for advanced stack trace handling and debugging.

Installation

composer require mortenscheel/tracer

Features

  • Get detailed stack traces with clean filtering options
  • Easily ignore vendor frames, specific classes, methods, files, or lines
  • StackTrace is a Laravel Collection
  • Format stack frames for debugging and display
  • Generate editor links (see mortenscheel/editor-links)
  • Serializable frames for logging or error reporting

Usage

Basic Usage

use Scheel\Tracer\StackTrace;

// Get a full stack trace
$trace = StackTrace::getTrace();

// Access the first frame
$firstFrame = $trace->first();

// Convert to array for inspection
$frames = $trace->toArray();

Filtering Frames

use Scheel\Tracer\StackTrace;
use Scheel\Tracer\Frame;

// Ignore vendor frames
$trace = StackTrace::getTrace()->ignoreVendor();

// Ignore specific classes
$trace = StackTrace::getTrace()->ignoreClass(SomeClass::class);

// Ignore specific class methods
$trace = StackTrace::getTrace()->ignoreClass(SomeClass::class, 'methodName');

// Ignore specific files
$trace = StackTrace::getTrace()->ignoreFile('/path/to/file.php');

// Ignore specific lines in files
$trace = StackTrace::getTrace()->ignoreFile('/path/to/file.php', 123);

// Custom filtering using the filter method
$trace = StackTrace::getTrace()->filter(function (Frame $frame): bool {
    return $frame->class !== 'ClassToIgnore';
});

Working with Frames

use Scheel\Tracer\StackTrace;

$frame = StackTrace::getTrace()->first();

// Get frame location as string
echo $frame->location(); // "/path/to/file.php:123"

// Convert frame to array
$frameData = $frame->toArray();
/*
[
    'file' => '/path/to/file.php',
    'line' => 123,
    'function' => 'methodName',
    'class' => 'ClassName',
    'type' => '::',
]
*/

// Generate editor links
echo $frame->editorLink(); // "phpstorm://open?file=/path/to/file.php&line=123"

Running Tests

composer test

License

MIT

Author

Morten Scheel