Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/class.log.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* -------------------------------
* Class Name: LogException
* Desc: A custom Exception to distinguish between the logger's exceptions and other exceptions.
*/
class LogException extends Exception {}


/* -------------------------------
* Class Name: Log
Expand All @@ -9,14 +16,14 @@
class Log {

private $logs;
public $entry;
public $log_status;
public $entry,
$log_status;

const LOG_INACTIVE = 0;
const LOG_ACTIVE = 1;
const LOG_FAILED = 2;
const LOG_INACTIVE = 0,
LOG_ACTIVE = 1,
LOG_FAILED = 2;

const TIMESTAMP_FORMAT = 'd/m/y - G:i:s';
private $timestamp_format = 'd/m/y - G:i:s';

/**
* __construct function
Expand All @@ -27,7 +34,7 @@ class Log {
* @param String $timezone
*/

public function __construct($logfiles = 'temp.log', $timezone = 0)
public function __construct($logfiles = 'temp.log', $timezone = 0, $timestamp_format = null)
{
if(is_array($logfiles)){
$this->logs = $logfiles;
Expand All @@ -39,6 +46,9 @@ public function __construct($logfiles = 'temp.log', $timezone = 0)

$timezone = (is_string($timezone)) ? $timezone : date_default_timezone_get();
date_default_timezone_set($timezone);

// If the user specified a custom timestamp format, use that
if ($timestamp_format) $this->timestamp_format = $timestamp_format;

$this->activateLog();
}
Expand Down Expand Up @@ -73,7 +83,7 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul

// Add timestamp
if($timestamp){
$this->entry = '[' . date(self::TIMESTAMP_FORMAT, time()) . ']: ';
$this->entry = '[' . date($this->timestamp_format, time()) . ']: ';
}

// Add meta data, if available
Expand All @@ -89,14 +99,15 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul

if($this->log_status && is_string($log)){

$fp = @fopen($this->logs[$log],'a') or die('Can\'t open log file: ' . $log);

if($fp){
fwrite($fp, $this->entry);
}else{
$fp = @fopen($this->logs[$log],'a');

if (!$fp)
{
$this->log_status = self::LOG_FAILED;
throw new LogException('Can\'t open log file: ' . $log);
}


fwrite($fp, $this->entry);
fclose($fp);
}
}
Expand All @@ -109,7 +120,8 @@ public function entry($entry, $meta = array(), $timestamp = true, $log = 'defaul

public function clearLog($log = 'default')
{
$fp = fopen($this->logs[$log],'w') or die("can't open log");
$fp = @fopen($this->logs[$log],'w');
if (!$fp) throw new LogException('Can\'t open log file: ' . $log);
}

/**
Expand Down