Overview

Namespaces

  • gregoryv
    • logger

Classes

  • gregoryv\logger\CachedWriter
  • gregoryv\logger\ConsoleWriter
  • gregoryv\logger\FileWriter
  • gregoryv\logger\Logger
  • gregoryv\logger\State
  • gregoryv\logger\SyslogWriter

Interfaces

  • gregoryv\logger\SeverityWriterInterface
  • Overview
  • Namespace
  • Class
 1: <?php
 2: // Copyright (c) 2015 Gregory Vinčić, The MIT License (MIT)
 3: namespace gregoryv\logger;
 4: 
 5: /**
 6:  * FIFO cache of log messages
 7:  */
 8: class CachedWriter implements SeverityWriterInterface
 9: {
10:     /**
11:      * @var array $cache holding the messages
12:      */
13:     public $cache;
14: 
15:     private $messageLimit;
16: 
17:     /**
18:      * @param $messageLimit how many messages to keep in cache
19:      */
20:     public function __construct($messageLimit=20)
21:     {
22:         $this->cache = [];
23:         $this->messageLimit = $messageLimit;
24:     }
25: 
26:     /**
27:      * Stores messages in the public cache by priority
28:      */
29:     public function swrite($severity, $value='')
30:     {
31:         if(sizeof($this->cache) == $this->messageLimit) {
32:             array_shift($this->cache);
33:         }
34:         $this->cache[] = $value;
35:     }
36: 
37:     /**
38:      * @param int $limit
39:      */
40:     public function setLimit($limit)
41:     {
42:         if(!is_int($limit)) {
43:             throw new \InvalidArgumentException("limit must be a int");
44:         }
45:         if($limit < 1) {
46:             throw new \InvalidArgumentException("limit must be 1 or more");
47:         }
48:         $this->resizeCache($limit);
49:         $this->messageLimit = $limit;
50:     }
51: 
52:     public function resizeCache($limit)
53:     {
54:         while(sizeof($this->cache) != $limit) {
55:             array_shift($this->cache);
56:         }
57:     }
58: }
59: 
API documentation generated by ApiGen