Make WordPress Core

Changeset 45635


Ignore:
Timestamp:
07/15/2019 04:06:24 AM (5 years ago)
Author:
pento
Message:

WPDB: Allow custom data to be added to logged queries.

This adds a new method, wpdb::log_query(), and a new filter, log_query_custom_data. The custom data is stored as a new element in each entry of the wpdb::$queries array.

Props CrazyJaco, johnbillion, pento.
Fixes 42151.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r45630 r45635  
    181181     * @since 2.5.0 The third element in each query log was added to record the calling functions.
    182182     * @since 5.1.0 The fourth element in each query log was added to record the start time.
     183     * @since 5.3.0 The fifth element in each query log was added to record custom data.
    183184     *
    184185     * @var array[] {
     
    192193     *         @type string $2 Comma separated list of the calling functions.
    193194     *         @type float  $3 Unix timestamp of the time at the start of the query.
     195     *         @type array  $4 Custom query data.
    194196     *     }
    195197     * }
     
    20192021
    20202022        if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
    2021             $this->queries[] = array(
     2023            $this->log_query(
    20222024                $query,
    20232025                $this->timer_stop(),
    20242026                $this->get_caller(),
    20252027                $this->time_start,
     2028                array()
    20262029            );
    20272030        }
     2031    }
     2032
     2033    /**
     2034     * Logs query data.
     2035     *
     2036     * @since 5.3.0
     2037     *
     2038     * @param string $query           The query's SQL.
     2039     * @param float  $query_time      Total time spent on the query, in seconds.
     2040     * @param string $query_callstack Comma separated list of the calling functions.
     2041     * @param float  $query_start     Unix timestamp of the time at the start of the query.
     2042     * @param array  $query_data      Custom query data.
     2043     * }
     2044     */
     2045    public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) {
     2046        /**
     2047         * Filters the custom query data being logged.
     2048         *
     2049         * Caution should be used when modifying any of this data, it is recommended that any additional
     2050         * information you need to store about a query be added as a new associative entry to the fourth
     2051         * element $query_data.
     2052         *
     2053         * @since 5.3.0
     2054         *
     2055         * @param array  $query_data      Custom query data.
     2056         * @param string $query           The query's SQL.
     2057         * @param float  $query_time      Total time spent on the query, in seconds.
     2058         * @param string $query_callstack Comma separated list of the calling functions.
     2059         * @param float  $query_start     Unix timestamp of the time at the start of the query.
     2060         */
     2061        $query_data = apply_filters( 'log_query_custom_data', $query_data, $query, $query_time, $query_callstack, $query_start );
     2062
     2063        $this->queries[] = array(
     2064            $query,
     2065            $query_time,
     2066            $query_callstack,
     2067            $query_start,
     2068            $query_data,
     2069        );
    20282070    }
    20292071
Note: See TracChangeset for help on using the changeset viewer.