summaryrefslogtreecommitdiff
path: root/framework/Log/EventLog/writers/writer_database.php
blob: 6908a770b900e98143d7f31f8c57e032cdfc9228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
 * File containing the ezcLogWriterDatabase class.
 *
 * @package EventLog
 * @version //autogentag//
 * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */

/**
 * Writes log messages to the database.
 *
 * @package EventLog
 * @version //autogentag//
 */
class ezcLogWriterDatabase implements ezcLogWriter
{
    private $db = null;

    private $properties = array();
    private $defaultColumns = array();
    private $additionalColumns = array();

    private $map;
    private $defaultTable = false;

    /**
     * Construct a new database log-writer.
     *
     * You can set the default table to write to with the $defaultTable parameter.
     * If $databaseInstance is given, that instance will be used for writing. If it
     * is ommitted the default database instance will be retrieved.
     *
     * This constructor is a tie-in.
     *
     * @param string $defaultTable
     * @param ezcDbHandler $databaseInstance
     *
     */
    public function __construct( ezcDbHandler $databaseInstance, $defaultTable = false )
    {
        $this->db = $databaseInstance;

        $this->map = new ezcLogMap();
        $this->defaultTable = $defaultTable;

        $this->message = "message";
        $this->datetime = "time";
        $this->severity = "severity";
        $this->source = "source";
        $this->category = "category";
    }

    /**
     * Sets the property $name to $value.
     *
     * @throws ezcBasePropertyNotFoundException if the property does not exist.
     * @param string $name
     * @param mixed $value
     * @return void
     */
    public function __set( $name, $value )
    {
        switch ( $name )
        {
            case 'table':    $this->properties['table'] = $value;    break;
            case 'message':
            case 'datetime':
            case 'severity':
            case 'source':
            case 'category': $this->defaultColumns[ $name ] = $value; break;
            default:         $this->additionalColumns[ $name ] = $value; break;
        }
    }

    /**
     * Returns the property $name.
     *
     * @throws ezcBasePropertyNotFoundException if the property does not exist.
     * @param string $name
     * @return mixed
     */
    public function __get( $name )
    {
        switch ( $name )
        {
            case 'table':    return $this->properties['table'];    break;
            case 'message':
            case 'datetime':
            case 'severity':
            case 'source':
            case 'category': return $this->defaultColumns[ $name ]; break;

            default:         return $this->additionalColumns[ $name ]; break;
        }
    }

    /**
     * Writes the message $message to the log.
     *
     * The writer can use the severity, source, and category to filter the
     * incoming messages and determine the location where the messages should
     * be written.
     *
     * $optional may contain extra information that can be added to the log. For example:
     * line numbers, file names, usernames, etc.
     *
     * @throws ezcLogWriterException when the log writer was unable to write
     *         the log message.
     * @param string $message
     * @param int $severity
	 *        ezcLog:: DEBUG, SUCCES_AUDIT, FAILED_AUDIT, INFO, NOTICE, WARNING, ERROR or FATAL.
     *
     * $param string $source
     * @param string $category
     * @param array(string=>string) $optional
     * @return void
     */
    public function writeLogMessage( $message, $severity, $source, $category, $optional = array() )
    {
        $severityName = ezcLog::translateSeverityName( $severity );

        $colStr = "";
        $valStr = "";

        if ( is_array( $optional ) )
        {
            foreach ( $optional as $key => $val )
            {
                $colStr .= ", " . ( isset( $this->additionalColumns[$key] ) ? $this->additionalColumns[$key] : $key );
                $valStr .= ", " . $this->db->quote( $val );
            }
        }

        $tables = $this->map->get( $severity, $source, $category );

        $query = $this->db->createSelectQuery();
        if ( count( $tables ) > 0)
        {
            foreach ( $tables as $t )
            {
                try
                {
                    $this->db->exec( "INSERT INTO `{$t}` ( {$this->message}, {$this->severity}, {$this->source}, {$this->category}, {$this->datetime} $colStr ) ".
                        "VALUES( ".$this->db->quote( $message ).", ".$this->db->quote( $severityName ).", ".$this->db->quote( $source ).", ".
                        $this->db->quote( $category ).", ".$query->expr->now()." $valStr )" );
                }
                catch ( PDOException $e )
                {
                    throw new ezcLogWriterException( "SQL query failed in ezcLogWriterDatabase.\n". $e->getMessage() );
                }
            }
        }
        else
        {
            if ( $this->defaultTable !== false )
            {
                try
                {
                    $this->db->exec( "INSERT INTO `{$this->defaultTable}` ( {$this->message}, {$this->severity}, {$this->source}, {$this->category}, {$this->datetime} $colStr ) ".
                        "VALUES( ".$this->db->quote( $message ).", ".$this->db->quote( $severityName ).", ".$this->db->quote( $source ).", ".
                        $this->db->quote( $category ).", ".$query->expr->now()." $valStr )" );
                }
                catch ( PDOException $e )
                {
                    throw new ezcLogWriterException( "SQL query failed in ezcLogWriterDatabase.\n". $e->getMessage() );
                }
            }
        }
    }

    /**
     * Returns an array that describes the coupling between the logMessage
     * information and the columns in the database.
     *
     * @return array(string=>string)
     */
    public function getColumnTranslations()
    {
        return array_merge( $this->defaultColumns, $this->additionalColumns );
    }


	/**
	 * Maps the table $tableName to the messages specified by the {@link ezcLogFilter} $logFilter.
     *
     * Log messages that matches with the filter are written to the table $tableName. 
     * This method works the same as {@link ezclog::map()}.
     *
     * @param ezcLogFilter $logFilter 
     * @param string $tableName
     * @return void
	 */
	public function map( ezcLogFilter $logFilter, $tableName )
	{
        $this->map->map( $logFilter->severity, $logFilter->source, $logFilter->category, $tableName );
	}

	/**
	 * Unmaps the table $tableName from the messages specified by the {@link ezcLogFilter} $logFilter.
     *
     * Log messages that matches with the filter are no longer written to the table $tableName. 
     * This method works the same as {@link ezclog::unmap()}.
     *
     * @param ezcLogFilter $logFilter 
     * @param string $fileName
     * @return void
	 */
    public function unmap( ezcLogFilter $logFilter, $tableName )
    {
        $this->map->unmap( $logFilter->severity, $logFilter->source, $logFilter->category, $tableName );
    }
}

?>