问题
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.
回答1:
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
}
回答2:
You can use different verbosities as per the documentation:
https://laravel.com/api/5.6/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.
来源:https://stackoverflow.com/questions/27611213/using-verbose-in-laravel-artisan-commands