[18220] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 4 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 5 | * this work for additional information regarding copyright ownership. |
---|
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 7 | * (the "License"); you may not use this file except in compliance with |
---|
| 8 | * the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | * |
---|
| 18 | * |
---|
| 19 | * @package log4php |
---|
| 20 | * @subpackage appenders |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * @ignore |
---|
| 25 | */ |
---|
| 26 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
| 27 | |
---|
| 28 | require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php'); |
---|
| 29 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php'); |
---|
| 30 | require_once(LOG4PHP_DIR . '/LoggerLog.php'); |
---|
| 31 | require_once('DB.php'); |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Appends log events to a db table using PEAR::DB class. |
---|
| 35 | * |
---|
| 36 | * <p>This appender uses a table in a database to log events.</p> |
---|
| 37 | * <p>Parameters are {@link $dsn}, {@link $createTable}, {@link table} and {@link $sql}.</p> |
---|
| 38 | * <p>See examples in test directory.</p> |
---|
| 39 | * |
---|
| 40 | * @author Marco Vassura |
---|
| 41 | * @version $Revision: 635069 $ |
---|
| 42 | * @package log4php |
---|
| 43 | * @subpackage appenders |
---|
| 44 | * @since 0.3 |
---|
| 45 | */ |
---|
| 46 | class LoggerAppenderDb extends LoggerAppenderSkeleton { |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Create the log table if it does not exists (optional). |
---|
| 50 | * @var boolean |
---|
| 51 | */ |
---|
| 52 | var $createTable = true; |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * PEAR::Db Data source name. Read PEAR::Db for dsn syntax (mandatory). |
---|
| 56 | * @var string |
---|
| 57 | */ |
---|
| 58 | var $dsn; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * A {@link LoggerPatternLayout} string used to format a valid insert query (mandatory). |
---|
| 62 | * @var string |
---|
| 63 | */ |
---|
| 64 | var $sql; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Table name to write events. Used only if {@link $createTable} is true. |
---|
| 68 | * @var string |
---|
| 69 | */ |
---|
| 70 | var $table; |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * @var object PEAR::Db instance |
---|
| 74 | * @access private |
---|
| 75 | */ |
---|
| 76 | var $db = null; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * @var boolean used to check if all conditions to append are true |
---|
| 80 | * @access private |
---|
| 81 | */ |
---|
| 82 | var $canAppend = true; |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * @access private |
---|
| 86 | */ |
---|
| 87 | var $requiresLayout = false; |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Constructor. |
---|
| 91 | * |
---|
| 92 | * @param string $name appender name |
---|
| 93 | */ |
---|
| 94 | function LoggerAppenderDb($name) |
---|
| 95 | { |
---|
| 96 | $this->LoggerAppenderSkeleton($name); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Setup db connection. |
---|
| 101 | * Based on defined options, this method connects to db defined in {@link $dsn} |
---|
| 102 | * and creates a {@link $table} table if {@link $createTable} is true. |
---|
| 103 | * @return boolean true if all ok. |
---|
| 104 | */ |
---|
| 105 | function activateOptions() |
---|
| 106 | { |
---|
| 107 | $this->db = DB::connect($this->dsn); |
---|
| 108 | |
---|
| 109 | if (DB::isError($this->db)) { |
---|
| 110 | LoggerLog::debug("LoggerAppenderDb::activateOptions() DB Connect Error [".$this->db->getMessage()."]"); |
---|
| 111 | $this->db = null; |
---|
| 112 | $this->closed = true; |
---|
| 113 | $this->canAppend = false; |
---|
| 114 | |
---|
| 115 | } else { |
---|
| 116 | |
---|
| 117 | $this->layout = LoggerLayout::factory('LoggerPatternLayout'); |
---|
| 118 | $this->layout->setConversionPattern($this->getSql()); |
---|
| 119 | |
---|
| 120 | // test if log table exists |
---|
| 121 | $tableInfo = $this->db->tableInfo($this->table, $mode = null); |
---|
| 122 | if (DB::isError($tableInfo) and $this->getCreateTable()) { |
---|
| 123 | $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) );"; |
---|
| 124 | |
---|
| 125 | LoggerLog::debug("LoggerAppenderDb::activateOptions() creating table '{$this->table}'... using sql='$query'"); |
---|
| 126 | |
---|
| 127 | $result = $this->db->query($query); |
---|
| 128 | if (DB::isError($result)) { |
---|
| 129 | LoggerLog::debug("LoggerAppenderDb::activateOptions() error while creating '{$this->table}'. Error is ".$result->getMessage()); |
---|
| 130 | $this->closed = true; |
---|
| 131 | $this->canAppend = false; |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | $this->canAppend = true; |
---|
| 136 | $this->closed = false; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | function append($event) |
---|
| 142 | { |
---|
| 143 | if ($this->canAppend) { |
---|
| 144 | |
---|
| 145 | $query = $this->layout->format($event); |
---|
| 146 | |
---|
| 147 | LoggerLog::debug("LoggerAppenderDb::append() query='$query'"); |
---|
| 148 | |
---|
| 149 | $this->db->query($query); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | function close() |
---|
| 154 | { |
---|
| 155 | if ($this->db !== null) |
---|
| 156 | $this->db->disconnect(); |
---|
| 157 | $this->closed = true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * @return boolean |
---|
| 162 | */ |
---|
| 163 | function getCreateTable() |
---|
| 164 | { |
---|
| 165 | return $this->createTable; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * @return string the defined dsn |
---|
| 170 | */ |
---|
| 171 | function getDsn() |
---|
| 172 | { |
---|
| 173 | return $this->dsn; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | * @return string the sql pattern string |
---|
| 178 | */ |
---|
| 179 | function getSql() |
---|
| 180 | { |
---|
| 181 | return $this->sql; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * @return string the table name to create |
---|
| 186 | */ |
---|
| 187 | function getTable() |
---|
| 188 | { |
---|
| 189 | return $this->table; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | function setCreateTable($flag) |
---|
| 193 | { |
---|
| 194 | $this->createTable = LoggerOptionConverter::toBoolean($flag, true); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | function setDsn($newDsn) |
---|
| 198 | { |
---|
| 199 | $this->dsn = $newDsn; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | function setSql($sql) |
---|
| 203 | { |
---|
| 204 | $this->sql = $sql; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | function setTable($table) |
---|
| 208 | { |
---|
| 209 | $this->table = $table; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|