Passed
Push — master ( 48b9a1...94ae61 )
by Timo
20:07
created

SolrLogManager::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
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
namespace ApacheSolrForTypo3\Solr\System\Logging;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 - Thomas Hohn <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Util;
29
use TYPO3\CMS\Core\Log\LogLevel;
30
use TYPO3\CMS\Core\Log\LogManager;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Wrapper to for the TYPO3 Logging Framework
35
 *
36
 * @author Thomas Hohn <[email protected]>
37
 */
38
class SolrLogManager
39
{
40
    const WARNING = LogLevel::WARNING;
41
    const ERROR = LogLevel::ERROR;
42
    const INFO = LogLevel::INFO;
43
    const NOTICE = LogLevel::NOTICE;
44
45
    /**
46
     * @var \TYPO3\CMS\Core\Log\Logger
47
     */
48
    protected $logger = null;
49
50
    /**
51
     * @var DebugWriter
52
     */
53
    protected $debugWriter = null;
54
55
    /**
56
     * @var string
57
     */
58
    protected $className = '';
59
60
    /**
61
     * SolrLogManager constructor.
62
     *
63
     * @param string $className
64
     * @param DebugWriter $debugWriter
65
     */
66 165
    public function __construct($className, DebugWriter $debugWriter = null)
67
    {
68 165
        $this->className = $className;
69 165
        $this->debugWriter = isset($debugWriter) ? $debugWriter : GeneralUtility::makeInstance(DebugWriter::class);
70 165
    }
71
72
    /**
73
     * @return \TYPO3\CMS\Core\Log\Logger
74
     */
75 66
    protected function getLogger()
76
    {
77 66
        if ($this->logger === null) {
78 66
            $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger($this->className);
79
        }
80
81 66
        return $this->logger;
82
    }
83
84
    /**
85
     * Adds an entry to the LogManager
86
     *
87
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
88
     * @param string $message Log message.
89
     * @param array $data Additional data to log
90
     *
91
     * @return mixed
92
     */
93 66
    public function log($level, $message, array $data = [])
94
    {
95 66
        $this->getLogger()->log($level, $message, $data);
96 66
        $this->debugWriter->write($level, $message, $data);
97 66
    }
98
}
99