Completed
Push — master ( da9a56...eb48c5 )
by Dmitry
03:52
created

Logger::sync_single_log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 *      This is a simple Logger implementation that other Loggers can inherit from.
5
 *
6
 *      @package php_debulog
7
 *      @version 1.0
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace Debulog;
13
14
abstract class Logger implements LoggerInterface {
15
16
    /**
17
     *      @var string
18
     */
19
    protected $dir;
20
21
    /**
22
     *      @var string
23
     */
24
    protected $prefix;
25
26
    /**
27
     *      @var boolean
28
     */
29
    protected $ondebug;
30
31
    /**
32
     *      @var array
33
     */
34
    protected $_messages = array();
35
36
    /**
37
     *      @var array
38
     */
39
    protected $_errors = array();
40
41
    /**
42
     *      @var array
43
     */
44
    protected $_debugs = array();
45
46
    /**
47
     *      Logger constructor
48
     *
49
     */
50
    public function __construct($dir, $prefix='my', $debug=false)
51
    {
52
        $this->dir = $dir;
53
        $this->prefix = $prefix;
54
        $this->ondebug = $debug;
55
56
        if ($this->ondebug === TRUE)
57
        {
58
            $this->_debugs[] = PHP_EOL . $this->log_debug_event('start');
59
        }
60
61
        register_shutdown_function(array($this, 'shutdown'));
62
    }
63
64
    /**
65
     *      Add message to logger buffer
66
     *
67
     *      @param string $message Message text
68
     *
69
     */
70
    public function add($message)
71
    {
72
        $this->_messages[] = $this->log_timestamp() . $message . PHP_EOL;
73
        $this->debug($message);
74
    }
75
76
    /**
77
     *      Add error message to logger buffer
78
     *
79
     *      @param string $message Message text
80
     *
81
     */
82
    public function error($message)
83
    {
84
        $this->_errors[] = $this->log_timestamp() . $message . PHP_EOL;
85
        $this->debug('ERROR: '.$message);
86
    }
87
88
    /**
89
     *      Add debug message to logger buffer
90
     *
91
     *      @param string $message Message to log
92
     *
93
     */
94
    public function debug($message)
95
    {
96
        if ($this->ondebug === TRUE)
97
        {
98
            $this->_debugs[] = $message . PHP_EOL;
99
        }
100
    }
101
102
    /**
103
     *      Finish and sync all buffers to files
104
     *
105
     */
106
    public function shutdown()
107
    {
108
        if ($this->ondebug === TRUE)
109
        {
110
            $this->_debugs[] = $this->log_debug_event('end') . PHP_EOL;
111
        }
112
        $this->sync();
113
    }
114
115
    /**
116
     *      Sync all buffers to files
117
     */
118
    public function sync()
119
    {
120
        $this->sync_single_log($this->_messages, '');
121
        $this->sync_single_log($this->_debugs, '_debug');
122
        $this->sync_single_log($this->_errors, '_error');
123
    }
124
125
    /**
126
     *      Sync specific buffer to file
127
     *
128
     *      @param array $buffer
129
     *      @param string $suffix log filename suffix
130
     */
131
    protected function sync_single_log(&$buffer, $suffix)
132
    {
133
        if ( ! empty($buffer))
134
        {
135
            $this->write($buffer, $this->dir . $this->prefix . $suffix . '.log');
136
            $buffer = array();
137
        }
138
    }
139
140
    /**
141
     *      Get formated string of debug event
142
     *
143
     *      @param string $event Name of event
144
     *      @return string
145
     */
146
    protected function log_debug_event($event)
147
    {
148
        return $event . ' of debugging at ' . $this->log_timestamp() . PHP_EOL;
149
    }
150
151
    /**
152
     *      Get formatted timestamp
153
     *
154
     *      @return string
155
     */
156
    protected function log_timestamp()
157
    {
158
        return strftime('%d.%m.%Y %H:%M:%S ');
159
    }
160
161
    /**
162
     *      Write text to file
163
     *
164
     *      @param array $messages
165
     *      @param string $file
166
     *
167
     *      @throws Exception
168
     *
169
     */
170
    protected function write($messages, $file)
171
    {
172
        $f = @fopen($file, 'a');
173
        if ($f === false)
174
        {
175
            throw new \Exception("Logfile $file is not writeable!");
176
        }
177
178
        foreach($messages as $msg)
179
        {
180
            fwrite($f, $msg);
181
        }
182
183
        fclose($f);
184
    }
185
}
186