cron-command icon indicating copy to clipboard operation
cron-command copied to clipboard

Add debug output for cron event arguments

Open Copilot opened this issue 2 months ago • 3 comments

Plan to add cron arguments into logging as debug output:

  • [x] Understand the current code structure and event object
  • [x] Add WP_CLI::debug() call to log event arguments after successful execution
  • [x] Format arguments appropriately (handle empty arrays, serialize complex data)
  • [x] Add test case to verify debug output with arguments
  • [x] Run code review - no issues found
  • [x] Run security check - no vulnerabilities found
  • [x] Fix PHPCS error (use single quotes)
  • [x] Fix test assertion to match actual debug output format
  • [x] Fix test to check STDERR for debug output

Summary

This PR adds debug output to show cron event arguments when executing cron events with the wp cron event run command. This addresses the feature request to include argument information in logging, particularly useful for events like publish_future_post where seeing the post ID would be helpful for debugging.

Changes Made

  1. Modified src/Cron_Event_Command.php:

    • Added WP_CLI::debug() call after the execution log message
    • Debug output only appears when --debug=cron flag is used
    • Arguments are displayed as JSON using wp_json_encode() for readability
    • Only displays when event has non-empty arguments
    • Fixed PHPCS error by using single quotes instead of double quotes
  2. Added test in features/cron-event.feature:

    • Tests execution with arguments and verifies debug output appears in STDERR
    • Tests execution without arguments and verifies no debug output appears
    • Uses "I try" to properly test STDERR output
    • Ensures backward compatibility

Example Usage

# Schedule an event with arguments
wp cron event schedule my_event now --0=123 --1=my-value

# Run with debug output enabled
wp cron event run my_event --due-now --debug=cron

# Output will include:
# Executed the cron event 'my_event' in 0.001s. (STDOUT)
# Debug: Arguments: ["123","my-value"] (STDERR)

The implementation is minimal, backward compatible, and follows WP-CLI best practices for debug output.

Original prompt

This section details on the original issue you should resolve

<issue_title>Feature request: Add cron arguments into logging</issue_title> <issue_description>Would be great if the Executed the cron event 'x' message included a dump of the arguments passed in. Perhaps as part of a debug group so can be enabled/disabled via the --debug flag?

i.e. this line https://github.com/wp-cli/cron-command/blob/master/src/Cron_Event_Command.php#L262

Personal reasoning is it'd be good to see the post ID for any publish_future_post events, but I'm sure it'd come in handy for others wanting to debug.</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera Yes, sounds like a good change, using debug output for backwards compatibility.

Are you interested in providing a PR for this?</comment_new>

  • Fixes wp-cli/cron-command#41

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 01 '25 19:11 Copilot

@copilot This Behat test is currently failing:

001 Scenario: Run cron event with arguments and debug output                            # features/cron-event.feature:134
      When I run `wp cron event run wp_cli_test_event_with_args --due-now --debug=cron` # features/cron-event.feature:141
        $ wp cron event run wp_cli_test_event_with_args --due-now --debug=cron
        Executed the cron event 'wp_cli_test_event_with_args' in 0.001s.
        Success: Executed a total of 1 cron event.
        
        Debug: Arguments: ["123","test-value"] (0.2s)

Also, there's one PHPCS error: String "Arguments: %s" does not require double quotes; use single quotes instead

swissspidy avatar Nov 01 '25 20:11 swissspidy

@copilot This Behat test is currently failing:

...

Fixed both issues in commit 2077acd:

  • Changed double quotes to single quotes to fix PHPCS error
  • Updated test assertion to match actual debug output format (without (cron) group in parentheses)

Copilot avatar Nov 01 '25 20:11 Copilot

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests.

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Nov 01 '25 21:11 codecov[bot]