Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/flex logger #42

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
9 changes: 6 additions & 3 deletions php/consola/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
$consola->run( $argv );

$dir_logs = toba_modelo_instalacion::dir_base()."/logs_comandos";
toba_logger::instancia()->set_directorio_logs($dir_logs);
toba_logger::instancia()->guardar_en_archivo('comandos.log');

$handler = toba_handler_log::instancia();
$handler->set_directorio_logs($dir_logs);
$handler->set_archivo_destino('comandos.log');

toba_logger::instancia()->set_logger_instance($handler);
toba_logger::instancia()->guardar();
} else {
echo " ATENCION: La variable de entorno 'toba_dir' no esta definida!";
}
Expand Down
7 changes: 5 additions & 2 deletions php/instalacion/toba_instalador.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ function set_progreso($progreso)
function grabar_logs()
{
$dir_logs = toba_modelo_instalacion::dir_base()."/logs_comandos";
toba_logger::instancia()->set_directorio_logs($dir_logs);
toba_logger::instancia()->guardar_en_archivo('comandos.log');
$handler = toba_handler_log::instancia();
$handler->set_directorio_logs($dir_logs);
$handler->set_archivo_destino('comandos.log');
toba_logger::instancia()->set_logger_instance($handler);
toba_logger::instancia()->guardar();
}

//--------------------------------------------------------------
Expand Down
138 changes: 138 additions & 0 deletions php/nucleo/lib/log_handlers/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
use Psr\Log\InvalidArgumentException;

class logger extends AbstractLogger
{
use \toba_basic_logger;

static protected $instancia;

protected $activo = true;
protected $proyecto_actual;
protected $id_solicitud;
protected $mapeo_niveles = array();

function __construct($proyecto)
{
$this->proyecto_actual = $proyecto;
$this->mapeo_niveles = array_flip($this->get_niveles());
}

static function instancia($proyecto=null)
{
if (is_null($proyecto)) {
$proyecto = \toba_basic_logger::get_proyecto_actual();
}

if (! isset(self::$instancia[$proyecto])) {
self::$instancia[$proyecto] = new logger($proyecto); //Hay que cambiarlo por logger y setearle el handler especifico para que guarde
}
return self::$instancia[$proyecto];
}

public function set_logger_instance($writer, $proyecto=null)
{
if (is_null($proyecto)) {
$this->write_handler = array($writer);
} else {
$this->write_handler[$proyecto] = $writer;
}
}

public function set_id_solicitud($solicitud)
{
$this->id_solicitud = $solicitud;
}

public function log($level, $message, array $context = array())
{
if (! $this->activo) { //Si no estoy logueando ni me gasto.
return;
}
// PSR-3 dice que el mensaje siempre debe ser un string
$mensaje = (is_object($message)) ? $message->__toString() : (string) $message;
/*if (strpos('{', $mensaje) !== false) { //Habria que parsear para ver si no existe algun replace en base al contexto.
//Hay que hacer el replace aca dentro del mensaje por ahora awanto
}*/

$nivel_toba = (is_int($level) && isset($this->ref_niveles[$level]));
if (! is_int($level)) {
$nivel_pedido = strtoupper($level);
} elseif ($nivel_toba) {
$nivel_pedido = $this->ref_niveles[$level];
}

$nivel_psr = (! is_int($level) && isset($this->mapeo_niveles[$nivel_pedido]));
if (($nivel_psr || $nivel_toba) && $this->mapeo_niveles[$nivel_pedido] <= $this->nivel_maximo) {
if (isset($context['exception']) && ($context['exception'] instanceof \Exception)) {
//Obtener la traza de la excepcion?.
}
$msg = $this->format_msg($mensaje, $nivel_pedido);
$this->escribir_msg($msg, $nivel_pedido);
}
}

public function guardar()
{
foreach($this->write_handler as $handler) {
if (method_exists($handler, 'guardar')) {
$handler->guardar();
}
}
}

protected function escribir_msg($mensaje, $nivel)
{
if (isset($this->write_handler[$this->proyecto_actual])) {
$this->write_handler[$this->proyecto_actual]->log($nivel, $mensaje);
} else { //Si no hay uno especifico para el proyecto lo zampo a todos x es gral.
foreach($this->write_handler as $log_writer) {
$log_writer->log($nivel, $mensaje);
}
}
}

protected function armar_mensaje($mensaje, $nivel)
{
return "[" . $this->id_solicitud . "][" .$this->proyecto_actual . "][" . $nivel ."] " . $mensaje . PHP_EOL;
}

protected function format_msg($mensaje, $level)
{
$level = strtolower($level);
switch ($level) {
case LogLevel::EMERGENCY:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_EMERGENCY);
break;
case LogLevel::ALERT:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_ALERT);
break;
case LogLevel::CRITICAL:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_CRIT);
break;
case LogLevel::ERROR:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_ERROR);
break;
case LogLevel::WARNING:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_WARNING);
break;
case LogLevel::NOTICE:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_NOTICE);
break;
case LogLevel::INFO:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_INFO);
break;
case LogLevel::DEBUG:
$msg = $this->armar_mensaje($mensaje, TOBA_LOG_DEBUG);
break;
default:
// Unknown level --> PSR-3 says kaboom
throw new InvalidArgumentException("Severidad del msg desconocida" );
}
return $msg;
}
}
?>