Beispiel #1
0
 public function addHandler(Handler $handler)
 {
     $name = $handler->getName();
     if (!isset($this->handlers[$name])) {
         $this->handlers[$name] = $handler;
     } else {
         throw new \Exception('Handler' . $name . ' already exists.');
     }
 }
Beispiel #2
0
 /**
  * Contruct a FileHandler
  *
  * @param string $name the name for this handler
  * @param string $basePath the base directory for log files
  * @param string $prefix for filename, allowed pattern: '[A-Za-z\.\-\_]*',
  *      prefix should not be blank if type is TYPE_BY_SIZE
  * @param integer $type separate logs by log file size or by date
  * @param mixed $pattern, if type is TYPE_BY_SIZE, then $pattern specifies the
  * file size in bytes, if type is TYPE_BY_DATE, its the pattern for date($pattern) 
  */
 public function __construct($name, $basePath, $prefix = 'log', $type = self::TYPE_BY_DATE, $pattern = 'Ymd', $extension = '.log', $bufferFlushCount = 10)
 {
     if (!is_dir($basePath) || !is_writable($basePath)) {
         throw new \Exception($basePath . ' is not a directory or has no permission to write');
     }
     $this->basePath = $basePath;
     if (!preg_match('/^[A-Za-z\\.\\-_]*$/', $prefix)) {
         throw new \Exception('Invalid prefix: ' . $prefix);
     }
     $this->filePrefix = $prefix;
     if ($type == self::TYPE_BY_DATE) {
         $this->pattern = $pattern;
     } else {
         if ($type == self::TYPE_BY_SIZE) {
             if (strlen($this->filePrefix) < 1) {
                 throw new Exception('Prefix should be specified');
             }
             $this->pattern = (int) $pattern;
             if ($this->pattern < 512) {
                 throw new \Exception('File size limitation for log file should be larger than 512 bytes');
             }
         } else {
             throw new \Exception('Invalid type for FileHandler');
         }
     }
     parent::__construct($name);
     $this->type = $type;
     $this->extension = trim($extension);
     $this->buffers = '';
     $this->bufferCount = 0;
     $this->bufferFlushCount = (int) $bufferFlushCount;
     $this->open();
 }