diff --git a/includes/log-handlers/class-wc-log-handler-db.php b/includes/log-handlers/class-wc-log-handler-db.php new file mode 100644 index 00000000000..c7f7267be00 --- /dev/null +++ b/includes/log-handlers/class-wc-log-handler-db.php @@ -0,0 +1,85 @@ +should_handle( $level ) ) { + return true; + } + + if ( isset( $context['tag'] ) && $context['tag'] ) { + $tag = $context['tag']; + } else { + $tag = ''; + } + + // Bubble if add is NOT successful + return ! $this->add( $timestamp, $level, $message, $tag, $context ); + } + + /** + * Add a log entry to chosen file. + * + * @param int $timestamp Log timestamp. + * @param string $level emergency|alert|critical|error|warning|notice|info|debug + * @param string $message Log message. + * @param string $tag Log tag. Useful for filtering and sorting. + * @param array $context { + * Context will be serialized and stored in database. + * } + * + * @return bool True if write was successful. + */ + public function add( $timestamp, $level, $message, $tag, $context ) { + global $wpdb; + + $insert = array( + 'timestamp' => date( 'Y-m-d H:i:s', $timestamp ), + 'level' => $level, + 'message' => $message, + 'tag' => $tag, + ); + + if ( ! empty( $context ) ) { + $insert['context'] = serialize( $context ); + } + + $result = $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert ); + + var_dump( $result ); + + return false !== $result; + } + +}