Created
December 28, 2015 14:45
-
-
Save donpandix/e40d1009f19e45d16c3a to your computer and use it in GitHub Desktop.
Funcion sencilla en PHP para el debug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Debuging { | |
private $filePath; #Ruta del archivo log | |
private $fileName; #Nombre del archivo que invoca el debug | |
/** | |
* Clase de debugeo | |
* @param string $fileName nombre del archivo que invoca la clase | |
* @param string $filePath nombre del archivo donde se almacenará la traza | |
*/ | |
function __construct( $fileName = '', $filePath = '' ) { | |
if ( trim($filePath) == '' ) | |
$this->filePath = dirname(__FILE__) . '/log_' . date('Ymd') . '.log'; #por defecto crea una traza diaria | |
$this->fileName = $fileName; | |
$this->log(str_pad(strtoupper($fileName), 120,'_', STR_PAD_BOTH)); | |
} | |
/** | |
* Escribe al linea de LOG | |
* @param unknown $data Cualquier tipo de dato a imprimir, si no es cadena lo transformará a formato JSON | |
* @param string $additional linea adicional de texto | |
*/ | |
public function log ($data, $additional = '') { | |
if (!is_scalar($data) ) | |
$data = json_encode ($data); | |
$data = basename($this->fileName) . (($additional != '') ? ' [linea: ' . $additional . '] ' : '') . ' » ' . $data; | |
$myfile = fopen( $this->filePath, "a") or die("Unable to open file!"); | |
fwrite($myfile, date('Y-m-d h:i:s') . ' » ' . $data . "\n"); | |
fclose($myfile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment