SimpleLogger::isRotationNeeded()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Logger;
9
10
use Generics\Streams\FileOutputStream;
11
12
/**
13
 * This class is a standard reference implementation of the PSR LoggerInterface.
14
 *
15
 * @author Maik Greubel <[email protected]>
16
 */
17
class SimpleLogger extends BasicLogger
18
{
19
20
    /**
21
     * The log file path
22
     *
23
     * @var string
24
     */
25
    private $file = null;
26
27
    /**
28
     * The maximum log file size in megabyte
29
     *
30
     * @var int
31
     */
32
    private $maxLogSize;
33
34
    /**
35
     * Create a new SimpleLogger instance
36
     *
37
     * @param string $logFilePath
38
     *            The path to the log file.
39
     * @param int $maxLogSize
40
     *            The maximum log file size before it is rotated.
41
     */
42 16
    public function __construct($logFilePath = 'application.log', $maxLogSize = 2)
43
    {
44 16
        $this->file = $logFilePath;
45 16
        $this->maxLogSize = intval($maxLogSize);
46
        
47 16
        if ($this->maxLogSize < 1 || $this->maxLogSize > 50) {
48 1
            $this->maxLogSize = 2;
49
        }
50 16
    }
51
52
    /**
53
     * This function provides the real logging function.
54
     *
55
     * First the log file size is checked.
56
     * When the maximum size has reached, the file will be overwritten.
57
     * Otherwise the log string is appended.
58
     *
59
     * @param integer $level
60
     *            The arbitrary level
61
     * @param string $message
62
     *            The message to log
63
     * @param array $context
64
     *            The context of logging
65
     */
66 15
    protected function logImpl($level, $message, array $context = array())
67
    {
68 15
        if (! $this->levelHasReached($level)) {
69
            return;
70
        }
71
        
72 15
        if ($this->isRotationNeeded()) {
73 1
            unlink($this->file);
74
        }
75
        
76 15
        $ms = $this->getMessage($level, $message, $context);
77
        
78 14
        $fos = new FileOutputStream($this->file, true);
79 14
        $fos->write($ms);
80 14
        $fos->flush();
81 14
        $fos->close();
82 14
    }
83
84
    /**
85
     * Checks whether a rotation of log file is necessary
86
     *
87
     * @return boolean true in case of its necessary, false otherwise
88
     */
89 15
    private function isRotationNeeded()
90
    {
91 15
        clearstatcache();
92
        
93 15
        if (! file_exists($this->file)) {
94 15
            return false;
95
        }
96
        
97 1
        $result = false;
98
        
99 1
        $attributes = stat($this->file);
100
        
101 1
        if ($attributes == false || $attributes['size'] >= $this->maxLogSize * 1024 * 1024) {
102 1
            $result = true;
103
        }
104
        
105 1
        return $result;
106
    }
107
108
    /**
109
     * Retrieve the file name of logger
110
     *
111
     * @return string
112
     */
113 14
    public function getFile()
114
    {
115 14
        return $this->file;
116
    }
117
118
    /**
119
     * Retrieve the maximum size of log file in megabytes
120
     *
121
     * @return int
122
     */
123 1
    public function getMaxLogSize()
124
    {
125 1
        return $this->maxLogSize;
126
    }
127
}
128