Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created October 2, 2012 20:18
Show Gist options
  • Save cam-gists/3823032 to your computer and use it in GitHub Desktop.
Save cam-gists/3823032 to your computer and use it in GitHub Desktop.
PHP: Logger
<?php
/**
* Debug / Logger
*
* @author : christopher a. moore <[email protected]>
* @Usage :
* $log = new Log();
* $log->debug = true;
* $log->msg('Initialize Debug Class');
*
*/
class Log
{
public $file;
public $debug;
/**
* Setup Logfile
*/
function __construct()
{
$this->file = dirname(__FILE__) . '/log.txt';
}
/**
* Write Msg
*/
function msg($data){
if (!$this->debug === false ){
$msg = $this->whereCalled() . $data;
file_put_contents($this->file, $msg."\n", FILE_APPEND);
}
}
/**
* Detect Where Called From
*/
function whereCalled( $level = 1 ) {
$trace = debug_backtrace();
$file = $trace[$level]['file'];
$line = $trace[$level]['line'];
$object = $trace[$level]['object'];
if (is_object($object)) { $object = get_class($object); }
return "Line : $line ($file) : ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment