I want to create a log class with three methods. I can write to a text file, write to a log and send email. I want to be able to switch to either of the methods using the $type var.
I have a quetion.
I wanted to make this class a factory so I could create the logs. I'm not sure the factory concept would work in this case but I made it a static function so it can't be instantiated.
public static function Log()
{
return new Log;
}
The class is not done yet but I am trying to understand the factory concept for this case. Thanks,
<?php
require_once "db.php";
class Log
{
private $fh, $msg, $type;
function __construct($msg){
$this->fh = null;
$this->type=$type;
$this->msg=$new_msg;
}
public static function Log()
{
return new Log;
}
funtion set_message($new_msg){
$this->msg= $new_msg;
}
function get_message(){
return $this->msg;
}
public function logToFile($msg){
$fd = fopen($filename, "a");
$str = "[" .date("Y/m/d h:i:s", mktme()) . "] " . $msg;
fwrite($fd, $str. "\n");
$fclose($fd);
$db = LogDb::getConnection();
}//end function
public static function logToDb($msg, $type){
// open connection to database
$db = LogDb::getConnection();
$query = "INSERT INTO log (date, type, msg) VALUES(NOW(),
'$type', '$msg')";
}//end function
}//end class
function logToMail($msg, $address,$type)
{
// append date/time to message
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
mail($address, "Log message", $str);
}
$conn = @mysql_connect("localhost", "joe", "pass");
if (!$conn) { logToMail("Could not connect to database",
"webmaster@my.domain.name.com"); }
$a = array("chocolate", "strawberry", "peach");
if (!in_array('fish', $a)) { logToDB("No fish available", 14); }
logFactory::()->writelog();
$v = "Mary had a little lamb";
if (!is_numeric($v)) { logToFile("my.log", "Non-numeric variable
encountered"); }
$a = array("chocolate", "strawberry", "peach");
if (!in_array('fish', $a)) { logToFile("my.log", "No fish available"); }
$conn = @mysql_connect("localhost", "joe", "pass");
if (!$conn)
{
logToFile("my.log", "Could not connect to database");
die("Could not connect to database");
}