WC_Log_Handler_DB

Handles log entries by writing to database.

Defined (1)

The class is defined in the following location(s).

/includes/log-handlers/class-wc-log-handler-db.php  
  1. class WC_Log_Handler_DB extends WC_Log_Handler { 
  2.  
  3. /** 
  4. * Handle a log entry. 
  5. * @param int $timestamp Log timestamp. 
  6. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  7. * @param string $message Log message. 
  8. * @param array $context { 
  9. * Additional information for log handlers. 
  10. * @type string $source Optional. Source will be available in log table. 
  11. * If no source is provided, attempt to provide sensible default. 
  12. * } 
  13. * @see WC_Log_Handler_DB::get_log_source() for default source. 
  14. * @return bool False if value was not handled and true if value was handled. 
  15. */ 
  16. public function handle( $timestamp, $level, $message, $context ) { 
  17.  
  18. if ( isset( $context['source'] ) && $context['source'] ) { 
  19. $source = $context['source']; 
  20. } else { 
  21. $source = $this->get_log_source(); 
  22.  
  23. return $this->add( $timestamp, $level, $message, $source, $context ); 
  24.  
  25. /** 
  26. * Add a log entry to chosen file. 
  27. * @param int $timestamp Log timestamp. 
  28. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  29. * @param string $message Log message. 
  30. * @param string $source Log source. Useful for filtering and sorting. 
  31. * @param array $context { 
  32. * Context will be serialized and stored in database. 
  33. * } 
  34. * @return bool True if write was successful. 
  35. */ 
  36. protected static function add( $timestamp, $level, $message, $source, $context ) { 
  37. global $wpdb; 
  38.  
  39. $insert = array( 
  40. 'timestamp' => date( 'Y-m-d H:i:s', $timestamp ),  
  41. 'level' => WC_Log_Levels::get_level_severity( $level ),  
  42. 'message' => $message,  
  43. 'source' => $source,  
  44. ); 
  45.  
  46. $format = array( 
  47. '%s',  
  48. '%d',  
  49. '%s',  
  50. '%s',  
  51. '%s', // possible serialized context 
  52. ); 
  53.  
  54. if ( ! empty( $context ) ) { 
  55. $insert['context'] = serialize( $context ); 
  56.  
  57. return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert, $format ); 
  58.  
  59. /** 
  60. * Clear all logs from the DB. 
  61. * @return bool True if flush was successful. 
  62. */ 
  63. public static function flush() { 
  64. global $wpdb; 
  65.  
  66. return $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_log" ); 
  67.  
  68. /** 
  69. * Delete selected logs from DB. 
  70. * @param int|string|array Log ID or array of Log IDs to be deleted. 
  71. * @return bool 
  72. */ 
  73. public static function delete( $log_ids ) { 
  74. global $wpdb; 
  75.  
  76. if ( ! is_array( $log_ids ) ) { 
  77. $log_ids = array( $log_ids ); 
  78.  
  79. $format = array_fill( 0, count( $log_ids ), '%d' ); 
  80.  
  81. $query_in = '(' . implode( ', ', $format ) . ')'; 
  82.  
  83. $query = $wpdb->prepare( 
  84. "DELETE FROM {$wpdb->prefix}woocommerce_log WHERE log_id IN {$query_in}",  
  85. $log_ids 
  86. ); 
  87.  
  88. return $wpdb->query( $query ); 
  89.  
  90. /** 
  91. * Get appropriate source based on file name. 
  92. * Try to provide an appropriate source in case none is provided. 
  93. * @return string Text to use as log source. "" (empty string) if none is found. 
  94. */ 
  95. protected static function get_log_source() { 
  96. static $ignore_files = array( 'class-wc-log-handler-db', 'class-wc-logger' ); 
  97.  
  98. /** 
  99. * PHP < 5.3.6 correct behavior 
  100. * @see http://php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters 
  101. */ 
  102. if ( defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' ) ) { 
  103. $debug_backtrace_arg = DEBUG_BACKTRACE_IGNORE_ARGS; 
  104. } else { 
  105. $debug_backtrace_arg = false; 
  106.  
  107. $trace = debug_backtrace( $debug_backtrace_arg ); 
  108. foreach ( $trace as $t ) { 
  109. if ( isset( $t['file'] ) ) { 
  110. $filename = pathinfo( $t['file'], PATHINFO_FILENAME ); 
  111. if ( ! in_array( $filename, $ignore_files ) ) { 
  112. return $filename; 
  113.  
  114. return ''; 
  115.