FluentdTarget::createTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * @link      https://github.com/bilberrry/yii2-fluentd
4
 * @copyright Copyright (c) 2016 Alex Mukho <[email protected]>
5
 * @license   https://github.com/bilberrry/yii2-fluentd/blob/master/LICENSE
6
 */
7
8
namespace bilberrry\log;
9
10
use Fluent\Logger\FluentLogger;
11
use yii\log\Logger;
12
13
14
/**
15
 * FluentdTarget sends log messages to Fluentd
16
 */
17
class FluentdTarget extends \yii\log\Target
18
{
19
20
    /**
21
     * @var string Fluentd host
22
     */
23
    public $host = 'localhost';
24
    /**
25
     * @var int Fluentd port
26
     */
27
    public $port = 24224;
28
    /**
29
     * @var array Options for Fluentd client
30
     */
31
    public $options = [];
32
33
    /**
34
     * @var string Tag format, available placeholders: %date, %timestamp, %level
35
     */
36
    public $tagFormat = 'app.%level';
37
38
    /**
39
     * @var FluentLogger Fluentd client instance
40
     */
41
    private $_logger;
42
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function init()
48
    {
49
        parent::init();
50
51
        $this->_logger = FluentLogger::open($this->host, $this->port, $this->options);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function export()
58
    {
59
        $messages = array_map([$this, 'formatMessage'], $this->messages);
60
61
        $this->_logger->post($this->createTag($this->messages), $messages);
62
    }
63
64
65
    /**
66
     * Generating tag name based on $tagFormat
67
     *
68
     * @param $messages
69
     * @return string
70
     */
71
    private function createTag($messages)
72
    {
73
        return str_replace(['%date', '%timestamp', '%level'], [
74
            date('Y-m-d H:i:s'), time(), Logger::getLevelName($messages[0][1])
75
        ], $this->tagFormat);
76
    }
77
}