Logging functions
The pb_ functions you call from your own plugin or theme, with their arguments and examples.
On this page
Paul Bunyan gives your code a logger. Messages you log appear in the same timeline as the hooks, queries, outside calls and emails of the request they came from, with levels, components and timings.
pb_info( 'shop', 'Pulled SKUs from supplier', array( 'count' => 412 ) );The levels
Each level has its own function. All take the same three arguments: a component, a message and an optional array of data.
| Function | Level | Use it for |
|---|---|---|
pb_verbose( $component, $message, $data ) | Verbose | Detail you only want while debugging. Dropped unless the minimum level is Verbose. |
pb_info( $component, $message, $data ) | Info | Things that happened: a batch created, a sync finished. |
pb_warn( $component, $message, $data ) | Warning | Something unexpected that the code recovered from. |
pb_error( $component, $message, $data ) | Error | Something failed. |
pb_bug( $component, $message, $data ) | Error | Something that should never happen. Always recorded, whatever the minimum level. |
pb_log( $level, $component, $message, $data ) | Any | When the level is decided at run time. |
The arguments
$component: a short name for the part of your code, such asshop,syncorraffle. You can filter the Messages screen by it.$message: what happened, in plain words. Keep it the same each time and put the changing parts in$data, so identical messages group together.$data: an optional array of values. It is stored with the message and shown when you open it.
Example
function my_sync_inventory() {
$items = my_fetch_supplier_items();
if ( is_wp_error( $items ) ) {
pb_error( 'sync', 'Supplier feed failed', array( 'error' => $items->get_error_message() ) );
return;
}
pb_info( 'sync', 'Supplier feed loaded', array( 'items' => count( $items ) ) );
$missing = my_count_missing_prices( $items );
if ( $missing ) {
pb_warn( 'sync', 'Items missing a price', array( 'count' => $missing ) );
}
}Keep personal data out
Log counts, ids and states, not names, email addresses, keys or tokens. Paul Bunyan masks email addresses and redacts keys that look like secrets, but the safest message is one that never contained them.
Logging inside a loop
A request holds up to 1,000 messages; past that the rest are dropped (PB-1006). Log a count once after the loop, or use pb_verbose() inside it.
If your code must run without Paul Bunyan
These functions exist only while Paul Bunyan is active. For a plugin you ship to other people, use the action instead; see Log without depending on Paul Bunyan.
