Permalink
Browse files

Cleaned up logger class, added some simple usage (which is self expla…

…nitory anyway) and added Logger#clear to clear the current log.
  • Loading branch information...
1 parent ef7a5b3 commit 9bc4658119bb514e85c4403a5c84a336b9504144 Josh Nesbitt committed Aug 14, 2009
Showing with 58 additions and 31 deletions.
  1. +1 −1 MIT-LICENSE
  2. +38 −27 lib/logger.php
  3. +19 −3 readme.rdoc
View
@@ -1,4 +1,4 @@
-Copyright (c) 2009 [name of plugin creator]
+Copyright (c) 2009 [Josh Nesbitt]
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -1,80 +1,91 @@
<?php
/**
* Simple Logger Class
+*
* @author Josh Nesbitt <josh@josh-nesbitt.net>
*
-* By default will write to /log/filename.log
+* By default will write to path/to/logger/ + log/filename.log
*
**/
class Logger {
- var $file, $level, $stream;
+ var $file, $path, $level, $stream;
const INFO = 4;
- const WARN = 3;
- const DEBUG = 2;
+ const DEBUG = 3;
+ const WARN = 2;
const ERROR = 1;
const FATAL = 0;
function __construct($file, $level)
{
$this->file = $file;
$this->level = $level;
- $this->stream = fopen($_SERVER["DOCUMENT_ROOT"] . "/log/$this->file", "a") or die("Cannot write to file '$this->path'");
+ $this->path = $_SERVER["DOCUMENT_ROOT"] . "/log/$this->file";
+ $this->start();
}
function info($string)
{
- if($this->level < self::INFO) return true;
- $this->log($string);
+ return $this->check_level(self::INFO) ? true : $this->log($string);
}
function warn($string)
{
- if($this->level < self::WARN) return true;
- $this->log($string);
+ return $this->check_level(self::WARN) ? true : $this->log($string);
}
function debug($string)
{
- if($this->level < self::DEBUG) return true;
- $this->log($string);
+ return $this->check_level(self::DEBUG) ? true : $this->log($string);
}
function error($string)
{
- if($this->level < self::ERROR) return true;
- $this->log($string);
+ return $this->check_level(self::ERROR) ? true : $this->log($string);
}
function fatal($string)
{
- if($this->level < self::FATAL) return true;
- $this->log($string);
+ return $this->check_level(self::FATAL) ? true : $this->log($string);
+ }
+
+ function clear()
+ {
+ $this->close();
+ $this->open("w");
+ $this->close();
+ $this->open();
+ }
+
+ private function check_level($level)
+ {
+ return $this->level < $level;
}
private function log($string)
{
$this->write("[". date('l jS F Y : h:i:sa') . "] ". $string . "\r\n");
}
-
+
private function write($string)
{
return fwrite($this->stream, $string);
}
+
+ private function start()
+ {
+ return $this->open();
+ }
+
+ private function open($mode="a")
+ {
+ return $this->stream = fopen($this->path, $mode) or die("Cannot write to file '$this->path', please ensure '$this->path' is writable.");
+ }
- private function cut()
+ private function close()
{
return fclose($this->stream);
}
+
}
-$logger = new Logger("development.log", Logger::INFO);
-
-$logger->info("--> info");
-$logger->warn("--> warn");
-$logger->debug("--> debug");
-$logger->error("--> error");
-$logger->fatal("--> fatal");
-
-# TODO: finish me...
-
?>
View
@@ -1,7 +1,23 @@
= Logger
-A simple PHP logger class. If you only need something simple:
+A simple PHP logger class for logging out to a specified file.
- $logger = new Logger("development.log", Logger::INFO);
+Log levels can be one of the following:
+
+ INFO
+ DEBUG
+ WARN
+ ERROR
+ FATAL
+
+Output is only written to file if the log level is within the scope of the level specified when instantiating the logger (e.g. a info call would not be written if the log level was set to WARN).
- $logger->info("logging from the info level");
+== Usage
+
+ $logger = new Logger("development.log", Logger::INFO);
+
+ # would create a new log file at /path/to/logger/script/log/development.log
+
+ $logger->info("Why hello there Ted");
+
+ # writes "Why hello there Ted" to development.log

0 comments on commit 9bc4658

Please sign in to comment.