Skip to content

Instantly share code, notes, and snippets.

@jaimerodas
Created October 9, 2012 21:38

Revisions

  1. jaimerodas revised this gist Oct 11, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions logger.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    <?
    //
    // Logger
    //

    class Log {
    protected $path;
    protected $format = 'Y-m-d H:i:s';
  2. jaimerodas created this gist Oct 9, 2012.
    52 changes: 52 additions & 0 deletions logger.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?
    //
    // Logger
    //
    class Log {
    protected $path;
    protected $format = 'Y-m-d H:i:s';
    protected $enabled = TRUE;

    public function __construct($p)
    {
    $this->path = $p.'/Logs/';

    if ( ! is_dir($this->path) OR ! is_writable($this->path)) {
    $this->enabled = FALSE;
    }
    }

    public function write($msg)
    {
    if ($this->enabled === FALSE) {
    return FALSE;
    }

    $filepath = $this->path.date('Y-m-d').'.txt';
    $message = '';

    echo $msg."\n";

    if ( ! file_exists($filepath)) {
    $newfile = TRUE;
    $message .= "Registro de ".date('Y-m-d')."\n\n";
    }

    if ( ! $fp = @fopen($filepath, 'a')) {
    return FALSE;
    }

    $message .= date($this->format).' --> '.$msg."\n";

    flock($fp, LOCK_EX);
    fwrite($fp, $message);
    flock($fp, LOCK_UN);
    fclose($fp);

    if (isset($newfile) && $newfile === TRUE) {
    @chmod($filepath, FILE_WRITE_MODE);
    }
    return TRUE;
    }

    }