/**
  * Setup db connection.
  * Based on defined options, this method connects to db defined in {@link $dsn}
  * and creates a {@link $table} table if {@link $createTable} is true.
  * @return boolean true if all ok.
  */
 function activateOptions()
 {
     $this->db = \Scalr::getDb();
     $this->layout = LoggerReflectionUtils::createObject('LoggerPatternLayoutScalr');
     $this->layout->setConversionPattern($this->getSql());
     $this->canAppend = true;
 }
 /**
  * Setup db connection.
  * Based on defined options, this method connects to db defined in {@link $dsn}
  * and creates a {@link $table} table if {@link $createTable} is true.
  * @return boolean true if all ok.
  */
 function activateOptions()
 {
     $this->db = Core::GetDBInstance();
     //null, true);
     $this->layout = LoggerReflectionUtils::createObject('LoggerPatternLayoutScalr');
     $this->layout->setConversionPattern($this->getSql());
     $this->canAppend = true;
 }
 /**
  * Add a renderer to a hierarchy passed as parameter.
  * Note that hierarchy must implement getRendererMap() and setRenderer() methods.
  *
  * @param LoggerHierarchy $repository a logger repository.
  * @param string $renderedClassName
  * @param string $renderingClassName
  * @static
  */
 public function addRenderer($renderedClassName, $renderingClassName)
 {
     $renderer = LoggerReflectionUtils::createObject($renderingClassName);
     if ($renderer == null) {
         return;
     } else {
         $this->put($renderedClassName, $renderer);
     }
 }
 /**
  * Add a renderer to a hierarchy passed as parameter.
  * Note that hierarchy must implement getRendererMap() and setRenderer() methods.
  *
  * @param LoggerHierarchy $repository a logger repository.
  * @param string $renderedClassName
  * @param string $renderingClassName
  * @static
  */
 public static function addRenderer($repository, $renderedClassName, $renderingClassName)
 {
     $renderer = LoggerReflectionUtils::createObject($renderingClassName);
     if ($renderer == null) {
         return;
     } else {
         $repository->setRenderer($renderedClassName, $renderer);
     }
 }
 /**
  * 
  *
  * @param string $name 
  * @param string $class 
  * @return LoggerAppender
  */
 public static function getAppenderFromPool($name, $class = '')
 {
     if (isset(self::$appenderPool[$name])) {
         return self::$appenderPool[$name];
     }
     if (empty($class)) {
         return null;
     }
     $appender = LoggerReflectionUtils::createObject($class);
     $appender->setName($name);
     if ($appender !== null) {
         self::$appenderPool[$name] = $appender;
         return self::$appenderPool[$name];
     }
     return null;
 }
 /**
  * @param mixed $parser
  * @param string $tag
  */
 private function tagClose($parser, $tag)
 {
     switch ($tag) {
         case 'CONFIGURATION':
         case self::XMLNS . ':CONFIGURATION':
             break;
         case 'APPENDER':
         case self::XMLNS . ':APPENDER':
             if ($this->appender !== null) {
                 if ($this->appender->requiresLayout() and $this->appender->getLayout() === null) {
                     $appenderName = $this->appender->getName();
                     $this->appender->setLayout(LoggerReflectionUtils::createObject('LoggerLayoutSimple'));
                 }
                 $this->appender->activateOptions();
             }
             array_pop($this->state);
             break;
         case 'FILTER':
         case self::XMLNS . ':FILTER':
             if ($this->filter !== null) {
                 $this->filter->activateOptions();
                 $this->appender->addFilter($this->filter);
                 $this->filter = null;
             }
             array_pop($this->state);
             break;
         case 'LAYOUT':
         case self::XMLNS . ':LAYOUT':
             if ($this->appender !== null and $this->layout !== null and $this->appender->requiresLayout()) {
                 $this->layout->activateOptions();
                 $this->appender->setLayout($this->layout);
                 $this->layout = null;
             }
             array_pop($this->state);
             break;
         case 'LOGGER':
         case self::XMLNS . ':LOGGER':
             array_pop($this->state);
             break;
         case 'ROOT':
         case self::XMLNS . ':ROOT':
             array_pop($this->state);
             break;
     }
 }
