PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Log.php

https://bitbucket.org/delpho/tickhub
PHP | 66 lines | 37 code | 8 blank | 21 comment | 7 complexity | f4c52d82e174c58b240885b410781ab9 MD5 | raw file
  1. <?php
  2. /**
  3. * Description of Log
  4. *
  5. * @author jaraya
  6. */
  7. final class Log {
  8. /**
  9. * The object instance
  10. *
  11. * @var Log
  12. */
  13. private static $_instance = null;
  14. private $_logFile;
  15. private function __construct() {
  16. $this->_logFile = LOG_FILE;
  17. }
  18. /**
  19. * Get the instance (singleton)
  20. *
  21. * @return Log The instance of this class
  22. */
  23. public static function getInstance(){
  24. if ( !isset(self::$_instance) || self::$_instance == null ) {
  25. $c = __CLASS__;
  26. self::$_instance = new $c;
  27. }
  28. return self::$_instance;
  29. }
  30. /**
  31. * Log a message into the log file
  32. *
  33. * @param strign $message The message to log
  34. * @param bool $display If true show the message
  35. */
  36. public function log($message, $display = false){
  37. $current_tz = date_default_timezone_get();
  38. if( ini_get('date.timezone') )
  39. date_default_timezone_set(ini_get('date.timezone'));
  40. $date = gmdate("Y/m/d H:i:s");
  41. date_default_timezone_set($current_tz);
  42. $log = "[$date] - $message\n";
  43. $fp = @fopen($this->_logFile,'a');
  44. if ( $display ) echo "$log\n";
  45. if ( is_resource($fp) ) {
  46. @fwrite($fp, $log);
  47. @fclose($fp);
  48. } else {
  49. trigger_error("Can't open '{$this->_logFile}' log file",E_USER_WARNING);
  50. }
  51. }
  52. public function logException ( Exception $ex ) {
  53. $this->log("[exception] ".basename($ex->getFile())."@{$ex->getLine()} : {$ex->getMessage()}. Trace: {$ex->getTraceAsString()}");
  54. }
  55. }