Skip to content

Instantly share code, notes, and snippets.

@jaimerodas
Created October 9, 2012 21:38
Show Gist options
  • Save jaimerodas/3861624 to your computer and use it in GitHub Desktop.
Save jaimerodas/3861624 to your computer and use it in GitHub Desktop.
Logger
<?
// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment