Using verbose in Laravel artisan commands

大憨熊 提交于 2019-12-06 19:02:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!