Test Setup Failed
Push — master ( aa9039...94c501 )
by Derek
03:32
created

Logger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
namespace Subreality\Dilmun\Nabu;
4
5
use Psr\Log\AbstractLogger;
6
use \Psr\Log\InvalidArgumentException as arg_exception;
7
use Psr\Log\LoggerInterface;
8
use Subreality\Dilmun\Nabu\LoggerHandler\HandlerInterface;
9
use \Subreality\Dilmun\Nabu\StaticLogger as static_logger;
10
11
/**
12
 * PSR-3 logger implementation for logging RFC-5424 log-level messages
13
 *
14
 *  0   Emergency:      system is unusable
15
 *  1   Alert:          action must be taken immediately
16
 *  2   Critical:       critical conditions
17
 *  3   Error:          error conditions
18
 *  4   Warning:        warning conditions
19
 *  5   Notice:         normal but significant condition
20
 *  6   Informational:  informational messages
21
 *  7   Debug:          debug-level messages
22
 *
23
 * @see http://www.php-fig.org/psr/psr-3/
24
 * @see http://tools.ietf.org/html/rfc5424
25
 */
26
class Logger extends AbstractLogger implements LoggerInterface
27
{
28
29
    private $handler;
30
    private static $valid_levels = array(
31
        "EMERGENCY",
32
        "ALERT",
33
        "CRITICAL",
34
        "ERROR",
35
        "WARNING",
36
        "NOTICE",
37
        "INFO",
38
        "DEBUG"
39
    );
40
41
    /**
42
     * @param HandlerInterface $handler Sets the handler for logging (e.g. File handler).
43
     */
44
    public function __construct(HandlerInterface $handler)
45
    {
46
        $this->handler = $handler;
47
    }
48
49
    /**
50
     * @return HandlerInterface
51
     */
52
    public function getHandler()
53
    {
54
        return $this->handler;
55
    }
56
57
    /**
58
     * Logs with an arbitrary level.
59
     *
60
     * @see http://www.php-fig.org/psr/psr-3/
61
     *
62
     * @param mixed $level
63
     * @param string $message
64
     * @param array $context
65
     *
66
     * @return string           Returns the text of the log entry.
67
     */
68
    public function log($level, $message, array $context = array())
69
    {
70
        static_logger::setHandler($this->handler);
71
72
        $u_level = strtoupper($level);
73
74
        if (!in_array($u_level, self::$valid_levels)) {
75
            throw new arg_exception("Unknown log level");
76
        }
77
78
        $log_entry = static_logger::log($level, $message, $context);
79
80
        return $log_entry;
81
    }
82
}
83