/**
  * @expectedException RuntimeException
  */
 public function testHandleWithBadCallbackThrowsException()
 {
     $handler = new FingersCrossedHandler(function ($record, $handler) {
         return 'foo';
     });
     $handler->handle($this->getRecord(Logger::WARNING));
 }
 /**
  * @covers Monolog\Handler\FingersCrossedHandler::close
  */
 public function testPsrLevelPassthruOnClose()
 {
     $test = new TestHandler();
     $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, LogLevel::INFO);
     $handler->handle($this->getRecord(Logger::DEBUG));
     $handler->handle($this->getRecord(Logger::INFO));
     $handler->close();
     $this->assertFalse($test->hasDebugRecords());
     $this->assertTrue($test->hasInfoRecords());
 }
Example #3
0
 /**
  * log.ymlの内容に応じたHandlerの設定を行う
  *
  * @param array $channelValues
  * @return FingersCrossedHandler
  */
 public function getHandler(array $channelValues)
 {
     $app = $this->app;
     $levels = Logger::getLevels();
     // ファイル名などの設定を行い、設定がなければデフォルト値を設定
     $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename'];
     $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter'];
     $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat'];
     $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level'];
     $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level'];
     $passthruLevel = isset($channelValues['passthru_level']) ? $channelValues['passthru_level'] : $app['config']['log']['passthru_level'];
     $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files'];
     $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat'];
     $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format'];
     if ($app['debug']) {
         $level = Logger::DEBUG;
     } else {
         $level = $logLevel;
     }
     // RotateHandlerの設定
     $filename = $app['config']['root_dir'] . '/app/log/' . $logFileName . '.log';
     $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
     $RotateHandler->setFilenameFormat($logFileName . $delimiter . '{date}' . $app['config']['log']['suffix'], $dateFormat);
     // ログフォーマットの設定(設定ファイルで定義)
     $RotateHandler->setFormatter(new LineFormatter($logFormat . PHP_EOL, $logDateFormat, true, true));
     // FingerCossedHandlerの設定
     $FingerCrossedHandler = new FingersCrossedHandler($RotateHandler, new ErrorLevelActivationStrategy($levels[$actionLevel]), 0, true, true, $levels[$passthruLevel]);
     // Processorの内容をログ出力
     $webProcessor = new WebProcessor();
     $uidProcessor = new UidProcessor(8);
     $FingerCrossedHandler->pushProcessor(function ($record) use($app, $uidProcessor, $webProcessor) {
         // ログフォーマットに出力する値を独自に設定
         $record['level_name'] = sprintf("%-5s", $record['level_name']);
         // セッションIDと会員IDを設定
         $record['session_id'] = null;
         $record['user_id'] = null;
         if ($app->isBooted()) {
             if (isset($app['session'])) {
                 $sessionId = $app['session']->getId();
                 if ($sessionId) {
                     $record['session_id'] = substr(sha1($sessionId), 0, 8);
                 }
             }
             if (isset($app['user'])) {
                 $user = $app->user();
                 if ($user instanceof Customer || $user instanceof Member) {
                     $record['user_id'] = $user->getId();
                 }
             }
         }
         $record['uid'] = $uidProcessor->getUid();
         $record['url'] = $webProcessor->getRequestUri();
         $record['ip'] = $webProcessor->getClientIp();
         $record['referrer'] = $webProcessor->getReferer();
         $record['method'] = $webProcessor->getMethod();
         $record['user_agent'] = $webProcessor->getUserAgent();
         // クラス名などを一旦保持し、不要な情報は削除
         $line = $record['extra']['line'];
         $functionName = $record['extra']['function'];
         // php5.3だとclass名が取得できないため、ファイル名を元に出力
         // $className = $record['extra']['class'];
         $className = $record['extra']['file'];
         // 不要な情報を削除
         unset($record['extra']['file']);
         unset($record['extra']['line']);
         unset($record['extra']['class']);
         unset($record['extra']['function']);
         $record['class'] = pathinfo($className, PATHINFO_FILENAME);
         $record['function'] = $functionName;
         $record['line'] = $line;
         return $record;
     });
     // クラス名等を取得するProcessor、ログ出力時にクラス名/関数名を無視するための設定を行っている
     $skipClasses = array('Psr\\Log\\', 'Eccube\\Log\\');
     $skipFunctions = array('log_info', 'log_notice', 'log_warning', 'log_error', 'log_critical', 'log_alert', 'log_emergency');
     $intro = new IntrospectionProcessor(Logger::DEBUG, $skipClasses, $skipFunctions);
     $FingerCrossedHandler->pushProcessor($intro);
     return $FingerCrossedHandler;
 }
 /**
  * @covers Monolog\Handler\FingersCrossedHandler::handle
  */
 public function testHandleUsesProcessors()
 {
     $test = new TestHandler();
     $handler = new FingersCrossedHandler($test, Logger::INFO);
     $handler->pushProcessor(function ($record) {
         $record['extra']['foo'] = true;
         return $record;
     });
     $handler->handle($this->getRecord(Logger::WARNING));
     $this->assertTrue($test->hasWarningRecords());
     $records = $test->getRecords();
     $this->assertTrue($records[0]['extra']['foo']);
 }
 /**
  * @covers Monolog\Handler\FingersCrossedHandler::isHandling
  */
 public function testIsHandlingAlways()
 {
     $test = new TestHandler();
     $handler = new FingersCrossedHandler($test, Logger::ERROR);
     $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
 }
 /**
  * @param callable|HandlerInterface $handler
  * @param array $alwaysReport
  * @param null $activationStrategy
  * @param int $bufferSize
  * @param bool $bubble
  * @param bool $stopBuffering
  * @param null $passthruLevel
  */
 public function __construct($handler, array $alwaysReport = [], $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true, $passthruLevel = NULL)
 {
     $this->alwaysReport = $alwaysReport;
     parent::__construct($handler, $activationStrategy, $bufferSize, $bubble, $stopBuffering, $passthruLevel);
     // TODO: Change the autogenerated stub
 }
 public function clear()
 {
     $this->entityManager->clear();
     $this->fingersCrossedHandler->clear();
 }
 /**
  * @covers Monolog\Handler\FingersCrossedHandler::__construct
  */
 public function testActivationStrategy()
 {
     $test = new TestHandler();
     $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
     $handler->handle($this->getRecord(Logger::DEBUG));
     $this->assertFalse($test->hasDebugRecords());
     $handler->handle($this->getRecord(Logger::WARNING));
     $this->assertTrue($test->hasDebugRecords());
     $this->assertTrue($test->hasWarningRecords());
 }