Skip to content

Instantly share code, notes, and snippets.

@donpandix
Created December 28, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpandix/e40d1009f19e45d16c3a to your computer and use it in GitHub Desktop.
Save donpandix/e40d1009f19e45d16c3a to your computer and use it in GitHub Desktop.
Funcion sencilla en PHP para el debug
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