Get support for jeromegamez/php-psr-testlogger

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

jeromegamez/php-psr-testlogger

PSR Test Logger

PSR-3 compliant test logger for developers who like tests and want to check if their application logs messages as they expect.

This package was superseded by beste/psr-testlogger.

Packagist Supported PHP version Build Status GitHub license Total Downloads

Installation

composer require --dev gamez/psr-testlogger

Usage

Inject an instance of Gamez\Psr\Log\TestLogger into your Subject Under Test instead of your regular logger.

use Psr\Log\LoggerInterface;

class SubjectUnderTest
{
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute()
    {
        $this->logger->info('Message with a {placeholder}', ['placeholder' => 'value']);
        $this->logger->emergency('This {placeholder} will not be replaced.');
    }
}
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    /**
     * @var TestLogger
     */
    private $logger;

    /**
     * @var SubjectUnderTest
     */
    private $sut;

    protected function setUp()
    {
        $this->logger = new TestLogger();
        $this->sut = new SubjectUnderTest($this->logger);
    }
    
    public function testLogging()
    {
        $this->sut->execute();
        
        $log = $this->logger->log;
        
        $this->assertTrue($log->has('Message with a value'));
        $this->assertTrue($log->hasRecordsWithContextKey('foo'));
        $this->assertFalse($log->hasRecordsWithContextKeyAndValue('foo', 'unwanted'));
        // This will break
        $this->assertFalse($log->hasRecordsWithUnreplacedPlaceholders());
    }
}

You can find all available helper methods in the Gamez\Psr\Log\Log class. If it doesn't provide a method you need, you can use your own filters:

use Gamez\Psr\Log\Record;
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    /**
     * @var TestLogger
     */
    private $logger;

    /**
     * @var SubjectUnderTest
     */
    private $sut;

    protected function setUp()
    {
        $this->logger = new TestLogger();
        $this->sut = new SubjectUnderTest($this->logger);
    }
    
    public function testSomethingSpecial()
    {
        $filteredLog = $this->logger->log->filter(function (Record $record) {
            // Matches messages with only numbers
            return ctype_digit($record->message);
        });
        
        $this->assertCount(0, $filteredLog);
    }
}
Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2