Save command output to a file
When running long-running custom WP-CLI commands, or commands that will affect many records, it is recommended to save the output to a file for review.
Once the output for a natively supported WP-CLI command or a custom command is more than 100 lines of information, and potentially impacting dozens or more records in a database, having a log to audit what changes occurred, and what succeeded or failed, can be a critical step in debugging a root cause for any unexpected side effects.
Output can be written to the console using helpers like WP_CLI::line()
and WP_CLI::log()
, then redirected to a file.
Using tee
to view and save output
A common option is to pipe (|
) output to tee
so that responses can be logged while also being able to view them in the terminal shell to monitor progress. By design, this will only write the STDOUT
. The output from WP_CLI::warning()
, WP_CLI::debug()
, WP_CLI::error()
, and WP_CLI::error_multi_line()
all write to STDERR
, which will display on the screen but will not be recorded into the log file.
To ensure that complete output from the command is logged, both STDERR
and STDOUT
should be redirected to the same place using 2>&1
. This will ensure that all of the output seen in the terminal shell will be recorded to the log.
Write STDOUT
to a file, and show both STDOUT
and STDERR
in the terminal window
# Post ID 64791 does not exist, which will trigger a WP_CLI::warning(). $ wp demo 64783 64791 65384 | tee output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). Warning: Post not found (ID 64791). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command. $ cat output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command.
Write STDOUT
and STDERR
to a file, and show both STDOUT
and STDERR
in the terminal window
# Post ID 64791 does not exist, which will trigger a WP_CLI::warning(). $ wp demo 64783 64791 65384 2>&1 | tee all-output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). Warning: Post not found (ID 64791). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command. $ cat all-output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). Warning: Post not found (ID 64791). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command.
Using >
to only write to a log file
If an output display in the terminal is not required while the command is running, the redirection operator (>
) can be used. By default, the redirection operator will write all STDOUT
to the file and output the STDERR
to the terminal shell.
Alternatively the STDERR
and STDOUT
can be combined to the same file using 2>&1
or redirect each output to its own file.
Write STDOUT
to file, and show STDERR
in the terminal window
# Post ID 64791 does not exist, which will trigger a WP_CLI::warning(). $ wp demo 64783 64791 65384 > output.log Warning: Post not found (ID 64791). $ cat output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command.
Write STDOUT
to one file, STDERR
to another file, with nothing showing in the terminal window
# Post ID 64791 does not exist, which will trigger a WP_CLI::warning(). $ wp demo 64783 64791 65384 > output.log 2>error.log $ cat output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command. $ cat error.log Warning: Post not found (ID 64791).
Write both STDOUT
and STDERR
to a file, with nothing showing in the terminal window
# Post ID 64791 does not exist, which will trigger a WP_CLI::warning(). $ wp demo 64783 64791 65384 > output.log 2>&1 $ cat output.log 3 args provided. 1/3 Post found "New Product Hero Image" (ID 65384). Warning: Post not found (ID 64791). 3/3 Post found "Lab Testing Samples" (ID 64783). Success: Completed command.
Last updated: May 13, 2024