CustomFileHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 51
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getLogFile() 0 4 1
A handle() 0 7 2
1
<?php
2
3
/**
4
 * AppserverIo\Logger\Handlers\CustomFileHandler
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      http://github.com/appserver-io/logger
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Logger\Handlers;
22
23
use Psr\Log\LogLevel;
24
use AppserverIo\Logger\LogMessageInterface;
25
26
/**
27
 * Logger implementation that uses the PHP error_log() function to log to a custom file.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      http://github.com/appserver-io/logger
33
 * @link      http://www.appserver.io
34
 */
35
class CustomFileHandler extends DummyHandler
36
{
37
38
    /**
39
     * The file we want to log to.
40
     *
41
     * @var string
42
     */
43
    protected $logFile;
44
45
    /**
46
     * Initializes the logger instance with the log level.
47
     *
48
     * @param string  $logFile  The file we want to log to
49
     * @param integer $logLevel The log level we want to use
50
     */
51 8
    public function __construct($logFile, $logLevel = LogLevel::INFO)
52
    {
53
54
        // pass arguments to parent constructor
55 8
        parent::__construct($logLevel);
56
57
        // set the file we want to log to
58 8
        $this->logFile = $logFile;
59 8
    }
60
61
    /**
62
     * Returns the relative path to the file we want to log to.
63
     *
64
     * @return string The file we want to log to
65
     */
66 6
    protected function getLogFile()
67
    {
68 6
        return $this->logFile;
69
    }
70
71
    /**
72
     * Handles the log message.
73
     *
74
     * @param \AppserverIo\Logger\LogMessageInterface $logMessage The message to be handled
75
     *
76
     * @return void
77
     */
78 6
    public function handle(LogMessageInterface $logMessage)
79
    {
80 6
        if ($this->shouldLog($logMessage->getLevel())) {
81
            // check the log level
82 6
            error_log($this->getFormatter()->format($logMessage) . PHP_EOL, 3, $this->getLogFile());
83
        }
84 6
    }
85
}
86