LogAppender::append()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
crap 3
1
<?php
2
3
namespace PamiModule\Log;
4
5
use LoggerLevel;
6
use LoggerLoggingEvent;
7
use Zend\Log\Logger;
8
9
/**
10
 * Class LogAppender
11
 * This class allows use of Zend\Log.
12
 */
13
class LogAppender extends \LoggerAppender
14
{
15
    /**
16
     * ZF logger.
17
     *
18
     * @var Logger
19
     */
20
    protected $zendLog;
21
22
    /**
23
     * Tell that the log appender doesn't require a layout.
24
     *
25
     * @var bool
26
     */
27
    protected $requiresLayout = false;
28
29
    /**
30
     * Return the ZF logger.
31
     *
32
     * @return Logger
33
     */
34 6
    public function getZendLog()
35
    {
36 6
        return $this->zendLog;
37
    }
38
39
    /**
40
     * Set the ZF logger.
41
     *
42
     * @param Logger $zendLog Logger
43
     *
44
     * @return $this
45
     */
46 6
    public function setZendLog(Logger $zendLog)
47
    {
48 6
        $this->zendLog = $zendLog;
49
50 6
        return $this;
51
    }
52
53
    /**
54
     * Forwards the logging event to the destination.
55
     *
56
     * Derived appenders should implement this method to perform actual logging.
57
     *
58
     * @param LoggerLoggingEvent $event Logger event
59
     *
60
     * @throws \Zend\Log\Exception\InvalidArgumentException
61
     * @throws \Zend\Log\Exception\RuntimeException
62
     */
63 6
    public function append(LoggerLoggingEvent $event)
64
    {
65 6
        $level = $event->getLevel();
66
67 6
        $priority = Logger::DEBUG;
68
69
        $map = [
70 6
            LoggerLevel::FATAL => Logger::CRIT,
71 6
            LoggerLevel::ERROR => Logger::ERR,
72 6
            LoggerLevel::WARN => Logger::WARN,
73 6
            LoggerLevel::INFO => Logger::INFO,
74 6
            LoggerLevel::DEBUG => Logger::DEBUG,
75 6
            LoggerLevel::TRACE => Logger::DEBUG,
76 6
        ];
77
78 6
        foreach ($map as $value => $zendValue) {
79 6
            if ($level->toInt() >= $value) {
80 6
                $priority = $zendValue;
81 6
                break;
82
            }
83 6
        }
84
85 6
        $message = $event->getMessage();
86 6
        $this->getZendLog()->log($priority, $message);
87 6
    }
88
}
89