ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 }
__construct( $directory, $level=4)
Constructor.
Definition: Logger.php:68
info( $data)
Log info data.
Definition: Logger.php:132
write( $data)
Persist data to log.
Definition: Logger.php:196
log( $data, $level)
Log data to file.
Definition: Logger.php:178
warn( $data)
Log warn data.
Definition: Logger.php:141
getFile()
Get absolute path to current daily log file.
Definition: Logger.php:167
error( $data)
Log error data.
Definition: Logger.php:150
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
debug( $data)
Log debug data.
Definition: Logger.php:123
fatal( $data)
Log fatal data.
Definition: Logger.php:159
Create styles array
The data for the language used.
setLevel( $level)
Set log level.
Definition: Logger.php:101
setDirectory( $directory)
Set log directory.
Definition: Logger.php:78
getLevel()
Get log level.
Definition: Logger.php:114
getDirectory()
Get log directory.
Definition: Logger.php:91