logging.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Logging class:
  4. * - constructor requires the full file name and path for the log file. if it
  5. * does not exist php will automatically try and create it. The log file will
  6. * remain open for the life cycle of the object to improve performance.
  7. * - message is written with the following format: [d/M/Y:H:i:s] (script name) message.
  8. * - log file is closed when the object is destroyed.
  9. */
  10. class logging {
  11. // declare log file and file pointer as private properties
  12. private $fp;
  13. private $debug_func;
  14. private $debug_line;
  15. private $debug_file;
  16. private $debug_class;
  17. public function __construct(string $filename_and_path) {
  18. //init values
  19. $this->clear_debug();
  20. try {
  21. //open file in append mode
  22. $this->fp = fopen($filename_and_path, 'a');
  23. } catch (Exception $ex) {
  24. //send the error to the caller
  25. throw $ex;
  26. }
  27. }
  28. public function __destruct() {
  29. try {
  30. $this->flush();
  31. } catch (Exception $ex) {
  32. //do nothing
  33. } finally {
  34. //close the file
  35. if (is_resource($this->fp)) {
  36. fclose($this->fp);
  37. }
  38. }
  39. }
  40. /**
  41. * Ensure all data arrives on disk
  42. * @throws Exception
  43. */
  44. public function flush() {
  45. try {
  46. //ensure everything arrives on disk
  47. if (is_resource($this->fp)) {
  48. fflush($this->fp);
  49. }
  50. } catch (Exception $ex) {
  51. throw $ex;
  52. }
  53. }
  54. // write message to the log file
  55. private function _write($msg) {
  56. // define current time and suppress E_WARNING if using the system TZ settings
  57. }
  58. private function clear_debug() {
  59. $this->debug_line = null;
  60. $this->debug_file = null;
  61. $this->debug_func = null;
  62. $this->debug_class = null;
  63. }
  64. /**
  65. * Write raw data to the
  66. * @param string $level
  67. * @param string $message
  68. */
  69. public function write(string $level, string $message) {
  70. $this->get_backtrace_details();
  71. // write current time, script name and message to the log file
  72. // (don't forget to set the INI setting date.timezone)
  73. $time = @date('Y-m-d H:i:s');
  74. $file = $this->debug_file ?? 'file not set';
  75. $line = $this->debug_line ?? '0000';
  76. fwrite($this->fp, "[$time] [$level] [{$file}:{$line}] $message");
  77. $this->clear_debug();
  78. }
  79. public function debug_class(?string $debug_class = null) {
  80. if (func_num_args() > 0) {
  81. $this->debug_class = $debug_class;
  82. return $this;
  83. }
  84. return $this->debug_class;
  85. }
  86. public function debug_line(?string $debug_line = null) {
  87. if (func_num_args() > 0) {
  88. $this->debug_line = $debug_line;
  89. return $this;
  90. }
  91. return $this->debug_line;
  92. }
  93. public function debug_func(?string $debug_func = null) {
  94. if (func_num_args() > 0) {
  95. $this->debug_func = $debug_func;
  96. return $this;
  97. }
  98. return $this->debug_func;
  99. }
  100. public function debug_file(?string $debug_file = null) {
  101. if (func_num_args() > 0) {
  102. $this->debug_file = $debug_file;
  103. return $this;
  104. }
  105. return $this->debug_file;
  106. }
  107. public function writeln($level, $message) {
  108. $this->get_backtrace_details();
  109. $this->write($level, $message . "\n");
  110. }
  111. public function debug($message) {
  112. $this->get_backtrace_details();
  113. $this->writeln("DEBUG", $message);
  114. }
  115. public function info($message) {
  116. $this->get_backtrace_details();
  117. $this->writeln("INFO", $message);
  118. }
  119. public function warning($message) {
  120. $this->get_backtrace_details();
  121. $this->writeln("WARNING", $message);
  122. }
  123. public function error($message) {
  124. $this->get_backtrace_details();
  125. $this->writeln("ERROR", $message);
  126. }
  127. private function get_backtrace_details() {
  128. if ($this->debug_file === null) {
  129. $debug = debug_backtrace();
  130. $ndx = count($debug) - 1;
  131. $this->debug_file = $debug[$ndx]['file'];
  132. $this->debug_line = $debug[$ndx]['line'];
  133. $this->debug_func = $debug[$ndx]['function'];
  134. $this->debug_class = $debug[$ndx]['class'] ?? '';
  135. }
  136. }
  137. }
  138. /*
  139. * Example:
  140. $log = new logging(sys_get_temp_dir() . '/logging.log');
  141. $log->writeln("debug", "passed validation");
  142. $log->debug("pass");
  143. $log->warning("variable should not used");
  144. $log->debug_file(__FILE__)->debug_line(__LINE__)->write("DEBUG", "Raw message\n");
  145. */