Using verbose in Laravel artisan commands

13.3k Views Asked by At

Is there a way to detect what verbosity level the user has specified when creating a custom artisan command? I don't see anything about it in the docs.

2

There are 2 best solutions below

4
On BEST ANSWER

There's the getVerbosity() function in Symfony\Component\Console\Output\OutputInterface and you can use $this->getOutput() to retrieve the output object.

$verbosityLevel = $this->getOutput()->getVerbosity();

You then can compare the level to the constants defined inside OutputInterface. For example:

if($verbosityLevel >= OutputInterface::VERBOSITY_VERBOSE){
    // show verbose messages
}
0
On

You can use different verbosities as per the documentation:

https://laravel.com/api/6.x/Illuminate/Console/OutputStyle.html#method_isQuiet

isQuiet()       - no verbosity is set                                   (no option set)
isVerbose()     - if the level is quiet or verbose                      (-v)
isVeryVerbose() - if the level is very verbose, verbose or quiet        (-vv)
isDebug()       - if the level is debug, very verbose, verbose or quiet (-vvv)

e.g. In your command $this->getOutput()->isQuiet()

This also affects writeLn(). If you were to write $this->line('Serious message', null, 'vv'); The message would appear for -vv and -vvv options, but not -v and silent modes as it is "too detailed" for those levels of logging.