Esempio n. 1
0
 /**
  * @see \PHPLog\FilterAbstract::decide()
  */
 public function decide(Event $event)
 {
     //if level max is not a Level.
     if (!$this->levelMax instanceof Level) {
         return FilterAbstract::NEUTRAL;
     }
     //if level min is not a level.
     if (!$this->levelMin instanceof Level) {
         return FilterAbstract::NEUTRAL;
     }
     //if level min is equal to all or is greater than max.
     if ($this->levelMin->equals(Level::all()) || $this->levelMin->isGreaterOrEqualTo($this->levelMax)) {
         return FilterAbstract::NEUTRAL;
     }
     //if max if equal to off.
     if ($this->levelMax->equals(Level::off())) {
         return FilterAbstract::NEUTRAL;
     }
     //check that the event level is between the range.
     $lmi = $this->levelMin->getIntLevel();
     $lma = $this->levelMax->getIntLevel();
     $e = $event->getLevel()->getIntLevel();
     if ($e >= $lmi && $e <= $lma) {
         //in the range.
         return $this->acceptOnMatch ? FilterAbstract::ACCEPT : FilterAbstract::DENY;
     }
     return FilterAbstract::NEUTRAL;
 }
Esempio n. 2
0
 /**
  * Constructor - initializes this logger with a level and a
  * default EchoWriter writer (which means all logs are printed on screen)
  * @param Level $level [optional] the level in which to set the root logger.
  * defaulted to ALL.
  */
 public function __construct(Level $level = null)
 {
     parent::__construct('root');
     if ($level == null) {
         $level = Level::all();
     }
     $this->setLevel($level);
 }
Esempio n. 3
0
 /**
  * initializes this writer with the provided configuration.
  * @param Configuration $config the configuration provided to this writer.
  * @see WriterAbstract::init()
  */
 public function init(Configuration $config)
 {
     //check to see if the config contains a set useMinErrorLevel and that its a boolean value.
     $this->useMinErrorLevel = $config->get('useMinErrorLevel', true);
     //check to see if the minErrorLevel is set and that its a level instance and useMinErrorLevel is true.
     $this->minErrorLevel = $config->get('minErrorLevel', Level::error(), function ($level) {
         return $level instanceof Level;
     });
     //check that the user provided a valid target stream.
     $this->target = $config->get('target', self::STDOUT, function ($target) {
         return isset($target) && in_array($target, array(self::STDOUT, self::STDERR, self::OUTPUT));
     });
     $this->getLayoutConfig()->set('pattern', $this->pattern, true);
     //set the layout for this writer.
     $this->setLayout(new Pattern());
 }
Esempio n. 4
0
 /**
  * determines the syslog level to use based on the level in the log
  * event.
  * @param Level $logLevel the level that we are parsing.
  * @return int the syslog level that resembles the $levels level.
  */
 private function getSyslogEquivilentLevel(Level $logLevel)
 {
     $level = LOG_DEBUG;
     switch ($logLevel->getIntLevel()) {
         case Level::FATAL:
             $level = LOG_ALERT;
             break;
         case Level::ERROR:
             $level = LOG_ERR;
             break;
         case Level::WARN:
             $level = LOG_WARNING;
             break;
         case Level::INFO:
             $level = LOG_INFO;
             break;
         default:
             $level = LOG_DEBUG;
             break;
     }
     return $level;
 }
Esempio n. 5
0
 /**
  * @override
  * Constructor - Initailizes the Mail transport and the mailer class and checks the validity of the
  * emails in config.
  * @param array $config the configuration for this writer.
  */
 public function init(Configuration $config)
 {
     $transport = $config->transport;
     if (!$transport instanceof Configuration || count($transport) == 0) {
         throw new \Exception('no transport config provided');
     }
     //check we have a transport type.
     if (!isset($transport->type) || !in_array($transport->type, array(Mail::TRANSPORT_TYPE_MAIL, Mail::TRANSPORT_TYPE_SENDMAIL, Mail::TRANSPORT_TYPE_SMTP))) {
         throw new \Exception('no valid transport type defined');
     }
     //get the transport parameters.
     $params = clone $transport;
     unset($param->type);
     $type = $tranport->type;
     //check we have enough overall args.
     if (count($params) == 0 && $type != Mail::TRANSPORT_TYPE_MAIL) {
         throw new \Exception('not enough provided arguments for transport type: ' . $type);
     }
     //check we have a username, password and host if the type is SMTP
     if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('username', $params) || !array_key_exists('password', $params) || !array_key_exists('host', $params))) {
         throw new \Exception('smtp needs a username, password and host');
     }
     //determine that the host/command is set dependant on the type.
     if ($type == Mail::TRANSPORT_TYPE_SENDMAIL && !array_key_exists('command', $params)) {
         throw new \Exception('sendmail needs a command to execute.');
     }
     //determine if an optional port is set for SMTP.
     if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('port', $params) || !is_numeric($params['port']))) {
         $params['port'] = 25;
     }
     //determine if an optional security is set for SMTP.
     if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('ssl', $params) || !is_bool($params['ssl']))) {
         $bool = isset($params['ssl']) ? filter_var($params['ssl'], FILTER_VALIDATE_BOOLEAN) : false;
         $params['ssl'] = $bool;
     }
     $ssl = $params['ssl'] ? 'ssl' : null;
     //initialize the transport.
     $className = "\\Swift_{$type}Transport";
     $transport = $type == Mail::TRANSPORT_TYPE_SENDMAIL ? $className::newInstance($params['command']) : ($type == Mail::TRANSPORT_TYPE_SMTP ? $className::newInstance($params['host'], $params['port'], $ssl) : $className::newInstance());
     //if transport is SMTP, set the username and password.
     if ($type == Mail::TRANSPORT_TYPE_SMTP) {
         $transport->setUsername($params['username'])->setPassword($params['password']);
     }
     //initialize the mailer.
     $this->transport = $transport;
     $this->mailer = \Swift_Mailer::newInstance($transport);
     //check the to and from emails for validity.
     if (!isset($config['to']) || !isset($config['from'])) {
         throw new \Exception('to or from email is not set.');
     }
     foreach (array($config['to'], $config['from']) as $email) {
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             throw new \Exception('could not validate to and from emails.');
         }
     }
     $this->to = $config['to'];
     $this->from = $config['from'];
     //set the default subject if one is not set.
     if (!array_key_exists('subject', $config) || strlen($config['subject']) == 0) {
         $config['subject'] = 'PHPLog Mail Writer';
     }
     $this->subject = $config['subject'];
     //finally see if the users set any custom threshold for just this writer.
     $this->threshold = $config->threshold instanceof Level ? $config['threshold'] : Level::all();
     //set the layout.
     $this->getLayoutConfig()->set('pattern', $this->pattern, true);
     $this->setLayout(new Pattern());
 }
