Skip to content

Instantly share code, notes, and snippets.

@nerdy-sam
Created January 22, 2012 08:13
Show Gist options
  • Save nerdy-sam/1656231 to your computer and use it in GitHub Desktop.
Save nerdy-sam/1656231 to your computer and use it in GitHub Desktop.
Simple Logging Class for PHP
/**
* Simple Logging Class for PHP
*
* I hate using “echo” statements in PHP for debugging. Instead,
* I prefer to tail a log file. I wrote this little logging class
* that I can include in whatever project I’m working on.
*
* @author Joyce Johnston http://fourmilestomay.com/contact-us/
* @license Unknown but made public by author
* @link http://fourmilestomay.com/2009/simple-logging-class-for-php/
*/
class Logger {
private $level;
private $filename;
function __construct($level = '',$path='') {
if ($level !='') {
$this->level = $level;
}
else {
$this->level = 'info';
}
if($path=='') {
$this->filename = BASE_URI.'shared/log/development.log';
}
else {
$this->filename = $path;
}
}
public function debug($message) {
if($this->level == 'debug') {
$this->write_log($message);
}
}
public function info($message) {
if($this->level == 'info' || $this->level == 'debug' ) {
$this->write_log($message);
}
}
public function warn($message) {
if($this->level == 'warn' ||$this->level == 'info' || $this->level == 'debug' ) {
$this->write_log($message);
}
}
public function error($message) {
if($this->level == 'error' || $this->level == 'warn' || $this->level == 'info' || $this->level == 'debug' ) {
$this->write_log($message);
}
}
public function request() {
$headers = getallheaders();
$this->write_log("REQUEST:");
$this->write_log($headers);
$get_params = $_GET;
if($get_params) {
$this->write_log("GET parameters:");
$this->write_log($get_params);
}
$post_params = $_POST;
if($post_params) {
$this->write_log("POST parameters:");
$this->write_log($post_params);
}
}
public function session() {
$session_params = $_SESSION;
if($session_params) {
$this->write_log("SESSION parameters:");
$this->write_log($session_params);
}
}
private function write_log($message) {
//$message can either be a string or an array
$fd = fopen($this->filename, 'a');
if(is_array($message)) {
$this->write_array($message, $fd);
}
else {
$this->write_string($message, $fd);
}
fclose($fd);
}
private function write_string($message, $fd) {
fwrite($fd, $message."\n");
}
private function write_array($message, $fd) {
foreach($message as $key => $value) {
if(is_array($value)) {
fwrite($fd, $key."{ ");
$this->write_array($value, $fd);
fwrite($fd, " }\n");
}
else {
$string = "\t {".$key.': '.$value."}\n ";
fwrite($fd, $string);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment