PHPackages                             timacdonald/log-fake - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. timacdonald/log-fake

ActiveLibrary[Testing &amp; Quality](/categories/testing)

timacdonald/log-fake
====================

A drop in fake logger for testing with the Laravel framework.

v2.4.2(2mo ago)4235.9M—0.3%32[2 issues](https://github.com/timacdonald/log-fake/issues)20MITPHPPHP ^8.2CI passing

Since Oct 6Pushed 3mo ago5 watchersCompare

[ Source](https://github.com/timacdonald/log-fake)[ Packagist](https://packagist.org/packages/timacdonald/log-fake)[ RSS](/packages/timacdonald-log-fake/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (20)Versions (21)Used By (20)

[![Log Fake: a Laravel package by Tim MacDonald](/art/header.png)](/art/header.png)

Log fake for Laravel
====================

[](#log-fake-for-laravel)

A bunch of Laravel facades / services are able to be faked, such as the Dispatcher with `Bus::fake()`, to help with testing and assertions. This package gives you the ability to fake the logger in your app, and includes the ability to make assertions against channels, stacks, and a whole bunch more.

Installation
------------

[](#installation)

You can install using [composer](https://getcomposer.org/):

```
composer require timacdonald/log-fake --dev
```

Basic usage
-----------

[](#basic-usage)

```
use Illuminate\Support\Facades\Log;
use TiMacDonald\Log\LogEntry;
use TiMacDonald\Log\LogFake;

public function testItLogsWhenAUserAuthenticates()
{
    /*
     * Test setup.
     *
     * In the setup of your tests, you can call the following `bind` helper,
     * which will switch out the underlying log driver with the fake.
     */
    LogFake::bind();

    /*
     * Application implementation.
     *
     * In your application's implementation, you then utilise the logger, as you
     * normally would.
     */
    Log::info('User logged in.', ['user_id' => $user->id]);

    /*
     * Test assertions.
     *
     * Finally you can make assertions against the log channels, stacks, etc. to
     * ensure the expected logging occurred in your implementation.
     */
    Log::assertLogged(fn (LogEntry $log) =>
        $log->level === 'info'
        && $log->message === 'User logged in.'
        && $log->context === ['user_id' => 5]
    );
}
```

Channels
--------

[](#channels)

If you are logging to a specific channel (i.e. not the default channel) in your app, you need to also prefix your assertions in the same manner.

```
public function testItLogsWhenAUserAuthenticates()
{
    // setup...
    LogFake::bind();

    // implementation...
    Log::channel('slack')->info('User logged in.', ['user_id' => $user->id]);

    // assertions...
    Log::channel('slack')->assertLogged(
        fn (LogEntry $log) => $log->message === 'User logged in.'
    );
}
```

Stacks
------

[](#stacks)

If you are logging to a stack in your app, like with channels, you will need to prefix your assertions. Note that the order of the stack does not matter.

```
public function testItLogsWhenAUserAuthenticates()
{
    // setup...
    LogFake::bind();

    // implementation...
    Log::stack(['stderr', 'single'])->info('User logged in.', ['user_id' => $user->id]);

    // assertions...
    Log::stack(['stderr', 'single'])->assertLogged(
        fn (LogEntry $log) => $log->message === 'User logged in.'
    );
}
```

That's it really. Now let's dig into the available assertions to improve your experience testing your applications logging.

Available assertions
--------------------

[](#available-assertions)

Remember that all assertions are relative to the channel or stack as shown above.

- [`assertLogged()`](#assertlogged)
- [`assertLoggedTimes()`](#assertloggedtimes)
- [`assertNotLogged()`](#assertnotlogged)
- [`assertNothingLogged()`](#assertnothinglogged)
- [`assertWasForgotten()`](#assertwasforgotten)
- [`assertWasForgottenTimes()`](#assertwasforgottentimes)
- [`assertWasNotForgotten()`](#assertwasnotforgotten)
- [`assertChannelIsCurrentlyForgotten()`](#assertchanneliscurrentlyforgotten)
- [`assertCurrentContext()`](#assertcurrentcontext)

### assertLogged()

[](#assertlogged)

Assert that a log was created.

#### Can be called on

[](#can-be-called-on)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests)

```
/*
 * implementation...
 */

Log::info('User logged in.');

/*
 * assertions...
 */

Log::assertLogged(
    fn (LogEntry $log) => $log->message === 'User logged in.'
); // ✅

Log::assertLogged(
    fn (LogEntry $log) => $log->level === 'critical'
); // ❌ as log had a level of `info`.
```

### assertLoggedTimes()

[](#assertloggedtimes)

Assert that a log was created a specific number of times.

#### Can be called on

[](#can-be-called-on-1)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-1)

```
/*
 * implementation...
 */

Log::info('Stripe request initiated.');

Log::info('Stripe request initiated.');

/*
 * assertions...
 */

Log::assertLoggedTimes(
    fn (LogEntry $log) => $log->message === 'Stripe request initiated.',
    2
); // ✅

Log::assertLoggedTimes(
    fn (LogEntry $log) => $log->message === 'Stripe request initiated.',
    99
); // ❌ as the log was created twice, not 99 times.
```

### assertNotLogged()

[](#assertnotlogged)

Assert that a log was never created.

#### Can be called on

[](#can-be-called-on-2)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-2)

```
/*
 * implementation...
 */

Log::info('User logged in.');

/*
 * assertions...
 */

Log::assertNotLogged(
    fn (LogEntry $log) => $log->level === 'critical'
); // ✅

Log::assertNotLogged(
    fn (LogEntry $log) => $log->level === 'info'
); // ❌ as the level was `info`.
```

### assertNothingLogged()

[](#assertnothinglogged)

Assert that no logs were created.

#### Can be called on

[](#can-be-called-on-3)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-3)

```
/*
 * implementation...
 */

Log::channel('single')->info('User logged in.');

/*
 * assertions...
 */

Log::channel('stderr')->assertNothingLogged(); // ✅

Log::channel('single')->assertNothingLogged(); // ❌ as a log was created in the `single` channel.
```

### assertWasForgotten()

[](#assertwasforgotten)

Assert that the channel was forgotten at least one time.

#### Can be called on

[](#can-be-called-on-4)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-4)

```
/*
 * implementation...
 */

Log::channel('single')->info('User logged in.');

Log::forgetChannel('single');

/*
 * assertions...
 */

Log::channel('single')->assertWasForgotten(); // ✅

Log::channel('stderr')->assertWasForgotten(); // ❌ as it was the `single` not the `stderr` channel that was not forgotten.
```

### assertWasForgottenTimes()

[](#assertwasforgottentimes)

Assert that the channel was forgotten a specific number of times.

#### Can be called on

[](#can-be-called-on-5)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-5)

```
/*
 * implementation...
 */

Log::channel('single')->info('User logged in.');

Log::forgetChannel('single');

Log::channel('single')->info('User logged in.');

Log::forgetChannel('single');

/*
 * assertions...
 */

Log::channel('single')->assertWasForgottenTimes(2); // ✅

Log::channel('single')->assertWasForgottenTimes(99); // ❌ as the channel was forgotten twice, not 99 times.
```

### assertWasNotForgotten()

[](#assertwasnotforgotten)

Assert that the channel was not forgotten.

#### Can be called on

[](#can-be-called-on-6)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-6)

```
/*
 * implementation...
 */

Log::channel('single')->info('User logged in.');

/*
 * assertions...
 */

Log::channel('single')->assertWasNotForgotten(); // ✅
```

### assertChannelIsCurrentlyForgotten()

[](#assertchanneliscurrentlyforgotten)

Assert that a channel is *currently* forgotten. This is distinct from [asserting that a channel *was* forgotten](https://github.com/timacdonald/log-fake#assertwasforgotten).

#### Can be called on

[](#can-be-called-on-7)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-7)

```
/*
 * implementation...
 */

Log::channel('single')->info('xxxx');

Log::forgetChannel('single');

/*
 * assertions...
 */

Log::assertChannelIsCurrentlyForgotten('single'); // ✅

Log::assertChannelIsCurrentlyForgotten('stderr'); // ❌ as the `single` channel was forgotten, not the `stderr` channel.
```

### assertCurrentContext()

[](#assertcurrentcontext)

Assert that the channel currently has the specified context. It is possible to provide the expected context as an array or alternatively you can provide a truth-test closure to check the current context.

#### Can be called on

[](#can-be-called-on-8)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-8)

```
/*
 * implementation...
 */

Log::withContext([
    'app' => 'Acme CRM',
]);

Log::withContext([
    'env' => 'production',
]);

/*
 * assertions...
 */

Log::assertCurrentContext([
    'app' => 'Acme CRM',
    'env' => 'production',
]); // ✅

Log::assertCurrentContext(
    fn (array $context) => $context['app'] === 'Acme CRM')
); // ✅

Log::assertCurrentContext([
    'env' => 'production',
]); // ❌ missing the "app" key.

Log::assertCurrentContext(
    fn (array $context) => $context['env'] === 'develop')
); // ❌ the 'env' key is set to "production"
```

### assertHasSharedContext()

[](#asserthassharedcontext)

Assert that the Log manager currently has the given shared context. It is possible to provide the expected context as an array or alternatively you can provide a truth-test closure to check the current context.

#### Can be called on

[](#can-be-called-on-9)

- Facade base (default channel)
- Channels
- Stacks

#### Example tests

[](#example-tests-9)

```
/*
 * implementation...
 */

Log::shareContext([
    'invocation-id' => '54',
]);

/*
 * assertions...
 */

Log::assertHasSharedContext([
    'invocation-id' => '54',
]); // ✅

Log::assertCurrentContext(
    fn (array $context) => $context['invocation-id'] === '54')
); // ✅

Log::assertCurrentContext([
    'invocation-id' => '99',
]); // ❌ wrong invocation ID

Log::assertCurrentContext(
    fn (array $context) => $context['invocation-id'] === '99')
); // ❌ wrong invocation ID
```

Inspection
----------

[](#inspection)

Sometimes when debugging tests it's useful to be able to take a peek at the messages that have been logged. There are a couple of helpers to assist with this.

### dump()

[](#dump)

Dumps all the logs in the channel. You can also pass a truth-based closure to filter the logs that are dumped.

#### Can be called on

[](#can-be-called-on-10)

- Facade base (default channel)
- Channels
- Stacks

```
/*
 * implementation...
 */

Log::info('User logged in.');

Log::channel('slack')->alert('Stripe request initiated.');

/*
 * inspection...
 */

Log::dump();

// array:1 [
//   0 => array:4 [
//     "level" => "info"
//     "message" => "User logged in."
//     "context" => []
//     "channel" => "stack"
//   ]
// ]

Log::channel('slack')->dump();

// array:1 [
//   0 => array:4 [
//     "level" => "alert"
//     "message" => "Stripe request initiated."
//     "context" => []
//     "channel" => "slack"
//   ]
// ]
```

### dd()

[](#dd)

The same as [`dump`](https://github.com/timacdonald/log-fake#dump), but also ends the execution.

### dumpAll()

[](#dumpall)

Dumps the logs for all channels. Also accepts a truth-test closure to filter any logs.

#### Can be called on

[](#can-be-called-on-11)

- Facade base (default channel)
- Channels
- Stacks

#### Example usage

[](#example-usage)

```
/*
 * implementation...
 */

Log::info('User logged in.');

Log::channel('slack')->alert('Stripe request initiated.');

/*
 * inspection...
 */

Log::dumpAll();

// array:2 [
//   0 => array:4 [
//     "level" => "info"
//     "message" => "User logged in."
//     "context" => []
//     "times_channel_has_been_forgotten_at_time_of_writing_log" => 0
//     "channel" => "stack"
//   ]
//   1 => array:4 [
//     "level" => "alert"
//     "message" => "Stripe request initiated."
//     "context" => []
//     "times_channel_has_been_forgotten_at_time_of_writing_log" => 0
//     "channel" => "slack"
//   ]
// ]
```

### ddAll()

[](#ddall)

The same as [`dumpAll()`](https://github.com/timacdonald/log-fake#dumpall), but also ends the execution.

Other APIs
----------

[](#other-apis)

### logs()

[](#logs)

Get a collection of all log entries from a channel or stack.

#### Can be called on

[](#can-be-called-on-12)

- Facade base (default channel)
- Channels
- Stacks

#### Example usage

[](#example-usage-1)

```
/*
 * implementation...
 */

Log::channel('slack')->info('User logged in.');

Log::channel('slack')->alert('Stripe request initiated.');

/*
 * example usage...
 */

$logs = Log::channel('slack')->logs();

assert($logs->count() === 2); ✅
```

### allLogs()

[](#alllogs)

Similar to [`logs()`](https://github.com/timacdonald/log-fake#logs), except that it is called on the Facade base and returns a collection of logs from all the channels and stacks.

#### Can be called on

[](#can-be-called-on-13)

- Facade base (default channel)
- Channels
- Stacks

#### Example usage

[](#example-usage-2)

```
/*
 * implementation...
 */

Log::info('User logged in.');

Log::channel('slack')->alert('Stripe request initiated.');

/*
 * example usage...
 */

$logs = Log::allLogs();

assert($logs->count() === 2); ✅
```

Credits
-------

[](#credits)

- [Tim MacDonald](https://github.com/timacdonald)
- [All Contributors](../../contributors)

And a special (vegi) thanks to [Caneco](https://twitter.com/caneco) for the logo ✨

Thanksware
----------

[](#thanksware)

You are free to use this package, but I ask that you reach out to someone (not me) who has previously, or is currently, maintaining or contributing to an open source library you are using in your project and thank them for their work. Consider your entire tech stack: packages, frameworks, languages, databases, operating systems, frontend, backend, etc.

###  Health Score

72

—

ExcellentBetter than 100% of packages

Maintenance85

Actively maintained with recent releases

Popularity65

Solid adoption and visibility

Community39

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 90.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~143 days

Recently: every ~183 days

Total

20

Last Release

60d ago

Major Versions

1.9.1 → 2.0.0-rc12022-04-03

PHP version history (6 changes)v1.0.0PHP ^7.1.3

1.7.0PHP ^7.1

1.8.0PHP ^7.1 || ^8.0

2.0.0-rc1PHP ^8.0

2.1.0PHP ^8.1

v2.4.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/24803032?v=4)[Tim MacDonald](/maintainers/timacdonald)[@timacdonald](https://github.com/timacdonald)

---

Top Contributors

[![timacdonald](https://avatars.githubusercontent.com/u/24803032?v=4)](https://github.com/timacdonald "timacdonald (205 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (4 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![ludo237](https://avatars.githubusercontent.com/u/921500?v=4)](https://github.com/ludo237 "ludo237 (2 commits)")[![brentkelly](https://avatars.githubusercontent.com/u/2694025?v=4)](https://github.com/brentkelly "brentkelly (2 commits)")[![it-can](https://avatars.githubusercontent.com/u/644288?v=4)](https://github.com/it-can "it-can (1 commits)")[![Lednerb](https://avatars.githubusercontent.com/u/2056876?v=4)](https://github.com/Lednerb "Lednerb (1 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (1 commits)")[![markwalet](https://avatars.githubusercontent.com/u/11446771?v=4)](https://github.com/markwalet "markwalet (1 commits)")[![nunomaduro](https://avatars.githubusercontent.com/u/5457236?v=4)](https://github.com/nunomaduro "nunomaduro (1 commits)")[![rmachado-studocu](https://avatars.githubusercontent.com/u/89906313?v=4)](https://github.com/rmachado-studocu "rmachado-studocu (1 commits)")[![romkatsu](https://avatars.githubusercontent.com/u/1677515?v=4)](https://github.com/romkatsu "romkatsu (1 commits)")[![sebastiaanluca](https://avatars.githubusercontent.com/u/711940?v=4)](https://github.com/sebastiaanluca "sebastiaanluca (1 commits)")[![simoebenhida](https://avatars.githubusercontent.com/u/19809072?v=4)](https://github.com/simoebenhida "simoebenhida (1 commits)")[![caneco](https://avatars.githubusercontent.com/u/502041?v=4)](https://github.com/caneco "caneco (1 commits)")

---

Tags

fakehacktoberfestlaravelloggingphptestinglogtestinglaravelloggerfake

### Embed Badge

![Health badge](/badges/timacdonald-log-fake/health.svg)

```
[![Health](https://phpackages.com/badges/timacdonald-log-fake/health.svg)](https://phpackages.com/packages/timacdonald-log-fake)
```

###  Alternatives

[laravel-zero/framework

The Laravel Zero Framework.

3371.4M369](/packages/laravel-zero-framework)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[sti3bas/laravel-scout-array-driver

Array driver for Laravel Scout

971.5M3](/packages/sti3bas-laravel-scout-array-driver)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[guanguans/laravel-soar

SQL optimizer and rewriter for laravel. - laravel 的 SQL 优化器和重写器。

2227.8k](/packages/guanguans-laravel-soar)[dragon-code/laravel-http-logger

Logging incoming HTTP requests

319.8k3](/packages/dragon-code-laravel-http-logger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
