Logger para ver Nosotros
This commit is contained in:
67
provm/common/Implementation/Logger.php
Normal file
67
provm/common/Implementation/Logger.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Implementation;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class Logger implements LoggerInterface {
|
||||
protected $output_file;
|
||||
public function __construct(string $filename) {
|
||||
if (!file_exists($filename)) {
|
||||
$f = fopen($filename, 'c');
|
||||
fclose($f);
|
||||
}
|
||||
if (!file_exists($filename)) {
|
||||
throw new \Exception('Can not create log file.');
|
||||
}
|
||||
$this->output_file = $filename;
|
||||
}
|
||||
public function emergency($message, array $context = []) {
|
||||
return $this->log(LogLevel::EMERGENCY, $message, $context);
|
||||
}
|
||||
public function alert($message, array $context = []) {
|
||||
return $this->log(LogLevel::ALERT, $message, $context);
|
||||
}
|
||||
public function critical($message, array $context = []) {
|
||||
return $this->log(LogLevel::CRITICAL, $message, $context);
|
||||
}
|
||||
public function error($message, array $context = []) {
|
||||
return $this->log(LogLevel::ERROR, $message, $context);
|
||||
}
|
||||
public function warning($message, array $context = []) {
|
||||
return $this->log(LogLevel::WARNING, $message, $context);
|
||||
}
|
||||
public function notice($message, array $context = []) {
|
||||
return $this->log(LogLevel::NOTICE, $message, $context);
|
||||
}
|
||||
public function info($message, array $context = []) {
|
||||
return $this->log(LogLevel::INFO, $message, $context);
|
||||
}
|
||||
public function debug($message, array $context = []) {
|
||||
return $this->log(LogLevel::DEBUG, $message, $context);
|
||||
}
|
||||
public function log($level, $message, array $context = []) {
|
||||
$refl = new \ReflectionClass(LogLevel::class);
|
||||
$levels = $refl->getConstants();
|
||||
if (array_search($level, array_values($levels)) === false) {
|
||||
throw new \InvalidArgumentException('Invalid log level.');
|
||||
}
|
||||
|
||||
$f = Carbon::now('America/Santiago');
|
||||
$output = [
|
||||
'time' => $f->format('Y-m-d H:i:s,v'),
|
||||
'message' => $message
|
||||
];
|
||||
if (count($context) > 0) {
|
||||
$output['context'] = $context;
|
||||
}
|
||||
|
||||
$data = json_decode(trim(file_get_contents($this->output_file)));
|
||||
if ($data === null) {
|
||||
$data = [];
|
||||
}
|
||||
$data []= $output;
|
||||
file_put_contents($this->output_file, json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user