Completed
Pull Request — master (#29)
by Tim
09:12 queued 05:43
created

CustomFileLogger::getLogFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\WebServer\Loggers\CustomFileLogger
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    Johann Zelger <[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      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Loggers;
22
23
use Psr\Log\LogLevel;
24
25
/**
26
 * Logger implementation that uses the PHP 'error_log' function to log to a custom file.
27
 *
28
 * @author    Johann Zelger <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/server
32
 * @link      http://www.appserver.io
33
 */
34
class CustomFileLogger extends DummyLogger
35
{
36
37
    /**
38
     * The file we want to log to.
39
     *
40
     * @var string
41
     */
42
    protected $logFile;
43
44
    /**
45
     * Initializes the logger instance with the log level.
46
     *
47
     * @param string  $channelName The channel name
48
     * @param array   $handlers    The array with the handlers
49
     * @param array   $processors  The array with the processors
50
     * @param integer $logLevel    The log level we want to use
51
     * @param string  $logFile     The file we want to log to
52
     */
53
    public function __construct($channelName, array $handlers = array(), array $processors = array(), $logLevel = LogLevel::INFO, $logFile = null)
54
    {
55
56
        // pass arguments to parent constructor
57
        parent::__construct($channelName, $handlers, $processors, $logLevel);
58
59
        // set the file we want to log to
60
        $this->logFile = $logFile;
61
    }
62
63
    /**
64
     * Returns the relative path to the file we want to log to.
65
     *
66
     * @return string The file we want to log to
67
     */
68
    protected function getLogFile()
69
    {
70
        return $this->logFile;
71
    }
72
73
    /**
74
     * Logs with an arbitrary level.
75
     *
76
     * @param mixed  $level   The log level
77
     * @param string $message The message to log
78
     * @param array  $context The context for log
79
     *
80
     * @return null
81
     */
82
    public function log($level, $message, array $context = array())
83
    {
84
85
        // check the log level
86
        if ($this->shouldLog($level)) {
87
            // prepare the log message
88
            $logMessage = sprintf('[%s] - %s (%s): %s %s' . PHP_EOL, date('Y-m-d H:i:s'), gethostname(), $level, $message, json_encode($context));
89
90
            // write the log message
91
            error_log($logMessage, 3, $this->getLogFile());
92
        }
93
    }
94
}
95