I just wrote a little "logging" class and want to ask a question about the usage of this class, how i could make it easier to use.
For example:
$log = new Log();
$log->Error("You have an error!", __FILE__, __CLASS__, __FUNCTION__, __LINE__);
This is how i write errors to a log file at the moment, but it seems to complex?! Is there a way to get the "MAGIC CONSTANTS" inside the logging class from the "calling" php ?
Here is the class code (any other tips are welcome too):
<?php
class Log
{
private $path;
public function __construct()
{
$config = new Config(); // init. from autoloader
$path = $config->app_log_dir;
if (!is_dir($path) && !is_writable($path))
{
error_log('[ERROR] [Log::__Construct()] -> ' . $path . ' does not exist or is not writeable!',0);
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$this->path = $path;
}
public function Error($message, $file, $class = '', $function = '', $line)
{
$array_data = array($message, $file, $class, $function, $line);
$this->write('ERROR', $array_data);
}
public function TestError($message, $file = __FILE__, $class = __CLASS__, $function = __FUNCTION__, $line = __LINE__)
{
$array_data = array($message, $file, $class, $function, $line);
$this->write('TESTERROR', $array_data);
}
private function write($error_type, $array_data)
{
$date = date("Y-m-d H:i:s");
$dateFile = date("Y-m-d");
$message = "[{$date}] [{$error_type}] [{$array_data[1]}->{$array_data[2]}::{$array_data[3]}:{$array_data[4]}] $array_data[0]".PHP_EOL;
try
{
file_put_contents($this->path.'/'.$dateFile.'.log', $message, FILE_APPEND);
}
catch (Exception $e)
{
error_log('[ERROR] [Log::write()] -> ' . $e, 0);
header("HTTP/1.0 500 Internal Server Error");
exit();
}
}
}