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: /**
  7: * Loggers are used to send formated messages to the writers using
  8: * specific log levels.
  9: *
 10: * Default template of the message is
 11: *
 12: *   context( INFO|WARNING|ERROR|DEBUG) message
 13: */
 14: class Logger
 15: {
 16:     private static $writer;
 17:     private $template;
 18:     // Holds severity state, global default for all loggers
 19:     private static $SIEVE;
 20:     private $sieve; // local to this logger
 21: 
 22:     /**
 23:      * Constructs a new logger where all messages are prefixed with
 24:      * the context.
 25:      * If the context is an object the class name of that object is used.
 26:      *
 27:      * @param mixed $context string or class
 28:      */
 29:     function __construct($context='')
 30:     {
 31:         if(empty(self::$writer)) {
 32:             self::$writer = new SyslogWriter(); // @codeCoverageIgnore
 33:         } // @codeCoverageIgnore
 34:         if(is_object($context)) {
 35:             $context = get_class($context);
 36:         }
 37:         if(empty(self::$SIEVE)) {
 38:             self::$SIEVE = new State();
 39:         }
 40:         $label = empty($context) ? '' : $context . ' ';
 41:         $this->template = $label . "%s %s";
 42:         $this->sieve = clone Logger::$SIEVE;
 43:     }
 44: 
 45:     /**
 46:      * Set global writer for all your loggers
 47:      *
 48:      * @param SeverityWriterInterface $writer
 49:      */
 50:     public static function setWriter(SeverityWriterInterface $writer)
 51:     {
 52:         self::$writer = $writer;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Sets logging state of the given severity level in a readable format
 58:      *
 59:      * @example $log->turn('on debug');
 60:      * @example $log->turn('off all warn');
 61:      *
 62:      * @param string $toggle format: (on|off) [all] (debug|info|notice|warn|error|critical|alert|emergency)
 63:      */
 64:     public function turn($toggle)
 65:     {
 66:         if(preg_match_all('/^(on|off) (all)?\s?(debug|info|notice|warn|error|critical|alert|emergency)$/', $toggle, $matches)) {
 67:             $flag = $matches[1][0];
 68:             $all = $matches[2][0];
 69:             $name = $matches[3][0];
 70:             $this->sieve->toggle($flag, $name);
 71:             if($all === 'all') {
 72:                 self::$SIEVE->toggle($flag, $name);
 73:             }
 74:         } else {
 75:             throw new \InvalidArgumentException("Invalid format: $toggle");
 76:         }
 77:     }
 78: 
 79:     /**
 80:      * Debug (severity 7): debug-level messages
 81:      */
 82:     public function debug($value='')
 83:     {
 84:         if($this->sieve->debug) {
 85:             self::$writer->swrite(LOG_DEBUG, sprintf($this->template, 'DEBUG', $value));
 86:         }
 87:     }
 88: 
 89:     /**
 90:      * Same as debug(sprintf($format, $args...))
 91:      */
 92:     public function debugf()
 93:     {
 94:         $args = func_get_args();
 95:         $format = array_shift($args);
 96:         $this->debug(vsprintf($format, $args));
 97:     }
 98: 
 99:     /**
100:      * Informational (severity 6): informational messages
101:      */
102:     public function info($value='')
103:     {
104:         if($this->sieve->info) {
105:             self::$writer->swrite(LOG_INFO, sprintf($this->template, 'INFO', $value));
106:         }
107:     }
108: 
109:     /**
110:      * Same as info(sprintf($format, $args...))
111:      */
112:     public function infof()
113:     {
114:         $args = func_get_args();
115:         $format = array_shift($args);
116:         $this->info(vsprintf($format, $args));
117:     }
118: 
119:     /**
120:      * Notice (severity 5): normal but significant condition
121:      */
122:     public function notice($value='')
123:     {
124:         if($this->sieve->notice) {
125:             self::$writer->swrite(LOG_NOTICE, sprintf($this->template, 'NOTICE', $value));
126:         }
127:     }
128: 
129:     /**
130:      * Same as notice(sprintf($format, $args...))
131:      */
132:     public function noticef()
133:     {
134:         $args = func_get_args();
135:         $format = array_shift($args);
136:         $this->notice(vsprintf($format, $args));
137:     }
138: 
139:     /**
140:      * Warning (severity 4): warning conditions
141:      */
142:     public function warn($value='')
143:     {
144:         if($this->sieve->warn) {
145:             self::$writer->swrite(LOG_WARNING, sprintf($this->template, 'WARNING', $value));
146:         }
147:     }
148: 
149:     /**
150:      * Same as warn(sprintf($format, $args...))
151:      */
152:     public function warnf()
153:     {
154:         $args = func_get_args();
155:         $format = array_shift($args);
156:         $this->warn(vsprintf($format, $args));
157:     }
158: 
159:     /**
160:      * Error (severity 3): error conditions
161:      */
162:     public function error($value='')
163:     {
164:         if($this->sieve->error) {
165:             self::$writer->swrite(LOG_ERR, sprintf($this->template, 'ERROR', $value));
166:         }
167:     }
168: 
169:     /**
170:      * Same as error(sprintf($format, $args...))
171:      */
172:     public function errorf()
173:     {
174:         $args = func_get_args();
175:         $format = array_shift($args);
176:         $this->error(vsprintf($format, $args));
177:     }
178: 
179:     /**
180:      * Critical (severity 2): critical conditions
181:      */
182:     public function critical($value='')
183:     {
184:         if($this->sieve->critical) {
185:             self::$writer->swrite(LOG_CRIT, sprintf($this->template, 'CRITICAL', $value));
186:         }
187:     }
188: 
189:     /**
190:      * Same as critical(sprintf($format, $args...))
191:      */
192:     public function criticalf()
193:     {
194:         $args = func_get_args();
195:         $format = array_shift($args);
196:         $this->critical(vsprintf($format, $args));
197:     }
198: 
199:     /**
200:      * Alert (severity 1): action must be taken immediately
201:      */
202:     public function alert($value='')
203:     {
204:         if($this->sieve->alert) {
205:             self::$writer->swrite(LOG_ALERT, sprintf($this->template, 'ALERT', $value));
206:         }
207:     }
208: 
209:     /**
210:      * Same as alert(sprintf($format, $args...))
211:      */
212:     public function alertf()
213:     {
214:         $args = func_get_args();
215:         $format = array_shift($args);
216:         $this->alert(vsprintf($format, $args));
217:     }
218: 
219:     /**
220:      * Emergency (severity 0): system is unusable
221:      */
222:     public function emergency($value='')
223:     {
224:         if($this->sieve->emergency) {
225:             self::$writer->swrite(LOG_EMERG, sprintf($this->template, 'EMERGENCY', $value));
226:         }
227:     }
228: 
229:     /**
230:      * Same as emergency(sprintf($format, $args...))
231:      */
232:     public function emergencyf()
233:     {
234:         $args = func_get_args();
235:         $format = array_shift($args);
236:         $this->emergency(vsprintf($format, $args));
237:     }
238: 
239: }
240: 
API documentation generated by ApiGen