Changeset 2291

Show
Ignore:
Timestamp:
10/01/2007 09:11:00 AM (14 months ago)
Author:
xue
Message:

added TDbLogRoute

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/HISTORY

    r2283 r2291  
     1Version 3.1.2 To be released 
     2============================ 
     3NEW: Added TDbLogRoute (Qiang) 
     4 
    15Version 3.1.1 October 1, 2007 
    26============================= 
  • trunk/framework/Util/TLogRouter.php

    r2112 r2291  
    1010 * @package System.Util 
    1111 */ 
     12 
     13Prado::using('System.Data.TDbConnection'); 
    1214 
    1315/** 
     
    676678        } 
    677679} 
     680 
     681 
     682/** 
     683 * TDbLogRoute class 
     684 * 
     685 * TDbLogRoute stores log messages in a database table. 
     686 * To specify the database table, set {@link setConnectionID ConnectionID} to be 
     687 * the ID of a {@link TDataSourceConfig} module and {@link setLogTableName LogTableName}. 
     688 * If they are not setting, an SQLite3 database named 'sqlite3.log' will be created and used 
     689 * under the runtime directory. 
     690 * 
     691 * By default, the database table name is 'pradolog'. It has the following structure: 
     692 * <code> 
     693 *      CREATE TABLE pradolog 
     694 *  ( 
     695 *              log_id INTEGER NOT NULL PRIMARY KEY, 
     696 *              level INTEGER, 
     697 *              category VARCHAR(128), 
     698 *              logtime VARCHAR(20), 
     699 *              message VARCHAR(255) 
     700 *   ); 
     701 * </code> 
     702 * 
     703 * @author Qiang Xue <qiang.xue@gmail.com> 
     704 * @version $Id$ 
     705 * @package System.Util 
     706 * @since 3.1.2 
     707 */ 
     708class TDbLogRoute extends TLogRoute 
     709{ 
     710        /** 
     711         * @var string the ID of TDataSourceConfig module 
     712         */ 
     713        private $_connID=''; 
     714        /** 
     715         * @var TDbConnection the DB connection instance 
     716         */ 
     717        private $_db; 
     718        /** 
     719         * @var string name of the DB log table 
     720         */ 
     721        private $_logTable='pradolog'; 
     722        /** 
     723         * @var boolean whether the log DB table should be created automatically 
     724         */ 
     725        private $_autoCreate=true; 
     726 
     727        /** 
     728         * Destructor. 
     729         * Disconnect the db connection. 
     730         */ 
     731        public function __destruct() 
     732        { 
     733                if($this->_db!==null) 
     734                        $this->_db->setActive(false); 
     735        } 
     736 
     737        /** 
     738         * Initializes this module. 
     739         * This method is required by the IModule interface. 
     740         * It initializes the database for logging purpose. 
     741         * @param TXmlElement configuration for this module, can be null 
     742         * @throws TConfigurationException if the DB table does not exist. 
     743         */ 
     744        public function init($config) 
     745        { 
     746                $db=$this->getDbConnection(); 
     747                $db->setActive(true); 
     748 
     749                $sql='SELECT * FROM '.$this->_logTable.' WHERE 0'; 
     750                try 
     751                { 
     752                        $db->createCommand($sql)->execute(); 
     753                } 
     754                catch(Exception $e) 
     755                { 
     756                        // DB table not exists 
     757                        if($this->_autoCreate) 
     758                                $this->createDbTable(); 
     759                        else 
     760                                throw TConfigurationException('db_logtable_inexistent',$this->_logTable); 
     761                } 
     762 
     763                parent::init($config); 
     764        } 
     765 
     766        /** 
     767         * Stores log messages into database. 
     768         * @param array list of log messages 
     769         */ 
     770        protected function processLogs($logs) 
     771        { 
     772                $sql='INSERT INTO '.$this->_logTable.'(level, category, logtime, message) VALUES (:level, :category, :logtime, :message)'; 
     773                $command=$this->getDbConnection()->createCommand($sql); 
     774                foreach($logs as $log) 
     775                { 
     776                        $command->bindValue(':level',$log[0]); 
     777                        $command->bindValue(':category',$log[1]); 
     778                        $command->bindValue(':logtime',$log[2]); 
     779                        $command->bindValue(':message',$log[3]); 
     780                        $command->execute(); 
     781                } 
     782        } 
     783 
     784        /** 
     785         * Creates the DB table for storing log messages. 
     786         */ 
     787        protected function createDbTable() 
     788        { 
     789                $sql='CREATE TABLE '.$this->_logTable.' ( 
     790                        log_id INTEGER NOT NULL PRIMARY KEY, 
     791                        level INTEGER, 
     792                        category VARCHAR(128), 
     793                        logtime VARCHAR(20), 
     794                        message VARCHAR(255))'; 
     795                $this->getDbConnection()->createCommand($sql)->execute(); 
     796        } 
     797 
     798        /** 
     799         * Creates the DB connection. 
     800         * @param string the module ID for TDataSourceConfig 
     801         * @return TDbConnection the created DB connection 
     802         * @throws TConfigurationException if module ID is invalid or empty 
     803         */ 
     804        protected function createDbConnection() 
     805        { 
     806                if($this->_connID!=='') 
     807                { 
     808                        $config=$this->getApplication()->getModule($this->_connID); 
     809                        if($config instanceof TDataSourceConfig) 
     810                                return $config->getDbConnection(); 
     811                        else 
     812                                throw new TConfigurationException('dblogroute_connectionid_invalid',$this->_connID); 
     813                } 
     814                else 
     815                { 
     816                        $db=new TDbConnection; 
     817                        // default to SQLite3 database 
     818                        $dbFile=$this->getApplication()->getRuntimePath().'/sqlite3.log'; 
     819                        $db->setConnectionString('sqlite:'.$dbFile); 
     820                        return $db; 
     821                } 
     822        } 
     823 
     824        /** 
     825         * @return TDbConnection the DB connection instance 
     826         */ 
     827        public function getDbConnection() 
     828        { 
     829                if($this->_db===null) 
     830                        $this->_db=$this->createDbConnection(); 
     831                return $this->_db; 
     832        } 
     833 
     834        /** 
     835         * @return string the ID of a {@link TDataSourceConfig} module. Defaults to empty string, meaning not set. 
     836         */ 
     837        public function getConnectionID() 
     838        { 
     839                return $this->_connID; 
     840        } 
     841 
     842        /** 
     843         * Sets the ID of a TDataSourceConfig module. 
     844         * The datasource module will be used to establish the DB connection for this log route. 
     845         * @param string ID of the {@link TDataSourceConfig} module 
     846         */ 
     847        public function setConnectionID($value) 
     848        { 
     849                $this->_connID=$value; 
     850        } 
     851 
     852        /** 
     853         * @return string the name of the DB table to store log content. Defaults to 'pradolog'. 
     854         * @see setAutoCreateLogTable 
     855         */ 
     856        public function getLogTableName() 
     857        { 
     858                return $this->_logTable; 
     859        } 
     860 
     861        /** 
     862         * Sets the name of the DB table to store log content. 
     863         * Note, if {@link setAutoCreateLogTable AutoCreateLogTable} is false 
     864         * and you want to create the DB table manually by yourself, 
     865         * you need to make sure the DB table is of the following structure: 
     866         * (key CHAR(128) PRIMARY KEY, value BLOB, expire INT) 
     867         * @param string the name of the DB table to store log content 
     868         * @see setAutoCreateLogTable 
     869         */ 
     870        public function setLogTableName($value) 
     871        { 
     872                $this->_logTable=$value; 
     873        } 
     874 
     875        /** 
     876         * @return boolean whether the log DB table should be automatically created if not exists. Defaults to true. 
     877         * @see setAutoCreateLogTable 
     878         */ 
     879        public function getAutoCreateLogTable() 
     880        { 
     881                return $this->_autoCreate; 
     882        } 
     883 
     884        /** 
     885         * @param boolean whether the log DB table should be automatically created if not exists. 
     886         * @see setLogTableName 
     887         */ 
     888        public function setAutoCreateLogTable($value) 
     889        { 
     890                $this->_autoCreate=TPropertyValue::ensureBoolean($value); 
     891        } 
     892 
     893} 
     894 
    678895?>