| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Logger { |
|---|
| 4 | |
|---|
| 5 | private $name; |
|---|
| 6 | private $level; |
|---|
| 7 | private $appenders = array(); |
|---|
| 8 | private $starttime; |
|---|
| 9 | |
|---|
| 10 | public function __construct($name = '') { |
|---|
| 11 | $this->name = $name; |
|---|
| 12 | $this->level = LogLevel::$OFF; |
|---|
| 13 | $this->starttime = new DateTime; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | public function setLevel(LogLevel $level) { |
|---|
| 17 | $this->level = $level; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | public function getName(){ |
|---|
| 21 | return $this->name; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public function getLevel() { |
|---|
| 25 | return $this->level; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public function getStartTime() { |
|---|
| 29 | return $this->starttime; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | public function log(LogLevel $level, $format, array $params) { |
|---|
| 33 | if($level->toInt() <= $this->level->toInt()){ |
|---|
| 34 | return ; |
|---|
| 35 | } |
|---|
| 36 | $c = count($this->appenders); |
|---|
| 37 | if($c < 1){ |
|---|
| 38 | return ; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | $message = vsprintf($format, $params); |
|---|
| 42 | $event = new LoggingEvent($this, $level, $message); |
|---|
| 43 | for ($i = 0; $i < $c; ++$i) { |
|---|
| 44 | $this->appenders[$i]->append($event); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public function addAppender(Appender $appender) { |
|---|
| 49 | $this->appenders[] = $appender; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public function fatal(){ |
|---|
| 53 | $args = func_get_args(); |
|---|
| 54 | $this->log(LogLevel::$FATAL, array_shift($args), $args); |
|---|
| 55 | } |
|---|
| 56 | public function error(){ |
|---|
| 57 | $args = func_get_args(); |
|---|
| 58 | $this->log(LogLevel::$ERROR, array_shift($args), $args); |
|---|
| 59 | } |
|---|
| 60 | public function warn(){ |
|---|
| 61 | $args = func_get_args(); |
|---|
| 62 | $this->log(LogLevel::$WARN, array_shift($args), $args); |
|---|
| 63 | } |
|---|
| 64 | public function info(){ |
|---|
| 65 | $args = func_get_args(); |
|---|
| 66 | $this->log(LogLevel::$INFO, array_shift($args), $args); |
|---|
| 67 | } |
|---|
| 68 | public function debug(){ |
|---|
| 69 | $args = func_get_args(); |
|---|
| 70 | $this->log(LogLevel::$DEBUG, array_shift($args), $args); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | ?> |
|---|