Esempio n. 1
0
 /**
  * Get level code by name
  *
  * @param  string $level level name
  * @return int
  * @access public
  * @throws Exception\InvalidArgumentException
  *         if $throwException and not the right level name
  * @static
  * @api
  */
 public static function getLevelCode($level)
 {
     if (is_string($level) && isset(self::$levels[$level])) {
         return self::$levels[$level];
     }
     throw new Exception\InvalidArgumentException(Message::get(Message::INVALID_LOG_LEVEL, (string) $level), Message::INVALID_LOG_LEVEL);
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  */
 public function setDecorators(array $decorators, $flush = true)
 {
     // flush decorators first
     if ($flush) {
         $this->decorators = [];
     }
     // add decorators one by one
     if (count($decorators)) {
         foreach ($decorators as $deco) {
             if (!is_callable($deco)) {
                 throw new Exception\InvalidArgumentException(Message::get(Message::INVALID_LOG_DECORATOR, is_object($deco) ? get_class($deco) : strval($deco)), Message::INVALID_LOG_DECORATOR);
             }
             $this->addDecorator($deco);
         }
     }
 }
Esempio n. 3
0
 /**
  * {@inheritDoc}
  */
 public function __invoke(LogEntryInterface $log)
 {
     // open syslog
     if (!openlog($this->ident, $this->options, $this->facility)) {
         throw new Exception\InvalidArgumentException(Message::get(Message::INVALID_LOG_SYSLOG, $this->ident, $this->facility), Message::INVALID_LOG_SYSLOG);
     }
     // write syslog
     syslog($this->map[$log->getLevel()], call_user_func($this->getFormatter(), $log));
     // close syslog
     closelog();
 }
Esempio n. 4
0
 /**
  * Constructor
  *
  * @param  string|resource $stream the stream
  * @param  string $level level string
  * @param  array $configs (optional) properties to set
  * @throws Exception\InvalidArgumentException
  *         if $level not right, $stream not right
  * @access public
  * @api
  */
 public function __construct($stream = 'php://stdout', $level = LogLevel::NOTICE, array $configs = [])
 {
     // level
     $this->setHandleLevel($level);
     // auto-close stream ?
     $close = false;
     // file
     if (is_string($stream)) {
         $strm = null;
         // remove file:// prefix if any
         if ('file://' === substr($stream, 0, 7)) {
             $stream = substr($stream, 7);
         }
         if (strpos($stream, '://') !== false) {
             // not file
             $strm = @fopen($stream, 'a');
             $close = true;
         } else {
             // file & dir
             $dirname = dirname($stream);
             $dirok = true;
             if ($dirname && !is_dir($dirname)) {
                 $dirok = @mkdir($dirname, 0777, true);
             }
             if ($dirok) {
                 $strm = @fopen($stream, 'a');
                 $close = true;
             }
         }
         if (is_resource($strm)) {
             $stream = $strm;
         }
     }
     if (is_resource($stream)) {
         $this->stream = $stream;
         if ($close) {
             register_shutdown_function([__CLASS__, 'closeStream']);
         }
     } else {
         throw new Exception\InvalidArgumentException(Message::get(Message::INVALID_LOG_STREAM, $stream), Message::INVALID_LOG_STREAM);
     }
     // set other properties
     $this->setProperties($configs);
 }