ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Logger.php
Go to the documentation of this file.
1 <?php
45 class Slim_Logger {
46 
50  protected $levels = array(
51  0 => 'FATAL',
52  1 => 'ERROR',
53  2 => 'WARN',
54  3 => 'INFO',
55  4 => 'DEBUG'
56  );
57 
61  protected $directory;
62 
68  public function __construct( $directory, $level = 4 ) {
69  $this->setDirectory($directory);
70  $this->setLevel($level);
71  }
72 
78  public function setDirectory( $directory ) {
79  $realPath = realpath($directory);
80  if ( $realPath ) {
81  $this->directory = rtrim($realPath, '/') . '/';
82  } else {
83  $this->directory = false;
84  }
85  }
86 
91  public function getDirectory() {
92  return $this->directory;
93  }
94 
101  public function setLevel( $level ) {
102  $theLevel = (int)$level;
103  if ( $theLevel >= 0 && $theLevel <= 4 ) {
104  $this->level = $theLevel;
105  } else {
106  throw new InvalidArgumentException('Invalid Log Level. Must be one of: 0, 1, 2, 3, 4.');
107  }
108  }
109 
114  public function getLevel() {
115  return $this->level;
116  }
117 
123  public function debug( $data ) {
124  $this->log($data, 4);
125  }
126 
132  public function info( $data ) {
133  $this->log($data, 3);
134  }
135 
141  public function warn( $data ) {
142  $this->log($data, 2);
143  }
144 
150  public function error( $data ) {
151  $this->log($data, 1);
152  }
153 
159  public function fatal( $data ) {
160  $this->log($data, 0);
161  }
162 
167  public function getFile() {
168  return $this->getDirectory() . strftime('%Y-%m-%d') . '.log';
169  }
170 
178  protected function log( $data, $level ) {
179  $dir = $this->getDirectory();
180  if ( $dir == false || !is_dir($dir) ) {
181  throw new RuntimeException("Log directory '$dir' invalid.");
182  }
183  if ( !is_writable($dir) ) {
184  throw new RuntimeException("Log directory '$dir' not writable.");
185  }
186  if ( $level <= $this->getLevel() ) {
187  $this->write(sprintf("[%s] %s - %s\r\n", $this->levels[$level], date('c'), (string)$data));
188  }
189  }
190 
196  protected function write( $data ) {
197  @file_put_contents($this->getFile(), $data, FILE_APPEND | LOCK_EX);
198  }
199 
200 }