暫無描述

Logging.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. /**
  6. * Class PUM_Utils_Logging
  7. *
  8. * @since 1.8.0
  9. */
  10. class PUM_Utils_Logging {
  11. /**
  12. * @var bool
  13. */
  14. public $is_writable = true;
  15. /**
  16. * @var string
  17. */
  18. private $filename = '';
  19. /**
  20. * @var string
  21. */
  22. private $file = '';
  23. /**
  24. * @var string
  25. */
  26. private $content;
  27. /**
  28. * @var PUM_Utils_Logging
  29. */
  30. public static $instance;
  31. /**
  32. * @return PUM_Utils_Logging
  33. */
  34. public static function instance() {
  35. if ( ! isset( self::$instance ) ) {
  36. self::$instance = new self();
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * Get things started
  42. */
  43. public function __construct() {
  44. $this->init();
  45. }
  46. /**
  47. * Get things started
  48. */
  49. public function init() {
  50. $upload_dir = wp_upload_dir( null, false );
  51. $this->filename = 'pum-debug.log';
  52. $this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename;
  53. if ( ! is_writeable( $upload_dir['basedir'] ) ) {
  54. $this->is_writable = false;
  55. }
  56. // Truncate long log files.
  57. if ( file_exists( $this->file ) && filesize( $this->file ) >= 1048576 ) {
  58. $this->truncate_log();
  59. }
  60. }
  61. /**
  62. * Retrieves the url to the file
  63. *
  64. * @returns string
  65. * @since 1.12.0
  66. */
  67. public function get_file_url() {
  68. return PUM_Helpers::get_upload_dir_url( $this->filename );
  69. }
  70. /**
  71. * Retrieve the log data
  72. *
  73. * @return string
  74. */
  75. public function get_log() {
  76. return $this->get_file();
  77. }
  78. /**
  79. * Log message to file
  80. *
  81. * @param string $message
  82. */
  83. public function log( $message = '' ) {
  84. $this->write_to_log( date( 'Y-n-d H:i:s' ) . ' - ' . $message );
  85. }
  86. /**
  87. * Log unique message to file.
  88. *
  89. * @param string $message
  90. */
  91. public function log_unique( $message = '' ) {
  92. $contents = $this->get_file();
  93. if ( strpos( $contents, $message ) !== false ) {
  94. return;
  95. }
  96. $this->log( $message );
  97. }
  98. /**
  99. * Retrieve the file data is written to
  100. *
  101. * @return string
  102. */
  103. protected function get_file() {
  104. if ( ! isset( $this->content ) ) {
  105. $this->content = '';
  106. if ( @file_exists( $this->file ) ) {
  107. if ( ! is_writeable( $this->file ) ) {
  108. $this->is_writable = false;
  109. }
  110. $this->content = @file_get_contents( $this->file );
  111. } else {
  112. @file_put_contents( $this->file, '' );
  113. @chmod( $this->file, 0664 );
  114. }
  115. }
  116. return $this->content;
  117. }
  118. /**
  119. * Write the log message
  120. *
  121. * @param string $message
  122. */
  123. protected function write_to_log( $message = '' ) {
  124. // Disable logging by adding define( 'PUM_DISABLE_LOGGING', true );
  125. if ( defined( 'PUM_DISABLE_LOGGING' ) && PUM_DISABLE_LOGGING ) {
  126. return;
  127. }
  128. $file_contents = $this->get_file();
  129. $length = strlen( "\r\n" );
  130. // If it doesn't end with a new line, add one.
  131. if ( substr( $file_contents, - $length ) !== "\r\n" ) {
  132. $file_contents .= "\r\n";
  133. }
  134. $file_contents .= $message;
  135. $this->content = $file_contents;
  136. $this->save_logs();
  137. }
  138. /**
  139. * Save the current contents to file.
  140. */
  141. public function save_logs() {
  142. @file_put_contents( $this->file, $this->content );
  143. }
  144. /**
  145. * Get a line count.
  146. *
  147. * @return int
  148. */
  149. public function count_lines() {
  150. $file = $this->get_file();
  151. $lines = explode( "\r\n", $file );
  152. return count( $lines );
  153. }
  154. /**
  155. * Truncates a log file to maximum of 250 lines.
  156. */
  157. public function truncate_log() {
  158. $content = $this->get_file();
  159. $lines = explode( "\r\n", $content );
  160. $lines = array_slice( $lines, 0, 250 ); //50 is how many lines you want to keep
  161. $this->content = implode( "\r\n", $lines );
  162. $this->save_logs();
  163. }
  164. /**
  165. * Delete the log file.
  166. */
  167. public function clear_log() {
  168. @unlink( $this->file );
  169. }
  170. /**
  171. * @param $function
  172. * @param $version
  173. * @param null $replacement
  174. */
  175. public function log_deprecated_notice( $function, $version, $replacement = null ) {
  176. if ( ! is_null( $replacement ) ) {
  177. $notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement );
  178. } else {
  179. $notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version );
  180. }
  181. $this->log_unique( $notice );
  182. }
  183. }