1: <?php
2: /**
3: * Slim - a micro PHP 5 framework
4: *
5: * @author Josh Lockhart <info@slimframework.com>
6: * @copyright 2011 Josh Lockhart
7: * @link http://www.slimframework.com
8: * @license http://www.slimframework.com/license
9: * @version 2.2.0
10: * @package Slim
11: *
12: * MIT LICENSE
13: *
14: * Permission is hereby granted, free of charge, to any person obtaining
15: * a copy of this software and associated documentation files (the
16: * "Software"), to deal in the Software without restriction, including
17: * without limitation the rights to use, copy, modify, merge, publish,
18: * distribute, sublicense, and/or sell copies of the Software, and to
19: * permit persons to whom the Software is furnished to do so, subject to
20: * the following conditions:
21: *
22: * The above copyright notice and this permission notice shall be
23: * included in all copies or substantial portions of the Software.
24: *
25: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28: * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29: * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30: * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32: */
33: namespace Slim;
34:
35: /**
36: * Log
37: *
38: * This is the primary logger for a Slim application. You may provide
39: * a Log Writer in conjunction with this Log to write to various output
40: * destinations (e.g. a file). This class provides this interface:
41: *
42: * debug( mixed $object )
43: * info( mixed $object )
44: * warn( mixed $object )
45: * error( mixed $object )
46: * fatal( mixed $object )
47: *
48: * This class assumes only that your Log Writer has a public `write()` method
49: * that accepts any object as its one and only argument. The Log Writer
50: * class may write or send its argument anywhere: a file, STDERR,
51: * a remote web API, etc. The possibilities are endless.
52: *
53: * @package Slim
54: * @author Josh Lockhart
55: * @since 1.0.0
56: */
57: class Log
58: {
59: const FATAL = 0;
60: const ERROR = 1;
61: const WARN = 2;
62: const INFO = 3;
63: const DEBUG = 4;
64:
65: /**
66: * @var array
67: */
68: protected static $levels = array(
69: self::FATAL => 'FATAL',
70: self::ERROR => 'ERROR',
71: self::WARN => 'WARN',
72: self::INFO => 'INFO',
73: self::DEBUG => 'DEBUG'
74: );
75:
76: /**
77: * @var mixed
78: */
79: protected $writer;
80:
81: /**
82: * @var bool
83: */
84: protected $enabled;
85:
86: /**
87: * @var int
88: */
89: protected $level;
90:
91: /**
92: * Constructor
93: * @param mixed $writer
94: */
95: public function __construct($writer)
96: {
97: $this->writer = $writer;
98: $this->enabled = true;
99: $this->level = self::DEBUG;
100: }
101:
102: /**
103: * Is logging enabled?
104: * @return bool
105: */
106: public function getEnabled()
107: {
108: return $this->enabled;
109: }
110:
111: /**
112: * Enable or disable logging
113: * @param bool $enabled
114: */
115: public function setEnabled($enabled)
116: {
117: if ($enabled) {
118: $this->enabled = true;
119: } else {
120: $this->enabled = false;
121: }
122: }
123:
124: /**
125: * Set level
126: * @param int $level
127: * @throws \InvalidArgumentException If invalid log level specified
128: */
129: public function setLevel($level)
130: {
131: if (!isset(self::$levels[$level])) {
132: throw new \InvalidArgumentException('Invalid log level');
133: }
134: $this->level = $level;
135: }
136:
137: /**
138: * Get level
139: * @return int
140: */
141: public function getLevel()
142: {
143: return $this->level;
144: }
145:
146: /**
147: * Set writer
148: * @param mixed $writer
149: */
150: public function setWriter($writer)
151: {
152: $this->writer = $writer;
153: }
154:
155: /**
156: * Get writer
157: * @return mixed
158: */
159: public function getWriter()
160: {
161: return $this->writer;
162: }
163:
164: /**
165: * Is logging enabled?
166: * @return bool
167: */
168: public function isEnabled()
169: {
170: return $this->enabled;
171: }
172:
173: /**
174: * Log debug message
175: * @param mixed $object
176: * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
177: */
178: public function debug($object)
179: {
180: return $this->write($object, self::DEBUG);
181: }
182:
183: /**
184: * Log info message
185: * @param mixed $object
186: * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
187: */
188: public function info($object)
189: {
190: return $this->write($object, self::INFO);
191: }
192:
193: /**
194: * Log warn message
195: * @param mixed $object
196: * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
197: */
198: public function warn($object)
199: {
200: return $this->write($object, self::WARN);
201: }
202:
203: /**
204: * Log error message
205: * @param mixed $object
206: * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
207: */
208: public function error($object)
209: {
210: return $this->write($object, self::ERROR);
211: }
212:
213: /**
214: * Log fatal message
215: * @param mixed $object
216: * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
217: */
218: public function fatal($object)
219: {
220: return $this->write($object, self::FATAL);
221: }
222:
223: /**
224: * Log message
225: * @param mixed The object to log
226: * @param int The message level
227: * @return int|false
228: */
229: protected function write($object, $level)
230: {
231: if ($this->enabled && $this->writer && $level <= $this->level) {
232: return $this->writer->write($object, $level);
233: } else {
234: return false;
235: }
236: }
237: }
238: