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

/lib/Log.php

https://github.com/ryanc/php-pop3
PHP | 169 lines | 135 code | 33 blank | 1 comment | 8 complexity | 7b439d9d26967cc16ab567e52e5215b4 MD5 | raw file
  1. <?php
  2. class Log
  3. {
  4. private static $_instance = null;
  5. private static $_logFile;
  6. private $_fh;
  7. private $_dateFormat = "M j, Y H:i:s";
  8. private $_logFormat = "%s [%s] %s";
  9. const LF = "\n";
  10. // Error levels.
  11. const LOG_EMERGENCY = 0;
  12. const LOG_ALERT = 1;
  13. const LOG_CRITICAL = 2;
  14. const LOG_ERROR = 3;
  15. const LOG_WARNING = 4;
  16. const LOG_NOTICE = 5;
  17. const LOG_INFO = 6;
  18. const LOG_DEBUG = 7;
  19. private function __construct() {}
  20. public function __clone()
  21. {
  22. throw Exception("Cloning is not supported.");
  23. }
  24. public static function singleton($logfile)
  25. {
  26. if ($logfile === null) {
  27. throw new Log_Exception("The log file path cannot be null.");
  28. }
  29. self::$_logFile = $logfile;
  30. if (self::$_instance === null) {
  31. $class = __CLASS__;
  32. self::$_instance = new $class;
  33. }
  34. return self::$_instance;
  35. }
  36. public static function instance() {
  37. if(self::$_instance !== null) {
  38. return self::$_instance;
  39. }
  40. }
  41. public function open()
  42. {
  43. $this->_fh = @fopen(self::$_logFile, 'a');
  44. if ($this->isFileOpen() === false) {
  45. throw new Log_Exception("Cannot open the log file: {$logfile}");
  46. }
  47. }
  48. public function write($line)
  49. {
  50. $buf = sprintf("%s%s", $line, self::LF);
  51. if (fwrite($this->_fh, $buf) === false) {
  52. throw new Log_Exception("Error writing to log file.");
  53. }
  54. }
  55. public function close()
  56. {
  57. fclose($this->_fh);
  58. }
  59. public function isFileOpen()
  60. {
  61. if (is_resource($this->_fh) === true) {
  62. return true;
  63. }
  64. else {
  65. return false;
  66. }
  67. }
  68. public function priorityToString($priority)
  69. {
  70. $priorityMap = array(
  71. self::LOG_EMERGENCY => 'emergency',
  72. self::LOG_ALERT => 'alert',
  73. self::LOG_CRITICAL => 'critical',
  74. self::LOG_ERROR => 'error',
  75. self::LOG_WARNING => 'warning',
  76. self::LOG_NOTICE => 'notice',
  77. self::LOG_INFO => 'info',
  78. self::LOG_DEBUG => 'debug'
  79. );
  80. return $priorityMap[$priority];
  81. }
  82. public function stringToPriority($name)
  83. {
  84. $priorityMap = array(
  85. 'emergency' => self::LOG_EMERGENCY,
  86. 'alert' => self::LOG_ALERT,
  87. 'critical' => self::LOG_CRITICAL,
  88. 'error' => self::LOG_ERROR,
  89. 'warning' => self::LOG_WARNING,
  90. 'notice' => self::LOG_NOTICE,
  91. 'info' => self::LOG_INFO,
  92. 'debug' => self::LOG_DEBUG
  93. );
  94. return $priorityMap[strtolower($name)];
  95. }
  96. public function log($priority, $line)
  97. {
  98. $date = new DateTime();
  99. $this->write(sprintf($this->_logFormat,
  100. $date->format($this->_dateFormat),
  101. $this->priorityToString($priority),
  102. $line
  103. ));
  104. }
  105. public function emerg($line) {
  106. $this->log(self::LOG_EMERGENCY, $line);
  107. }
  108. public function alert($line) {
  109. $this->log(self::LOG_ALERT, $line);
  110. }
  111. public function crit($line) {
  112. $this->log(self::LOG_CRITICAL, $line);
  113. }
  114. public function error($line) {
  115. $this->log(self::LOG_ERROR, $line);
  116. }
  117. public function warn($line) {
  118. $this->log(self::LOG_WARNING, $line);
  119. }
  120. public function notice($line) {
  121. $this->log(self::LOG_NOTICE, $line);
  122. }
  123. public function info($line) {
  124. $this->log(self::LOG_INFO, $line);
  125. }
  126. public function debug($line) {
  127. $this->log(self::LOG_DEBUG, $line);
  128. }
  129. public function __destruct()
  130. {
  131. if ($this->isFileOpen() === true) {
  132. $this->close();
  133. }
  134. }
  135. }
  136. class Log_Exception extends Exception {}