DefaultFormatter::format()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Logger
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Logger\Formatter;
13
14
use Phoole\Logger\Entry\LogEntryInterface;
15
16
/**
17
 * DefaultFormatter
18
 *
19
 * @package Phoole\Logger
20
 */
21
class DefaultFormatter implements FormatterInterface
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function format(LogEntryInterface $entry): string
27
    {
28
        $mesg = '';
29
30
        // channel name
31
        $context = $entry->getContext();
32
        if (isset($context['__channel'])) {
33
            $mesg .= '[' . strtoupper($context['__channel']) . '] ';
34
        }
35
36
        return $mesg . \strtoupper($entry->getLevel()) . ': ' . $entry . $this->getEol();
37
    }
38
39
    /**
40
     * Get EOL char base on the platform WIN or UNIX
41
     *
42
     * @return string
43
     */
44
    protected function getEol(): string
45
    {
46
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
47
            return "\r\n";
48
        } else {
49
            return "\n";
50
        }
51
    }
52
}