日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何在command中使用log

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在command中使用log 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

###1. 在命令行中手動記錄日志

// src/AppBundle/Command/GreetCommand.php namespace AppBundle\Command;use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Psr\Log\LoggerInterface;class GreetCommand extends ContainerAwareCommand #注意不是繼承的Command類 {// ...protected function execute(InputInterface $input, OutputInterface $output){/** @var $logger LoggerInterface */$logger = $this->getContainer()->get('logger');//直接獲取logger服務$name = $input->getArgument('name');if ($name) {$text = 'Hello '.$name;} else {$text = 'Hello';}if ($input->getOption('yell')) {$text = strtoupper($text);$logger->warning('Yelled: '.$text); //寫日志} else {$logger->info('Greeted: '.$text);}$output->writeln($text);} }

###2. 異常自動開啟

####在服務容器中配置一個console.exception事件的監聽者

# app/config/services.yml services:kernel.listener.command_dispatch:class: AppBundle\EventListener\ConsoleExceptionListenerarguments:logger: '@logger'tags:- { name: kernel.event_listener, event: console.exception }

####書寫監聽者類

// src/AppBundle/EventListener/ConsoleExceptionListener.php namespace AppBundle\EventListener;use Symfony\Component\Console\Event\ConsoleExceptionEvent; use Psr\Log\LoggerInterface;class ConsoleExceptionListener {private $logger;public function __construct(LoggerInterface $logger){$this->logger = $logger;}public function onConsoleException(ConsoleExceptionEvent $event){$command = $event->getCommand();$exception = $event->getException();$message = sprintf('%s: %s (uncaught exception) at %s line %s while running console command `%s`',get_class($exception),$exception->getMessage(),$exception->getFile(),$exception->getLine(),$command->getName());$this->logger->error($message, array('exception' => $exception));} }

當任何一個命令拋出異常;該監聽者就會接收到事件;事件會傳遞一個ConsoleExceptionEvent對象過來;根據該對象提供的信息記錄日志

###記錄非0退出狀態

####配置一個console.terminate事件的監聽者

services:kernel.listener.command_dispatch:class: AppBundle\EventListener\ErrorLoggerListenerarguments:logger: '@logger'tags:- { name: kernel.event_listener, event: console.terminate }

####實現監聽者

// src/AppBundle/EventListener/ErrorLoggerListener.php namespace AppBundle\EventListener;use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Psr\Log\LoggerInterface;class ErrorLoggerListener {private $logger;public function __construct(LoggerInterface $logger){$this->logger = $logger;}public function onConsoleTerminate(ConsoleTerminateEvent $event){$statusCode = $event->getExitCode(); //退出狀態碼$command = $event->getCommand();if ($statusCode === 0) {return;}if ($statusCode > 255) {$statusCode = 255;$event->setExitCode($statusCode);}//記錄退出狀態嗎$this->logger->warning(sprintf('Command `%s` exited with status code %d',$command->getName(),$statusCode));} }

轉載于:https://my.oschina.net/u/729139/blog/545812

總結

以上是生活随笔為你收集整理的如何在command中使用log的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。