Armin Sam
Armin Sam

Reputation: 933

How to disable displaying of application log messages on console while phpunit tests are running?

I am writing tests for a Laravel application, specifically for a process which writes a lot of log messages on console.

e.g.

Log::info('Process starts', [
    'process_name' => 'product_import',
    'data' => // a huge text containing json_encode of the given message object
]

When I run phpunit, I see all these annoying log messages on console. Is there a way to disable or somehow stop these log messages?

Upvotes: 7

Views: 5201

Answers (3)

Carlton
Carlton

Reputation: 5731

Quite an old issue but in my case (Laravel 8, PHP 7.4) this was being caused by the fact I had an environment variable set

# /.env

LOG_CHANNEL=stack`

referring to a logging configuration that used the single and stderr channels...

# /config/logging.php

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single', 'stderr'],
        'ignore_exceptions' => false,
    ],

stderr was my issue, after removing this I no longer saw application log output in my phpunit tests

Upvotes: 0

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

You can set environment variables in your phpunit.xml file.

If you increase the log level to notice it should prevent info notices from being output

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
    <env name="APP_LOG_LEVEL" value="notice"/>
</php>

If you're using Laravel 5.2 or older, there is no APP_LOG_LEVEL env variable, but you can sill introduce your own env variable and configure Monolog in your app service provider.

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

You could mock the Log facade if you want to turn off login in some specified test methods or classes.

https://laravel.com/docs/5.5/mocking#mocking-facades

Upvotes: 1

Related Questions