Exemple #7
0
 /**
  * Initializes the log4php framework.
  * @return boolean
  */
 public static function initialize()
 {
     self::$initialized = true;
     $instance = LoggerReflectionUtils::createObject(self::$configurationClass);
     $result = $instance->configure(self::getHierarchy(), self::$configurationFile);
     return $result;
 }
 /**
  * @param array $props array of properties
  * @param string $appenderName
  * @return LoggerAppender
  */
 private function parseAppender($props, $appenderName)
 {
     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
     $prefix = self::APPENDER_PREFIX . $appenderName;
     if ($appender === null) {
         // Appender was not previously initialized.
         $appenderClass = @$props[$prefix];
         $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderClass);
         if ($appender === null) {
             return null;
         }
     }
     if ($appender->requiresLayout()) {
         $layoutPrefix = $prefix . ".layout";
         $layoutClass = @$props[$layoutPrefix];
         $layoutClass = LoggerOptionConverter::substVars($layoutClass, $props);
         if (empty($layoutClass)) {
             $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
         } else {
             $layout = LoggerReflectionUtils::createObject($layoutClass);
             if ($layout === null) {
                 $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
             }
         }
         LoggerReflectionUtils::setPropertiesByObject($layout, $props, $layoutPrefix . ".");
         $appender->setLayout($layout);
     }
     LoggerReflectionUtils::setPropertiesByObject($appender, $props, $prefix . ".");
     return $appender;
 }
 public function testCreateObject()
 {
     $object = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
     $name = get_class($object);
     self::assertEquals($name, 'LoggerLayoutSimple');
 }
 /**
  * Setup db connection.
  * Based on defined options, this method connects to db defined in {@link $dsn}
  * and creates a {@link $table} table if {@link $createTable} is true.
  * @return boolean true if all ok.
  */
 function activateOptions()
 {
     $this->db =& ADONewConnection($this->type);
     if (!$this->db->PConnect($this->host, $this->user, $this->password, $this->database)) {
         $this->db = null;
         $this->closed = true;
         $this->canAppend = false;
         return;
     }
     $this->layout = LoggerReflectionUtils::createObject('LoggerLayoutPattern');
     $this->layout->setConversionPattern($this->getSql());
     // test if log table exists
     $sql = 'select * from ' . $this->table . ' where 1 = 0';
     $dbrs = $this->db->Execute($sql);
     if ($dbrs == false and $this->getCreateTable()) {
         $query = "CREATE TABLE {$this->table} (timestamp varchar(32),logger varchar(32),level varchar(32),message varchar(64),thread varchar(32),file varchar(64),line varchar(4) );";
         $result = $this->db->Execute($query);
         if (!$result) {
             $this->canAppend = false;
             return;
         }
     }
     $this->canAppend = true;
 }
 public function testFactory()
 {
     $renderer = LoggerReflectionUtils::createObject('LoggerRendererDefault');
     self::assertType('LoggerRendererDefault', $renderer);
 }
 /**
  * Create a socket connection using defined parameters
  */
 public function activateOptions()
 {
     if (!$this->dry) {
         $this->sp = @fsockopen($this->getRemoteHost(), $this->getPort(), $errno, $errstr, $this->getTimeout());
         if ($this->sp === false) {
             throw new LoggerException("Could not open socket to " . $this->getRemoteHost() . ":" . $this->getPort() . ": {$errstr} ({$errno})");
         }
     }
     if ($this->getUseXml()) {
         $this->xmlLayout = LoggerReflectionUtils::createObject('LoggerLayoutXml');
         if ($this->xmlLayout === null) {
             $this->setUseXml(false);
         } else {
             $this->xmlLayout->setLocationInfo($this->getLocationInfo());
             $this->xmlLayout->setLog4jNamespace($this->getLog4jNamespace());
             $this->xmlLayout->activateOptions();
         }
     }
     $this->closed = false;
 }
 private function doConfigure($url, LoggerHierarchy $hierarchy)
 {
     $config = (require $url);
     // set threshold
     if (isset($config['threshold'])) {
         $hierarchy->setThreshold(LoggerOptionConverter::toLevel($config['threshold'], LoggerLevel::getLevelAll()));
     }
     // parse and create appenders
     if (isset($config['appenders'])) {
         foreach ($config['appenders'] as $appenderName => $appenderProperties) {
             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderProperties['class']);
             if ($appender->requiresLayout()) {
                 if (isset($appenderProperties['layout'])) {
                     if (isset($appenderProperties['layout']['class']) and !empty($appenderProperties['layout']['class'])) {
                         $layoutClass = $appenderProperties['layout']['class'];
                     } else {
                         $layoutClass = 'LoggerLayoutSimple';
                     }
                     $layout = LoggerReflectionUtils::createObject($layoutClass);
                     if ($layout === null) {
                         $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
                     }
                     if ($layout instanceof LoggerLayoutPattern) {
                         $layout->setConversionPattern($appenderProperties['layout']['conversionPattern']);
                     }
                     $appender->setLayout($layout);
                 } else {
                     // TODO: throw exception?
                 }
             }
         }
     }
     // parse and create root logger
     if (isset($config['rootLogger'])) {
         $rootLogger = $hierarchy->getRootLogger();
         if (isset($config['rootLogger']['level'])) {
             $rootLogger->setLevel(LoggerOptionConverter::toLevel($config['rootLogger']['level'], LoggerLevel::getLevelDebug()));
             if (isset($config['rootLogger']['appenders'])) {
                 foreach ($config['rootLogger']['appenders'] as $appenderName) {
                     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                     if ($appender !== null) {
                         $rootLogger->addAppender($appender);
                     }
                 }
             }
         }
     }
     // parse and create loggers
     if (isset($config['loggers'])) {
         foreach ($config['loggers'] as $loggerName => $loggerProperties) {
             if (is_string($loggerName)) {
                 $logger = $hierarchy->getLogger($loggerName);
                 if (isset($loggerProperties['level'])) {
                     $logger->setLevel(LoggerOptionConverter::toLevel($loggerProperties['level'], LoggerLevel::getLevelDebug()));
                     if (isset($loggerProperties['appenders'])) {
                         foreach ($loggerProperties['appenders'] as $appenderName) {
                             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                             if ($appender !== null) {
                                 $logger->addAppender($appender);
                             }
                         }
                     }
                 }
             } else {
                 // TODO: throw exception
             }
         }
     }
     return true;
 }
 private function doConfigure($url, LoggerHierarchy $hierarchy)
 {
     if (!is_array($url)) {
         $config = (require $url);
     } else {
         $config = $url;
     }
     // set threshold
     if (isset($config['threshold'])) {
         $hierarchy->setThreshold(LoggerOptionConverter::toLevel($config['threshold'], LoggerLevel::getLevelAll()));
     }
     // add renderes
     if (isset($config['renderers'])) {
         foreach ($config['renderers'] as $renderedClass => $renderingClass) {
             $hierarchy->getRendererMap()->addRenderer($renderedClass, $renderingClass);
         }
     }
     // parse and create appenders
     if (isset($config['appenders'])) {
         foreach ($config['appenders'] as $appenderName => $appenderProperties) {
             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName, $appenderProperties['class']);
             // unset so that the property wont be drawn up again
             unset($appenderProperties['class']);
             if ($appender->requiresLayout()) {
                 if (isset($appenderProperties['layout'])) {
                     if (isset($appenderProperties['layout']['class']) and !empty($appenderProperties['layout']['class'])) {
                         $layoutClass = $appenderProperties['layout']['class'];
                     } else {
                         $layoutClass = 'LoggerLayoutSimple';
                     }
                     $layout = LoggerReflectionUtils::createObject($layoutClass);
                     if ($layout === null) {
                         $layout = LoggerReflectionUtils::createObject('LoggerLayoutSimple');
                     }
                     if (isset($appenderProperties['file']) && method_exists($appender, 'setFileName')) {
                         $appender->setFile($appenderProperties['file'], true);
                     }
                     if ($layout instanceof LoggerLayoutPattern) {
                         $layout->setConversionPattern($appenderProperties['layout']['conversionPattern']);
                     }
                     $appender->setLayout($layout);
                     // unset so that the property wont be drawn up again
                     unset($appenderProperties['layout']);
                 } else {
                     // TODO: throw exception?
                 }
             }
             // set remaining properties and activate appender
             $setter = new LoggerReflectionUtils($appender);
             foreach ($appenderProperties as $key => $val) {
                 $setter->setProperty($key, $val);
             }
             $setter->activate();
         }
     }
     // parse and create root logger
     if (isset($config['rootLogger'])) {
         $rootLogger = $hierarchy->getRootLogger();
         if (isset($config['rootLogger']['level'])) {
             $rootLogger->setLevel(LoggerOptionConverter::toLevel($config['rootLogger']['level'], LoggerLevel::getLevelDebug()));
             if (isset($config['rootLogger']['appenders'])) {
                 foreach ($config['rootLogger']['appenders'] as $appenderName) {
                     $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                     if ($appender !== null) {
                         $rootLogger->addAppender($appender);
                     }
                 }
             }
         }
     }
     // parse and create loggers
     if (isset($config['loggers'])) {
         foreach ($config['loggers'] as $loggerName => $loggerProperties) {
             if (is_string($loggerName)) {
                 $logger = $hierarchy->getLogger($loggerName);
                 if (isset($loggerProperties['level'])) {
                     $logger->setLevel(LoggerOptionConverter::toLevel($loggerProperties['level'], LoggerLevel::getLevelDebug()));
                     if (isset($loggerProperties['appenders'])) {
                         foreach ($loggerProperties['appenders'] as $appenderName) {
                             $appender = LoggerAppenderPool::getAppenderFromPool($appenderName);
                             if ($appender !== null) {
                                 $logger->addAppender($appender);
                             }
                         }
                     }
                 }
             } else {
                 // TODO: throw exception
             }
         }
     }
     return true;
 }
 /**
  * Setup db connection.
  * Based on defined options, this method connects to db defined in {@link $dsn}
  * and creates a {@link $table} table if {@link $createTable} is true.
  * @return boolean true if all ok.
  * @throws a PDOException if the attempt to connect to the requested database fails.
  */
 public function activateOptions()
 {
     try {
         if ($this->user === null) {
             $this->db = new PDO($this->dsn);
         } else {
             if ($this->password === null) {
                 $this->db = new PDO($this->dsn, $this->user);
             } else {
                 $this->db = new PDO($this->dsn, $this->user, $this->password);
             }
         }
         $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         // test if log table exists
         try {
             $result = $this->db->query('select * from ' . $this->table . ' where 1 = 0');
         } catch (PDOException $e) {
             // It could be something else but a "no such table" is the most likely
             $result = false;
         }
         // create table if necessary
         if ($result == false and $this->createTable) {
             // TODO mysql syntax?
             $query = "CREATE TABLE {$this->table} (\t timestamp varchar(32)," . "logger varchar(32)," . "level varchar(32)," . "message varchar(64)," . "thread varchar(32)," . "file varchar(64)," . "line varchar(4) );";
             $result = $this->db->query($query);
         }
     } catch (PDOException $e) {
         $this->canAppend = false;
         throw new LoggerException($e);
     }
     if ($this->sql == '' || $this->sql == null) {
         $this->sql = "INSERT INTO {$this->table} ( timestamp, " . "logger, " . "level, " . "message, " . "thread, " . "file, " . "line" . ") VALUES ('%d','%c','%p','%m','%t','%F','%L')";
     }
     $this->layout = LoggerReflectionUtils::createObject('LoggerLayoutPattern');
     $this->layout->setConversionPattern($this->sql);
     $this->canAppend = true;
     return true;
 }