tickhub /classes/Log.php

Language PHP Lines 67
MD5 Hash f4c52d82e174c58b240885b410781ab9 Estimated Cost $752 (why?)
Repository https://bitbucket.org/delpho/tickhub.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

/**
 * Description of Log
 *
 * @author jaraya
 */
final class Log {

	/**
	 * The object instance
	 *
	 * @var Log
	 */
	private static $_instance = null;

	private $_logFile;

	private function  __construct() {
		$this->_logFile = LOG_FILE;
	}

	/**
	 * Get the instance (singleton)
	 *
	 * @return Log The instance of this class
	 */
	public static function getInstance(){
		if ( !isset(self::$_instance) || self::$_instance == null ) {
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}


	/**
	 * Log a message into the log file
	 *
	 * @param strign $message The message to log
	 * @param bool $display If true show the message
	 */
	public function log($message, $display = false){
		$current_tz =  date_default_timezone_get();
		if( ini_get('date.timezone') )
			date_default_timezone_set(ini_get('date.timezone'));
		$date = gmdate("Y/m/d H:i:s");

		date_default_timezone_set($current_tz);
		$log = "[$date] - $message\n";
		$fp = @fopen($this->_logFile,'a');
		if ( $display ) echo "$log\n";
		if ( is_resource($fp) ) {
			@fwrite($fp, $log);
			@fclose($fp);
		} else {
			trigger_error("Can't open '{$this->_logFile}' log file",E_USER_WARNING);
		}
	}
	
	public function logException ( Exception $ex ) {
		$this->log("[exception] ".basename($ex->getFile())."@{$ex->getLine()} : {$ex->getMessage()}. Trace: {$ex->getTraceAsString()}");
	}
	

}
Back to Top