-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedisTarget.php
49 lines (43 loc) · 1.2 KB
/
RedisTarget.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
<?php
/**
* @link http://github.com/JackyChan/yii2-redis-log
* @copyright Copyright (c) 2016 Jacky <[email protected]>
* @license BSD
*/
namespace jc\yii\redis;
use Yii;
use yii\di\Instance;
use yii\log\Target;
use yii\redis\Connection;
/**
* RedisTarget store log messages in a redis list.
*/
class RedisTarget extends Target
{
/**
* @var Connection|array|string the Redis connection object or a configuration array for creating the object, or the application component ID of the Redis connection.
*/
public $redis = 'redis';
/**
* @var string key of the Redis list to store log messages. Default to "log"
*/
public $key = 'log';
/**
* Initializes the RedisTarget component.
* This method will initialize the [[redis]] property to make sure it refers to a valid Redis connection.
* @throws InvalidConfigException if [[redis]] is invalid.
*/
public function init() {
parent::init();
$this->redis = Instance::ensure($this->redis, Connection::className());
}
/**
* Stores log messages to Redis.
*/
public function export() {
foreach ($this->messages as $message) {
$text = $this->formatMessage($message);
$this->redis->executeCommand('RPUSH', [$this->key, $text]);
}
}
}