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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id$
*
* @package Piwik_Log
*/
Zend_Loader::loadClass('Zend_Log');
Zend_Loader::loadClass('Zend_Log_Formatter_Interface');
Zend_Loader::loadClass('Zend_Log_Writer_Stream');
Zend_Loader::loadClass('Zend_Log_Writer_Db');
Zend_Loader::loadClass('Piwik_Common');
/**
*
*
* @package Piwik_Log
*/
abstract class Piwik_Log extends Zend_Log
{
protected $logToDatabaseTableName = null;
protected $logToDatabaseColumnMapping = null;
protected $logToFileFilename = null;
protected $fileFormatter = null;
protected $screenFormatter = null;
function __construct( $logToFileFilename,
$fileFormatter,
$screenFormatter,
$logToDatabaseTableName,
$logToDatabaseColumnMapping )
{
parent::__construct();
$this->logToFileFilename = Zend_Registry::get('config')->path->log . $logToFileFilename;
$this->fileFormatter = $fileFormatter;
$this->screenFormatter = $screenFormatter;
$this->logToDatabaseTableName = Piwik::prefixTable($logToDatabaseTableName);
$this->logToDatabaseColumnMapping = $logToDatabaseColumnMapping;
}
static public function dump($var)
{
Zend_Registry::get('logger_message')->log(var_export($var, true), Piwik_Log::DEBUG);
}
function addWriteToFile()
{
$writerFile = new Zend_Log_Writer_Stream($this->logToFileFilename);
Piwik::mkdir(Zend_Registry::get('config')->path->log);
$writerFile->setFormatter( $this->fileFormatter );
$this->addWriter($writerFile);
}
function addWriteToNull()
{
Zend_Loader::loadClass('Zend_Log_Writer_Null');
$this->addWriter( new Zend_Log_Writer_Null );
}
function addWriteToDatabase()
{
$writerDb = new Zend_Log_Writer_Db(
Zend_Registry::get('db'),
$this->logToDatabaseTableName,
$this->logToDatabaseColumnMapping);
$this->addWriter($writerDb);
}
function addWriteToScreen()
{
$writerScreen = new Zend_Log_Writer_Stream('php://output');
$writerScreen->setFormatter( $this->screenFormatter );
$this->addWriter($writerScreen);
}
public function getWritersCount()
{
return count($this->_writers);
}
/**
* Log an event
* Overload Zend_log::log
*/
public function log($event)
{
// sanity checks
if (empty($this->_writers)) {
throw new Zend_Log_Exception('No writers were added');
}
$event['timestamp'] = date('c');
// pack into event required by filters and writers
$event = array_merge( $event, $this->_extras);
// abort if rejected by the global filters
foreach ($this->_filters as $filter) {
if (! $filter->accept($event)) {
return;
}
}
// send to each writer
foreach ($this->_writers as $writer) {
$writer->write($event);
}
}
}
/**
*
*
* @package Piwik_Log
*/
class Piwik_Log_Formatter_FileFormatter implements Zend_Log_Formatter_Interface
{
/**
* Formats data into a single line to be written by the writer.
*
* @param array $event event data
* @return string formatted line to write to the log
*/
public function format($event)
{
foreach($event as &$value)
{
$value = str_replace("\n", '\n', $value);
$value = '"'.$value.'"';
}
$str = implode(" ", $event) . "\n";
return $str;
}
}
|