-
Notifications
You must be signed in to change notification settings - Fork 56
/
ARedisLogRoute.php
executable file
·88 lines (82 loc) · 1.91 KB
/
ARedisLogRoute.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
<?php
/**
* A log route that allows log items to be stored or broadcast by redis.
* @author Charles Pick
* @package packages.redis
*/
class ARedisLogRoute extends CLogRoute {
/**
* The name of the redis key to use when storing logs
* @var string
*/
public $redisKey;
/**
* Whether to broadcast log messages via pub/sub instead of saving them
* @var boolean
*/
public $useChannel = false;
/**
* Holds the redis connection
* @var ARedisConnection
*/
protected $_connection;
/**
* Sets the redis connection to use for caching
* @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
*/
public function setConnection($connection)
{
if (is_string($connection)) {
$connection = Yii::app()->{$connection};
}
$this->_connection = $connection;
}
/**
* Gets the redis connection to use for caching
* @return ARedisConnection
*/
public function getConnection()
{
if ($this->_connection === null) {
if (!isset(Yii::app()->redis)) {
throw new CException(get_class($this)." expects a 'redis' application component");
}
$this->_connection = Yii::app()->redis;
}
return $this->_connection;
}
/**
* Stores or broadcasts log messages via redis.
* @param array $logs list of log messages
*/
protected function processLogs($logs)
{
$redis = $this->getConnection()->getClient();
if (function_exists("json_encode")) {
$useCJSON = false;
}
else {
$useCJSON = true;
}
foreach($logs as $log) {
$item = array(
"level" => $log[1],
"category" => $log[2],
"time" => $log[3],
"message" => $log[0],
);
if ($useCJSON) {
$json = CJSON::encode($item);
}
else {
$json = json_encode($item);
}
if ($this->useChannel) {
$redis->publish($this->redisKey, $json);
}
else {
$redis->zAdd($this->redisKey,$log[3],$json);
}
}
}
}