Completed
Push — master ( f8db25...f08840 )
by Marc
02:09
created

SimpleFile::prepareLogLevels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the phpflo/flowtrace package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpFlo\Logger;
12
13
use Psr\Log\AbstractLogger;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\LogLevel;
16
17
/**
18
 * Class SimpleFile
19
 *
20
 * @package PhpFlo\Logger
21
 * @author Marc Aschmann <[email protected]>
22
 */
23
class SimpleFile extends AbstractLogger implements LoggerInterface
24
{
25
    const DEFAULT_FILENAME = 'flow.log';
26
27
    /**
28
     * @var string
29
     */
30
    private $logFile;
31
32
    private $levels;
33
34
    /**
35
     * @param string $logFile path/filename to log to.
36
     * @param string $level PSR3 loglevel to log
37
     */
38 3
    public function __construct($logFile, $level = LogLevel::INFO)
39
    {
40 3
        $log = pathinfo($logFile);
41
        // check if $logFile is a dir because of possible stream url
42 3
        $isLogdir = is_dir($logFile);
43 3
        if (!$isLogdir && (isset($log['dirname']) && !is_dir($log['dirname']))) {
44 1
            throw new \InvalidArgumentException(
45 1
                "Directory does not exist: {$log['dirname']}"
46 1
            );
47
        }
48
49 2
        if ($isLogdir || !isset($log['filename'])) {
50 1
            $logFile = $logFile . DIRECTORY_SEPARATOR . self::DEFAULT_FILENAME;
51 1
        }
52 2
        $this->logFile = $logFile;
53
54 2
        $this->prepareLogLevels($level);
55 2
    }
56
57
    /**
58
     * Logs with an arbitrary level.
59
     *
60
     * @param mixed $level
61
     * @param string $message
62
     * @param array $context
63
     *
64
     * @return void
65
     */
66 2
    public function log($level, $message, array $context = array())
67
    {
68 2
        if (in_array($level, $this->levels)) {
69 2
            file_put_contents(
70 2
                $this->logFile, $message . PHP_EOL, FILE_APPEND
71 2
            );
72 2
        }
73 2
    }
74
75
    /**
76
     * Prepare loglevels for logfile
77
     *
78
     * @param string $level
79
     */
80 2
    private function prepareLogLevels($level)
81
    {
82
        $levels = [
83 2
            LogLevel::EMERGENCY,
84 2
            LogLevel::ALERT,
85 2
            LogLevel::CRITICAL,
86 2
            LogLevel::ERROR,
87 2
            LogLevel::WARNING,
88 2
            LogLevel::NOTICE,
89 2
            LogLevel::INFO,
90 2
            LogLevel::DEBUG,
91 2
        ];
92
93 2
        $key = array_search($level, $levels);
94
95 2
        if (null !== $key) {
96 2
            $this->levels = array_slice($levels, 0, $key + 1);
97 2
        }
98 2
    }
99
}
100