Completed
Push — master ( 8f9bf0...f8db25 )
by Marc
02:11
created

SimpleFile::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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
16
/**
17
 * Class SimpleFile
18
 *
19
 * @package PhpFlo\Logger
20
 * @author Marc Aschmann <[email protected]>
21
 */
22
class SimpleFile extends AbstractLogger implements LoggerInterface
23
{
24
    const DEFAULT_FILENAME = 'flow.log';
25
26
    /**
27
     * @var string
28
     */
29
    private $logFile;
30
31
    /**
32
     * @param string $logFile path/filename to log to.
33
     */
34 3
    public function __construct($logFile)
35
    {
36 3
        $log = pathinfo($logFile);
37
        // check if $logFile is a dir because of possible stream url
38 3
        $isLogdir = is_dir($logFile);
39 3
        if (!$isLogdir && (isset($log['dirname']) && !is_dir($log['dirname']))) {
40 1
            throw new \InvalidArgumentException(
41 1
                "Directory does not exist: {$log['dirname']}"
42 1
            );
43
        }
44
45 2
        if ($isLogdir || !isset($log['filename'])) {
46 1
            $logFile = $logFile . DIRECTORY_SEPARATOR . self::DEFAULT_FILENAME;
47 1
        }
48 2
        $this->logFile = $logFile;
49 2
    }
50
51
    /**
52
     * Logs with an arbitrary level.
53
     *
54
     * @param mixed $level
55
     * @param string $message
56
     * @param array $context
57
     *
58
     * @return void
59
     */
60 2
    public function log($level, $message, array $context = array())
61
    {
62 2
        file_put_contents(
63 2
            $this->logFile, $message . PHP_EOL, FILE_APPEND
64 2
        );
65 2
    }
66
}
67