Esempio n. 6
0
 /**
  * Determines if this logger can handle the requested log level.
  * @param Level $level the log level to test.
  * @return boolean <b>TRUE</b> if this logger can handle the requested level
  * <b>FALSE</b> otherwise.
  */
 public function isEnabledFor(Level $level)
 {
     return $level->isGreaterOrEqualTo($this->getPropogatedLevel());
 }
Esempio n. 7
0
 /**
  * sets any initial configuration for a logger.
  * @param Logger $logger the logger to configure.
  * @param array  $config the config to configure the logger with.
  * @return Logger the configured logger.
  */
 private function setInitialConfiguration(&$logger, $config)
 {
     if (isset($config['level'])) {
         $level = $config['level'];
         if (!$level instanceof Level) {
             $level = Level::parseFromString($level);
         }
         if ($level instanceof Level) {
             $logger->setLevel($level);
         }
     }
     if (isset($config['enablePropogation']) && is_bool($config['enablePropogation'])) {
         $logger->setPropogation($config['enablePropogation']);
     }
     //see if any global extras or logger extras have been added to the config.
     if (isset($config['extras'])) {
         if (isset($config['extras']['global']) && is_array($config['extras']['global']) && count($config['extras']['global']) > 0) {
             foreach ($config['extras']['global'] as $key => $value) {
                 Logger::addGlobalExtra($key, $value);
             }
         }
         if (isset($config['extras']['local']) && is_array($config['extras']['local']) && count($config['extras']['local']) > 0) {
             foreach ($config['extras']['local'] as $key => $value) {
                 $logger->addExtra($key, $value);
             }
         }
     }
     if (isset($config['writers'])) {
         //set all of the valid writers.
         foreach ($config['writers'] as $name => $writerConf) {
             $className = '\\PHPLog\\Writer\\' . $name;
             if (class_exists($className)) {
                 $writer = new $className($writerConf);
                 $logger->addWriter($writer);
             }
         }
     }
     if (isset($config['filters'])) {
         //set all valid filters.
         foreach ($config['filters'] as $name => $filterConf) {
             $className = '\\PHPLog\\Filter\\' . $name;
             if (class_exists($className)) {
                 $filter = new $className($writerConf);
                 $logger->addFilter($filter);
             }
         }
     }
     if (isset($config['renderers'])) {
         foreach ($config['renderers'] as $class => $renderer) {
             if (!$renderer instanceof RendererInterface) {
                 if (!is_string($renderer)) {
                     return;
                 }
                 if (!class_exists($renderer)) {
                     $renderer = '\\PHPLog\\Renderer\\' . $renderer;
                     if (!class_exists($renderer)) {
                         return;
                     }
                 }
                 $renderer = new $renderer();
             }
             $logger->addRenderer($class, $renderer);
         }
     }
     return $logger;
 }
Esempio n. 8
0
 /**
  * Mainly tests that we can set a logger to a threshold.
  * Also confirms the functionality of disabling propogation and setting
  * level and 'enablePropogation' in the configuration array.
  */
 public function testSetLoggerThreshold()
 {
     //clear any previous configuration (as hierarchy is stored between calls)
     Logger::getHierarchy()->clear();
     $logger = Logger::getLogger('test_logger', array('enablePropogation' => false, 'level' => Level::warn(), 'writers' => array('EchoWriter' => array())));
     $tMessage = 'Tester Message';
     //attempt an info message, should be no output.
     ob_start();
     $logger->info($tMessage);
     $message = ob_get_clean();
     $this->assertEquals('', $message);
     //attempt a fatal message, should succeed.
     ob_start();
     $logger->fatal($tMessage);
     $message = ob_get_clean();
     $this->assertNotEquals('', $message);
 }