PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/class.logger.php

http://pork-dbobject.googlecode.com/
PHP | 253 lines | 152 code | 37 blank | 64 comment | 28 complexity | 513170bfbadaa9a9321edbc74106052d MD5 | raw file
  1. <?php
  2. /**
  3. * Logger
  4. *
  5. * The logger provides basic logging / debug info storage to the database. Needs to be extended in the future.
  6. *
  7. * @todo Make the auto-dbtype-switch if available.
  8. * @todo If all else fails: use emails.
  9. * @package Pork-Utilities
  10. * @author Jelle Ursem
  11. * @copyright Jelle Ursem 2009
  12. * @version 1.0
  13. * @access public
  14. */
  15. class Logger {
  16. private $checked= false;
  17. private $debugmode = false;
  18. #queries to create the initial table for your logging.
  19. private $createqueries = array(
  20. "sqlite" => "CREATE TABLE '@logtable@' ('RequestID' VARCHAR(255) NOT NULL, 'Message' VARCHAR(255) NOT NULL, 'Payload' TEXT NULL , 'Type' VARCHAR (20) NULL, 'Class' VARCHAR( 255 ) NULL , 'Function' VARCHAR( 255 ) NULL , 'File' VARCHAR( 255 ) NULL ,'Line' INT( 11 ) NULL , 'Time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , 'POST' TEXT NULL , 'GET' TEXT NULL , 'URI' VARCHAR( 255 ) NOT NULL , 'REMOTE_ADDR' VARCHAR( 25 ) NOT NULL )",
  21. "mysql" => "CREATE TABLE `@logtable@` ( `ROWID` int(11) NOT NULL auto_increment, `RequestID` varchar(255) NOT NULL, `Message` varchar(255) NOT NULL, `Payload` text NOT NULL, `Type` varchar(20) NOT NULL, `Class` varchar(255) NOT NULL, `Function` varchar(255) NOT NULL, `Line` int(11) NOT NULL, `File` varchar(255) NOT NULL, `Time` timestamp NOT NULL, `POST` text NOT NULL, `GET` text NOT NULL, `URI` varchar(255) NOT NULL, `REMOTE_ADDR` varchar(25) NOT NULL, PRIMARY KEY (`ROWID`) ) AUTO_INCREMENT=0 ;"
  22. );
  23. public $RequestID;
  24. /**
  25. * Logger::__construct()
  26. */
  27. public function __construct()
  28. {
  29. $this->RequestID = md5($_SERVER['REQUEST_URI'].$_SERVER['REMOTE_ADDR'].$_SERVER['REQUEST_TIME']).time().uniqid();
  30. }
  31. /**
  32. * Logger::Log()
  33. * Sends a LOG message to the log. This will not work if debugmode is disabled.
  34. */
  35. public static function Log($var)
  36. {
  37. if(Settings::Load()->Get('Logger', 'debug') == '1')
  38. {
  39. $trace =
  40. self::getInstance()->createLog(Array('Variable' => $var, 'Parameters' => func_get_args()), 'LOG', debug_backtrace());
  41. }
  42. }
  43. /**
  44. * Logger::Info()
  45. * Sends an INFO message to the log. This will not work if debugmode is disabled.
  46. */
  47. public static function Info($var)
  48. {
  49. if(Settings::Load()->Get('Logger', 'debug') == '1')
  50. {
  51. self::getInstance()->createLog(Array('Variable' => $var, 'Parameters' => func_get_args()), 'INFO', debug_backtrace());
  52. }
  53. }
  54. public static function Dump($var)
  55. {
  56. if(Settings::Load()->Get('Logger', 'debug') == '1')
  57. {
  58. self::getInstance()->createLog(Array('Variable' => $var, 'Parameters' => func_get_args()), 'DUMP', debug_backtrace());
  59. }
  60. }
  61. /**
  62. * Logger::Warn()
  63. * Sends a warning to the log. This will also work if debugmode is disabled.
  64. */
  65. public static function Warn($var)
  66. {
  67. self::getInstance()->createLog(Array('Variable' => $var, 'Parameters' => func_get_args()), 'WARN', debug_backtrace());
  68. }
  69. public static function Error($var)
  70. {
  71. self::getInstance()->trace[] = debugLog::Log(Array('Parameters' => func_get_args()), 'ERROR', debug_backtrace());
  72. }
  73. /**
  74. * Logger::Trace()
  75. * Sends a debug_print_backtrace to the log. This will also work if debugmode is disabled.
  76. */
  77. public static function Trace($var)
  78. {
  79. $trace = debug_backtrace();
  80. self::getInstance()->PHPError($var, 'TRACE', $trace[sizeof($trace) == 1 ? 0 : 1]['file'], $trace[sizeof($trace) == 1 ? 0 : 1]['line'], $trace);
  81. // self::getInstance()->createLog(Array('Variable' => $var, 'Parameters' => func_get_args(), 'Trace'=>$trace), 'TRACE');
  82. }
  83. /**
  84. * Logger::PHPError
  85. * Catches PHP errors to the database.
  86. */
  87. public function PHPError($errstr, $err, $errfile, $errline, $backtrace)
  88. {
  89. if((($err) == 'NOTICE' || $err == 'STRICT NOTICE') && $this->debugmode == false) return;
  90. $log = new DebugLog();
  91. $log->Payload = @json_encode($backtrace);
  92. $log->Message = $errstr;
  93. $log->REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
  94. $log->URI = $_SERVER['REQUEST_URI'];
  95. $log->POST = !empty($_POST) ? @json_encode($_POST) : "";
  96. $log->GET = !empty($_GET) ? @json_encode($_GET) : "";
  97. $log->RequestID = $this->RequestID;
  98. $log->Class = @$backtrace[0]['class'];
  99. $log->Function = @$backtrace[0]['function'];
  100. $log->Type = $err;
  101. $log->File = $errfile;
  102. $log->Line = $errline;
  103. $log->Time = now();
  104. $log->Save();
  105. }
  106. /**
  107. * Logger:: CreateLog
  108. * Used internally and externally to actually insert data into the db.
  109. *
  110. */
  111. public function createLog($debug, $type='LOG', $trace=false)
  112. {
  113. $log = new DebugLog();
  114. $log->Message = ($type != 'DUMP' && sizeof($debug['Parameters']) == 1 && is_string($debug['Variable'])) ? $debug['Variable'] : json_encode($debug);
  115. $log->RequestID = $this->RequestID;
  116. $log->REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
  117. $log->URI = $_SERVER['REQUEST_URI'];
  118. $log->POST = !empty($_POST) ? json_encode($_POST) : "";
  119. $log->GET = !empty($_GET) ? json_encode($_GET) : "";
  120. $log->Type = $type;
  121. $log->Time = now();
  122. if(!$trace) $trace = debug_backtrace();
  123. $curlog = (sizeof($trace) > 2) ? $trace[2] : (sizeof($trace)> 1) ? $trace[1] : $trace[0];
  124. if($type == 'TRACE') {
  125. ob_start();
  126. debug_print_backtrace();
  127. $debug['Trace']= nl2br(ob_get_clean());
  128. $log->Payload = json_encode($debug);
  129. }
  130. if($type == 'DUMP') {
  131. $log->Payload = json_encode($debug);
  132. }
  133. $log->Function = @$curlog['function'];
  134. $log->Class = @$curlog['class'];
  135. $log->Line = @$curlog['line'];
  136. $log->File = @$curlog['file'];
  137. $log->Save();
  138. }
  139. /**
  140. * Logger::logEmail()
  141. *
  142. * @todo make it so that this sends an email and is automatically used as fallback.
  143. * @param mixed $message
  144. */
  145. public static function logEmail($message)
  146. {
  147. echo('email log '.$message);
  148. }
  149. /**
  150. * Logger::dbCheck()
  151. * Checks if the current database exists, and creates it if it's not there.
  152. */
  153. public function dbCheck()
  154. {
  155. if(!$this->checked)
  156. {
  157. $this->log[] = "checking if db exists";
  158. $this->checked = dbConnection::getInstance('Logger')->tableExists(Settings::Load()->Get('Logger', 'logtable'));
  159. if (!$this->checked)
  160. {
  161. $createquery = $this->createqueries[strtolower(Settings::Load()->Get('Logger', 'dbtype'))];
  162. $createquery = str_replace('@logtable@', Settings::Load()->Get('Logger', 'logtable'), $createquery);
  163. $this->checked = dbConnection::getInstance('Logger')->query($createquery);
  164. if(!$this->checked) die( "Error creating logdatabase");
  165. Logger::Log("Log database created.");
  166. }
  167. }
  168. }
  169. /**
  170. * Logger::getInstance()
  171. * Singleton functionality
  172. *
  173. * @return Logger $instance
  174. */
  175. public static function getInstance()
  176. {
  177. static $instance;
  178. if (!isset($instance))
  179. {
  180. $c = __CLASS__;
  181. $instance = new $c;
  182. $instance->dbCheck();
  183. }
  184. return $instance;
  185. }
  186. }
  187. /**
  188. *
  189. * DebugLog dbObject class. As soon as the database is created, this is used to insert records into the debuglog table.
  190. *
  191. */
  192. class DebugLog extends dbObject
  193. {
  194. function __construct($ID=false)
  195. {
  196. $this->__setupDatabase('debuglog', // db table
  197. array('ROWID' => 'ID', // db field => object property
  198. 'RequestID' => 'RequestID',
  199. 'Message' => 'Message',
  200. 'Payload' => 'Payload',
  201. 'Type' => 'Type',
  202. 'Class' => 'Class',
  203. 'Function' => 'Function',
  204. 'Line' => 'Line',
  205. 'File' => 'File',
  206. 'Time' => 'Time',
  207. 'POST' => 'POST',
  208. 'GET' => 'GET',
  209. 'URI' => 'URI',
  210. 'REMOTE_ADDR' => 'REMOTE_ADDR'),
  211. 'ROWID', // primary db key
  212. $ID, 'Logger'); // primary key value
  213. $this->setOrderProperty('Time', 'DESC');
  214. }
  215. }
  216. ?>