1: <?php
2:
3: namespace gregoryv\logger;
4:
5:
6: 7: 8: 9: 10: 11: 12: 13:
14: class Logger
15: {
16: private static $writer;
17: private $template;
18:
19: private static $SIEVE;
20: private $sieve;
21:
22: 23: 24: 25: 26: 27: 28:
29: function __construct($context='')
30: {
31: if(empty(self::$writer)) {
32: self::$writer = new SyslogWriter();
33: }
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: 47: 48: 49:
50: public static function setWriter(SeverityWriterInterface $writer)
51: {
52: self::$writer = $writer;
53: }
54:
55:
56: 57: 58: 59: 60: 61: 62: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: