source: branches/comu-ver2/data/module/log4php/php4/log4php/appenders/LoggerAppenderDb.php @ 18220

Revision 18220, 5.6 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
1<?php
2/**
3 * log4php is a PHP port of the log4j java logging package.
4 *
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
9 *
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
12 *
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
15 *
16 * @package log4php
17 * @subpackage appenders
18 */
19
20/**
21 * @ignore
22 */
23if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
24 
25require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
26require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
27require_once(LOG4PHP_DIR . '/LoggerLog.php');
28require_once('DB.php');
29
30/**
31 * Appends log events to a db table using PEAR::DB class.
32 *
33 * <p>This appender uses a table in a database to log events.</p>
34 * <p>Parameters are {@link $dsn}, {@link $createTable}, {@link table} and {@link $sql}.</p>
35 * <p>See examples in test directory.</p>
36 *
37 * @author VxR <vxr@vxr.it>
38 * @version $Revision: 1.7 $
39 * @package log4php
40 * @subpackage appenders
41 * @since 0.3
42 */
43class LoggerAppenderDb extends LoggerAppenderSkeleton {
44
45    /**
46     * Create the log table if it does not exists (optional).
47     * @var boolean
48     */
49    var $createTable = true;
50   
51    /**
52     * PEAR::Db Data source name. Read PEAR::Db for dsn syntax (mandatory).
53     * @var string
54     */
55    var $dsn;
56   
57    /**
58     * A {@link LoggerPatternLayout} string used to format a valid insert query (mandatory).
59     * @var string
60     */
61    var $sql;
62   
63    /**
64     * Table name to write events. Used only if {@link $createTable} is true.
65     * @var string
66     */   
67    var $table;
68   
69    /**
70     * @var object PEAR::Db instance
71     * @access private
72     */
73    var $db = null;
74   
75    /**
76     * @var boolean used to check if all conditions to append are true
77     * @access private
78     */
79    var $canAppend = true;
80   
81    /**   
82     * @access private
83     */
84    var $requiresLayout = false;
85   
86    /**
87     * Constructor.
88     *
89     * @param string $name appender name
90     */
91    function LoggerAppenderDb($name)
92    {
93        $this->LoggerAppenderSkeleton($name);
94    }
95
96    /**
97     * Setup db connection.
98     * Based on defined options, this method connects to db defined in {@link $dsn}
99     * and creates a {@link $table} table if {@link $createTable} is true.
100     * @return boolean true if all ok.
101     */
102    function activateOptions()
103    {
104        $this->db = DB::connect($this->dsn);
105
106        if (DB::isError($this->db)) {
107            LoggerLog::debug("LoggerAppenderDb::activateOptions() DB Connect Error [".$this->db->getMessage()."]");           
108            $this->db = null;
109            $this->closed = true;
110            $this->canAppend = false;
111
112        } else {
113       
114            $this->layout = LoggerLayout::factory('LoggerPatternLayout');
115            $this->layout->setConversionPattern($this->getSql());
116       
117            // test if log table exists
118            $tableInfo = $this->db->tableInfo($this->table, $mode = null);
119            if (DB::isError($tableInfo) and $this->getCreateTable()) {
120                $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) );";
121
122                LoggerLog::debug("LoggerAppenderDb::activateOptions() creating table '{$this->table}'... using sql='$query'");
123                         
124                $result = $this->db->query($query);
125                if (DB::isError($result)) {
126                    LoggerLog::debug("LoggerAppenderDb::activateOptions() error while creating '{$this->table}'. Error is ".$result->getMessage());
127                    $this->canAppend = false;
128                    return;
129                }
130            }
131            $this->canAppend = true;           
132        }
133
134    }
135   
136    function append($event)
137    {
138        if ($this->canAppend) {
139
140            $query = $this->layout->format($event);
141
142            LoggerLog::debug("LoggerAppenderDb::append() query='$query'");
143
144            $this->db->query($query);
145        }
146    }
147   
148    function close()
149    {
150        if ($this->db !== null)
151            $this->db->disconnect();
152        $this->closed = true;
153    }
154   
155    /**
156     * @return boolean
157     */
158    function getCreateTable()
159    {
160        return $this->createTable;
161    }
162   
163    /**
164     * @return string the defined dsn
165     */
166    function getDsn()
167    {
168        return $this->dsn;
169    }
170   
171    /**
172     * @return string the sql pattern string
173     */
174    function getSql()
175    {
176        return $this->sql;
177    }
178   
179    /**
180     * @return string the table name to create
181     */
182    function getTable()
183    {
184        return $this->table;
185    }
186   
187    function setCreateTable($flag)
188    {
189        $this->createTable = LoggerOptionConverter::toBoolean($flag, true);
190    }
191   
192    function setDsn($newDsn)
193    {
194        $this->dsn = $newDsn;
195    }
196   
197    function setSql($sql)
198    {
199        $this->sql = $sql;   
200    }
201   
202    function setTable($table)
203    {
204        $this->table = $table;
205    }
206   
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.