source: tmp/version-2_5-test/data/module/log4php/php5/log4php/appenders/LoggerAppenderAdodb.php @ 18609

Revision 18609, 6.8 KB checked in by kajiwara, 14 years ago (diff)

正式版にナイトリービルド版をマージしてみるテスト

Line 
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 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27 
28require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
29require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
30require_once(LOG4PHP_DIR . '/LoggerLog.php');
31
32require_once(ADODB_DIR . '/adodb.inc.php');
33
34/**
35 * Appends log events to a db table using adodb class.
36 *
37 * <p>This appender uses a table in a database to log events.</p>
38 * <p>Parameters are {@link $host}, {@link $user}, {@link $password},
39 * {@link $database}, {@link $createTable}, {@link $table} and {@link $sql}.</p>
40 * <p>See examples in test directory.</p>
41 *
42 * @author sbw <sbw@ibiblio.org>
43 * @package log4php
44 * @subpackage appenders
45 * @since 0.9
46 */
47class LoggerAppenderAdodb extends LoggerAppenderSkeleton {
48
49    /**
50     * Create the log table if it does not exists (optional).
51     * @var boolean
52     */
53    var $createTable = true;
54   
55    /**
56     * The type of database to connect to
57     * @var string
58     */
59    var $type;
60   
61    /**
62     * Database user name
63     * @var string
64     */
65    var $user;
66   
67    /**
68     * Database password
69     * @var string
70     */
71    var $password;
72   
73    /**
74     * Database host to connect to
75     * @var string
76     */
77    var $host;
78   
79    /**
80     * Name of the database to connect to
81     * @var string
82     */
83    var $database;
84   
85    /**
86     * A {@link LoggerPatternLayout} string used to format a valid insert query (mandatory).
87     * @var string
88     */
89    var $sql;
90   
91    /**
92     * Table name to write events. Used only if {@link $createTable} is true.
93     * @var string
94     */   
95    var $table;
96   
97    /**
98     * @var object Adodb instance
99     * @access private
100     */
101    var $db = null;
102   
103    /**
104     * @var boolean used to check if all conditions to append are true
105     * @access private
106     */
107    var $canAppend = true;
108   
109    /**   
110     * @access private
111     */
112    var $requiresLayout = false;
113   
114    /**
115     * Constructor.
116     *
117     * @param string $name appender name
118     */
119    function LoggerAppenderDb($name)
120    {
121        $this->LoggerAppenderSkeleton($name);
122    }
123
124    /**
125     * Setup db connection.
126     * Based on defined options, this method connects to db defined in {@link $dsn}
127     * and creates a {@link $table} table if {@link $createTable} is true.
128     * @return boolean true if all ok.
129     */
130    function activateOptions()
131    {       
132        $this->db = &ADONewConnection($this->type);
133        if (! $this->db->PConnect($this->host, $this->user, $this->password, $this->database)) {
134          LoggerLog::debug("LoggerAppenderAdodb::activateOptions() DB Connect Error [".$this->db->ErrorMsg()."]");           
135          $this->db = null;
136          $this->closed = true;
137          $this->canAppend = false;
138          return;
139        }
140       
141        $this->layout = LoggerLayout::factory('LoggerPatternLayout');
142        $this->layout->setConversionPattern($this->getSql());
143   
144        // test if log table exists
145        $sql = 'select * from ' . $this->table . ' where 1 = 0';
146        $dbrs = $this->db->Execute($sql);
147        if ($dbrs == false and $this->getCreateTable()) {
148            $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) );";
149
150            LoggerLog::debug("LoggerAppenderAdodb::activateOptions() creating table '{$this->table}'... using sql='$query'");
151                     
152            $result = $this->db->Execute($query);
153            if (! $result) {
154                LoggerLog::debug("LoggerAppenderAdodb::activateOptions() error while creating '{$this->table}'. Error is ".$this->db->ErrorMsg());
155                $this->canAppend = false;
156                return;
157            }
158        }
159        $this->canAppend = true;
160    }
161   
162    function append($event)
163    {
164        if ($this->canAppend) {
165
166            $query = $this->layout->format($event);
167
168            LoggerLog::debug("LoggerAppenderAdodb::append() query='$query'");
169
170            $this->db->Execute($query);
171        }
172    }
173   
174    function close()
175    {
176        if ($this->db !== null)
177            $this->db->Close();
178        $this->closed = true;
179    }
180   
181    /**
182     * @return boolean
183     */
184    function getCreateTable()
185    {
186        return $this->createTable;
187    }
188   
189    /**
190     * @return string the sql pattern string
191     */
192    function getSql()
193    {
194        return $this->sql;
195    }
196   
197    /**
198     * @return string the table name to create
199     */
200    function getTable()
201    {
202        return $this->table;
203    }
204   
205    /**
206     * @return string the database to connect to
207     */
208    function getDatabase() {
209        return $this->database;
210    }
211   
212    /**
213     * @return string the database to connect to
214     */
215    function getHost() {
216        return $this->host;
217    }
218   
219    /**
220     * @return string the user to connect with
221     */
222    function getUser() {
223        return $this->user;
224    }
225   
226    /**
227     * @return string the password to connect with
228     */
229    function getPassword() {
230        return $this->password;
231    }
232   
233    /**
234     * @return string the type of database to connect to
235     */
236    function getType() {
237        return $this->type;
238    }
239   
240    function setCreateTable($flag)
241    {
242        $this->createTable = LoggerOptionConverter::toBoolean($flag, true);
243    }
244   
245    function setType($newType)
246    {
247        $this->type = $newType;
248    }
249   
250    function setDatabase($newDatabase)
251    {
252        $this->database = $newDatabase;
253    }
254   
255    function setHost($newHost)
256    {
257        $this->host = $newHost;
258    }
259   
260    function setUser($newUser)
261    {
262        $this->user = $newUser;
263    }
264   
265    function setPassword($newPassword)
266    {
267        $this->password = $newPassword;
268    }
269   
270    function setSql($sql)
271    {
272        $this->sql = $sql;   
273    }
274   
275    function setTable($table)
276    {
277        $this->table = $table;
278    }
279   
280}
281
Note: See TracBrowser for help on using the repository browser.