Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    Debug WordPress using WP Data Logger

    Debug WordPress using WP Data Logger

    Advanced WordPress systems often use integrations with third-party APIs or sophisticated backend transactions. The system works fine most of the time, but it might happen that every X transactions there is an error. These kinds of “random errors” are difficult to debug. They may be caused by non-ordinary function parameters values or the specific setup of environment variables.

    Everything works on your localhost, but on production the function is causing problems? Use the WP Data Logger plugin.

    Simple logger

    Logger U7 is a simple tool for debugging things in WordPress – https://github.com/hokoo/logger-u7 . It creates its own database table, so it doesn’t mess with core WordPress tables. Determine places in your code that are crucial (like saving data from API response) and log proper values using:

    do_action( 'logger', $data, 'status1_code' );

    A proper DB record will be created, for later review. The last parameter is the status, you can treat it as a “tag” and use it to separate different kinds of events in your application.

    Logs preview

    All records will be visible in the wp-admin / Tools / Logger backend section. For me personally, a more convenient way is to connect MYSQL to production and look at the data directly in the wp_data_logger table. You can use different SQL filtering to see only the data of your interest. For example:

    # look for specific data response
    SELECT * FROM wp_data_logger WHERE content LIKE '%part_of_response_here%' ORDER BY ID desc
    # look for logs with specific tag/status, newest at the top
    SELECT * FROM wp_data_logger WHERE status LIKE '%status1_code%' ORDER BY ID desc

    Real life example

    We’re displaying a counter that is fetched from API. The counter value is saved locally in postmeta. We’re going to update data every 1 hour (on page request we’re checking if the timestamp expired). On data expiration, we’re using the logger to save both values: our local counter and a counter from API. This will help later for data comparison.

    /**
     * Display counter, data fetched from external API
     * Counter value saved in postmeta
     * Refresh data from API - every 1 hour
     */
    $metaKey = 'ct_mycounter_' . $post_id . '_' . $id;
    $counterData = get_post_meta( $post_id, $metaKey );
    $counterData = is_array( $counterData ) ? $counterData[ 0 ] : $counterData;
    if ( ! isset($counterData) || $counterData[ 'expires' ] <= time() ) {
        $counter = $this->getCounterFromAPI( $id );
        // debug
        do_action( 'logger', $metaKey . ' | ' . $counterData['counter'] . ' @ ' . $counter, 'status1_code' );
        $counterDataNew = [
            'expires' => time() + (HOUR_IN_SECONDS),
            'counter' => $counter,
        ];
        update_post_meta( $post_id, $metaKey, $counterDataNew);
    } else {
        $counter = $counterData['counter'];
    }
    //if counter still zero - show nothing
    if ( 0 === (int)$counter ) {
        return;
    }
    echo '<div class="progress-bar">'. $counter . '</div>';

    To make things more interesting, we also have Ajax Form. On every submit, the local counter is increased and API is notified about a new submission.

    /**
     * Ajax form submit
     * notify API about submit
     * increase counter action in DB
     */
    $this->notifyAPI();
    // update local counter
    $metaKey = 'ct_k1_' . $post_id . '_' . $id;
    $counterData = get_post_meta( $post_id, $metaKey );
    $counterData = is_array( $counterData ) ? $counterData[ 0 ] : $counterData;
    if(isset($counterData['counter'])){
        $counterData['counter'] = intval($counterData['counter']) + 1;
        update_post_meta( $post_id, $metaKey, $counterData );
    }

    Every 1 hour, our local counter will be “replaced” by a proper counter from API. In theory, those values should be equal. Having wp_data_logger records, we can debug if values are properly saved and increased.

    Disabling Prod Logging

    Let’s say our work is finished, we’ve found the issue and fixed the code. The good news is that we don’t have to remove our do_action(“logger”) from the code. Just deactivate the WordPress plugin and the system will work without an interruption.

    That’s it for today’s tutorial. Make sure to follow us for other useful tips and guidelines.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    A game changer for WooCommerce users
    • Our Highlights

    A game changer for WooCommerce users

    March 24, 2023 by createIT
    The differences between a web app and a mobile app
    • Services
    • Technology
    • Trends

    The differences between a web app and a mobile app

    March 7, 2023 by createIT
    Webrooming and showrooming
    • Trends

    Webrooming and showrooming

    February 14, 2023 by createIT
    PHPStorm – fix long load time of a directory window
    • Dev Tips and Tricks

    PHPStorm – fix long load time of a directory window

    January 20, 2023 by createIT
    reCAPTCHA v3 – WordPress implementation
    • Dev Tips and Tricks

    reCAPTCHA v3 – WordPress implementation

    January 20, 2023 by createIT
    How to compare GIT and server files
    • Dev Tips and Tricks

    How to compare GIT and server files

    January 19, 2023 by createIT
    How to trigger a click event inside iframe?
    • Dev Tips and Tricks

    How to trigger a click event inside iframe?

    January 19, 2023 by createIT
    FOOEvents – generate a custom CSV report
    • Dev Tips and Tricks

    FOOEvents – generate a custom CSV report

    January 19, 2023 by createIT
    Headless chrome – testing webgl using playwright
    • Dev Tips and Tricks

    Headless chrome – testing webgl using playwright

    January 18, 2023 by createIT
    Preview big SQL files with PilotEdit
    • Dev Tips and Tricks

    Preview big SQL files with PilotEdit

    January 18, 2023 by createIT

    Technology
    Be on the same page as the rest of the industry.

    Contact us