forked from canni/YiiMongoDbSuite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEMongoLogRoute.php
164 lines (154 loc) · 3.86 KB
/
EMongoLogRoute.php
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
<?php
/**
* @author Ianaré Sévi (merge into EMongoDB)
* @author aoyagikouhei (original author)
* @license New BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
*/
/**
* EMongoLogRoute
*
* Example, in config/main.php:
* 'log'=>array(
* 'class' => 'CLogRouter',
* 'routes' => array(
* array(
* 'class'=>'ext.EMongoDbLogRoute',
* 'levels'=>'trace, info, error, warning',
* 'categories' => 'system.*',
* 'collectionName' => 'yiilog',
* ),
* ),
* ),
*
* Options:
* connectionID : mongo component name : default mongodb
* collectionName : collaction name : default yiilog
* message : message column name : default message
* level : level column name : default level
* category : category column name : default category
* timestamp : timestamp column name : default timestamp
* timestampType : float or date : default float
* fsync : fsync flag : defalut false
* safe : safe flag : defalut false
*
*/
/**
* EMongoLogRoute routes log messages to MongoDB.
*/
class EMongoLogRoute extends CLogRoute
{
/**
* @var string Mongo DB component.
*/
public $connectionID = 'mongodb';
/**
* @var string Collection name.
*/
public $collectionName = 'yiilogs';
/**
* @var string timestamp type name: 'float', 'date', 'string'
*/
public $timestampType = 'float';
/**
* @var string message column name
*/
public $message = 'message';
/**
* @var string level column name
*/
public $level = 'level';
/**
* @var string category column name
*/
public $category = 'category';
/**
* @var string timestamp column name
*/
public $timestamp = 'timestamp';
/**
* @var boolean Force the update to be synced to disk before returning success.
*/
public $fsync = false;
/**
* @var boolean The program will wait for the database response.
*/
public $safe = false;
/**
* @var array Insert options.
*/
protected $_options;
/**
* @var MongoCollection Collection object used.
*/
protected $_collection;
/**
* Returns current MongoCollection object.
* @return MongoCollection
*/
protected function setCollection($collectionName)
{
if (!isset($this->_collection))
{
$db = Yii::app()->getComponent($this->connectionID);
if (!($db instanceof EMongoDB))
throw new EMongoException('EMongoHttpSession.connectionID is invalid');
$this->_collection = $db->getDbInstance()->selectCollection($collectionName);
}
return $this->_collection;
}
/**
* Initialize the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
parent::init();
$this->setCollection($this->collectionName);
$this->_options = array(
'fsync' => $this->fsync,
'safe' => $this->safe
);
}
/**
* Return the formatted timestamp.
* @param float $timestamp Timestamp as given by log function.
* @return mixed
*/
protected function formatTimestamp($timestamp)
{
if ($this->timestampType === 'date')
$timestamp = new MongoDate($timestamp);
else if ($this->timestampType === 'string')
{
list($seconds, $microseconds) = explode('.', $timestamp);
$timestamp = date('Y-m-d H:i:s.', $seconds) . $microseconds;
}
return $timestamp;
}
/**
* Processes log messages and sends them to specific destination.
* @param array $logs list of messages. Each array elements represents one message
* with the following structure:
* array(
* [0] => message (string)
* [1] => level (string)
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true));
*/
protected function processLogs($logs)
{
foreach ($logs as $log)
{
$this->_collection->insert(array(
$this->message => $log[0],
$this->level => $log[1],
$this->category => $log[2],
$this->timestamp => $this->formatTimestamp($log[3]),
), $this->_options
);
}
}
}