Created
April 18, 2010 04:09
-
-
Save anonymous/370000 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Logger | |
{ | |
function log ($id, $type, $time, $description); | |
} | |
class Log | |
{ | |
const INFORMATION = 'i'; | |
const WARNING = 'w'; | |
const ERROR = 'e'; | |
protected $loggers = array(); | |
function addLogger(Logger $loggerObject) | |
{ | |
$loggers[] = $loggerObject; | |
} | |
function logEvent ($id, $type, $description) | |
{ | |
foreach($loggers as $logger) | |
{ | |
$logger->log($id, $type, time(), $description); | |
} | |
} | |
} | |
class FileLogger implements Logger | |
{ | |
protected $filePath; | |
protected $fileResource; | |
function log ($id, $type, $time, $description) | |
{ | |
if (empty($this->filePath)) | |
{ | |
throw new LogicException('A file path must be set before calling '.__METHOD__.'().'); | |
} | |
if(is_resource($this->fileResource) == false) { | |
$this->fileResource = fopen($this->filePath, 'a'); | |
} | |
fwrite($this->fileResource, sprintf("%s\t%s\t%d\t%s", $id, $type, $time, $description)); | |
} | |
function setFilePath ($filePath) | |
{ | |
$this->filePath = $filePath; | |
} | |
} | |
class DatabaseLogger implements Logger | |
{ | |
protected $db; | |
function log ($id, $type, $time, $description) | |
{ | |
if(empty($db)) { | |
throw new LogicException('A database object must be set before calling '.__METHOD__.'().'); | |
} | |
$this->db->query("INSERT INTO log (id, type, time, description) VALUES ($id, $type, $time, $description)"); | |
} | |
function setDatabase(PDO $db) | |
{ | |
$this->db = $db; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment