Slimore
  • Namespace
  • Class

Namespaces

  • None
  • Slimore
    • Cache
      • Exception
    • Captcha
    • Database
    • Debug
    • Http
    • Image
    • Log
    • Middleware
    • Mvc
    • Pagination
    • Upload

Classes

  • Slimore\Cache\File
  • Slimore\Captcha\Builder
  • Slimore\Database\Manager
  • Slimore\Debug\Simpler
  • Slimore\Http\Client
  • Slimore\Image\Gd
  • Slimore\Log\Writer
  • Slimore\Middleware\Exceptions
  • Slimore\Mvc\Application
  • Slimore\Mvc\Controller
  • Slimore\Mvc\Model
  • Slimore\Mvc\View
  • Slimore\Pagination\Paginator
  • Slimore\Upload\Uploader

Exceptions

  • Slimore\Cache\Exception\File
  • Slimore\Captcha\Exception
  • Slimore\Database\Exception
  • Slimore\Http\Exception
  • Slimore\Mvc\Exception
  • Slimore\Pagination\Exception
  • Slimore\Upload\Exception

Functions

  • arrayToObject
  • console
  • controller
  • ctl
  • currentUrl
  • decrypt
  • detectDevice
  • encrypt
  • fileFormatSize
  • getDirectoryItems
  • getDpi
  • hexToRGB
  • html
  • imageDataUrl
  • iPad
  • iPhone
  • iPod
  • isAndroid
  • isApache
  • isBlackberry
  • isChrome
  • isCli
  • isFirefox
  • isFreeBSD
  • isIE
  • isIIS
  • isJson
  • isLinux
  • isMacOSX
  • isMobile
  • isNginx
  • isOpera
  • isSafari
  • isTablet
  • isUnix
  • isUnixLike
  • isWebOS
  • isWindows
  • js
  • jsonToArray
  • linkTag
  • password
  • phoneMaskCode
  • randomCharacters
  • replaceDirSeparator
  • rgbToHex
  • script
  • shortUrl
  • style
  1 <?php
  2 
  3 /**
  4  * Slimore - The fully (H)MVC framework based on the Slim PHP framework.
  5  *
  6  * @author      Pandao <slimore@ipandao.com>
  7  * @copyright   2015 Pandao
  8  * @link        http://github.com/slimore/slimore
  9  * @license     MIT License https://github.com/slimore/slimore#license
 10  * @version     0.1.0
 11  * @package     Slimore\Log
 12  */
 13 
 14 namespace Slimore\Log;
 15 
 16 /**
 17  * Class Writer
 18  *
 19  * @author Pandao
 20  * @package Slimore\Log
 21  */
 22 
 23 class Writer
 24 {
 25     /**
 26      * @var int
 27      */
 28     protected $level;
 29 
 30     /**
 31      * @var string
 32      */
 33     protected $message;
 34 
 35     /**
 36      * @var string
 37      */
 38     protected $filename;
 39 
 40     /**
 41      * @var resource
 42      */
 43     protected $resource;
 44 
 45     /**
 46      * @var string
 47      */
 48     protected $messageOutput;
 49 
 50     /**
 51      * @var string
 52      */
 53     protected $label = 'DEBUG';
 54 
 55     /**
 56      * @var string
 57      */
 58     protected $path = '../app/logs';
 59 
 60     /**
 61      * @var string
 62      */
 63 
 64     protected $dateFormat = 'Y-m-d';
 65 
 66     /**
 67      * @var string
 68      */
 69     protected $extension = 'log';
 70 
 71     /**
 72      * @var string
 73      */
 74     protected $messageFormat = '[%label%][#][%date%] %message%';
 75 
 76     /**
 77      * @var array
 78      */
 79     protected $messageFormatSearchs = ['%label%', '[#]', '%date%', '%message%'];
 80 
 81     /**
 82      * @var array
 83      */
 84     protected $messageFormatReplaces;
 85 
 86     /**
 87      * @var null|callable
 88      */
 89     public    $writeBeforeHandle = null;
 90 
 91     public    $customMessageFormatParser = false;
 92 
 93     /**
 94      * @var array
 95      */
 96     protected $levels = [
 97         1 => 'EMERGENCY',
 98         2 => 'ALERT',
 99         3 => 'CRITICAL',
100         4 => 'ERROR',
101         5 => 'WARNING',
102         6 => 'NOTICE',
103         7 => 'INFO',
104         8 => 'DEBUG'
105     ];
106 
107     /**
108      * Constructor
109      */
110 
111     public function __construct()
112     {
113     }
114 
115     /**
116      * Settings
117      *
118      * @param string|array $key null
119      * @param string $value null
120      * @return void
121      */
122 
123     public function set()
124     {
125         $args  = func_get_args();
126         $count = func_num_args();
127 
128         if ( is_array($args[0]) )
129         {
130             foreach ($args[0] as $key => $value)
131             {
132                 if ($key === 'path')
133                 {
134                     $this->setPath($value);
135                     continue;
136                 }
137 
138                 if (property_exists($this, $key))
139                 {
140                     $this->$key = $value;
141                 }
142             }
143         }
144         elseif ($count === 2)
145         {
146             if ($args[0] === 'path')
147             {
148                 $this->setPath($args[1]);
149                 return ;
150             }
151 
152             if (property_exists($this, $args[0]))
153             {
154                 $this[$args[0]] = $args[1];
155             }
156         }
157     }
158 
159     /**
160      * Setting logs path
161      *
162      * @param string $path
163      *
164      * @return void
165      */
166 
167     public function setPath($path)
168     {
169         if (!file_exists($path))
170         {
171             mkdir($path, 0777, true);
172         }
173 
174         $this->path = $path;
175     }
176 
177     /**
178      * Get log files saved path
179      *
180      * @return string
181      */
182 
183     public function getPath()
184     {
185         return $this->path;
186     }
187 
188     /**
189      * Get log level
190      *
191      * @return int
192      */
193 
194     public function getLevel()
195     {
196         return $this->level;
197     }
198 
199     /**
200      * Get log label
201      *
202      * @return string
203      */
204 
205     public function getLabel()
206     {
207         return $this->label;
208     }
209 
210     /**
211      * Get log message content
212      *
213      * @return mixed
214      */
215 
216     public function getMessage()
217     {
218         return $this->message;
219     }
220 
221     /**
222      * Get log message format
223      *
224      * @return string
225      */
226 
227     public function getMessageFormat()
228     {
229         return $this->messageFormat;
230     }
231 
232     /**
233      * Set log message format
234      *
235      * @param string $format
236      * @return void
237      */
238 
239     public function setMessageFormat($format)
240     {
241         $this->messageFormat = (string) $format;
242     }
243 
244     /**
245      * Set log message format searchs
246      *
247      * @param array $searchs
248      * @return void
249      */
250 
251     public function setMessageFormatSearchs(array $searchs)
252     {
253         $this->messageFormatSearchs = $searchs;
254     }
255 
256     /**
257      * Get log message format searchs
258      *
259      * @return array
260      * @return void
261      */
262 
263     public function getMessageFormatSearchs()
264     {
265         return $this->messageFormatSearchs;
266     }
267 
268     /**
269      * Default log message format replaces
270      *
271      * @return void
272      */
273 
274     protected function defaultMessageFormatReplaces()
275     {
276         $this->messageFormatReplaces = [
277             $this->label,
278             str_repeat(' ', 9 - strlen($this->label)),
279             date('Y-m-d H:i:s e O'),
280             $this->message
281         ];
282     }
283 
284     /**
285      * Set log message format replaces
286      *
287      * @param array $replaces
288      * @return void
289      */
290 
291     public function setMessageFormatReplaces(array $replaces)
292     {
293         $this->messageFormatReplaces = $replaces;
294     }
295 
296     /**
297      * Get log message format repalces
298      *
299      * @return mixed
300      */
301 
302     public function getMessageFormatReplaces()
303     {
304         return $this->messageFormatReplaces;
305     }
306 
307     /**
308      * Log message format parser
309      *
310      * @return void
311      */
312 
313     protected function messageFormatParser()
314     {
315         $this->label = $this->levels[$this->level];
316 
317         $this->messageOutput = str_replace(
318             $this->getMessageFormatSearchs(),
319             $this->getMessageFormatReplaces(),
320             $this->getMessageFormat()
321         );
322     }
323 
324     /**
325      * Handle for writing log before
326      *
327      * @param callable $callback
328      * @return void
329      */
330 
331     protected function writeBefore(callable $callback)
332     {
333         $callback($this);
334     }
335 
336     /**
337      * Write to log file
338      *
339      * @param   mixed $content
340      * @param   int   $level
341      * @return  void
342      */
343 
344     public function write($content, $level)
345     {
346         $this->message = (string) $content;
347         $this->level   = $level;
348 
349         if ( is_callable($this->writeBeforeHandle) )
350         {
351             $this->writeBefore($this->writeBeforeHandle);
352 
353             if (!$this->customMessageFormatParser)
354             {
355                 $this->defaultMessageFormatReplaces();
356             }
357         }
358         else
359         {
360             $this->defaultMessageFormatReplaces();
361         }
362 
363         $this->messageFormatParser();
364 
365         if ( !$this->resource )
366         {
367             $this->filename = date($this->dateFormat);
368 
369             if ( !empty($this->extension) )
370             {
371                 $this->filename .= '.' . $this->extension;
372             }
373 
374             $this->resource = fopen($this->path . DIRECTORY_SEPARATOR . $this->filename, 'a');
375         }
376 
377         fwrite($this->resource, $this->messageOutput . PHP_EOL);
378     }
379 }
Slimore API documentation generated by ApiGen