Making WordPress.org


Ignore:
Timestamp:
05/30/2016 06:15:50 PM (9 years ago)
Author:
iandunn
Message:

WordCamp WP-CLI: Add a command to format our custom log entries.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-logger.php

    r3156 r3273  
    2525        $data = (array) $data;
    2626        redact_keys( $data );
    27         $data = str_replace( "\n", '[newline]', serialize( $data ) );
     27        $data = str_replace( "\n", '[newline]', wp_json_encode( $data ) );
    2828    }
    2929
     
    3434        $backtrace[1]['function'],
    3535        $error_code,
    36         $data ? ' - ' . $data : ''
     36        $data ? ' -- ' . $data : ''
    3737    );
    3838
     
    6767    return $data;
    6868}
     69
     70/**
     71 * Format entries created with log()
     72 *
     73 * See WordCamp_CLI_Miscellaneous::format_log() for usage instructions.
     74 *
     75 * @param string $raw_log
     76 * @param string $foreign_entries `ignore` or `include` entries that weren't created with log()
     77 *
     78 * @return string
     79 */
     80function format_log( $raw_log, $foreign_entries = 'include' ) {
     81    $formatted_log        = '';
     82    $raw_entries          = explode( "\n", $raw_log );
     83    $native_entry_pattern = '/(\[.*?\]) (.*?:.*?) - (.*?) -- (\{.*\})/';
     84
     85    foreach ( $raw_entries as $entry ) {
     86        $is_native_entry = 1 === preg_match( $native_entry_pattern, $entry, $entry_parts );
     87
     88        if ( $is_native_entry ) {
     89            $formatted_log .= sprintf(
     90                "\n%s %s - %s\n%s",
     91                $entry_parts[1],
     92                $entry_parts[2],
     93                $entry_parts[3],
     94                print_r( json_decode( $entry_parts[4] ), true )
     95            );
     96        } else {
     97            if ( 'ignore' !== $foreign_entries ) {
     98                $formatted_log .= $entry . "\n";
     99            }
     100        }
     101    }
     102
     103    return $formatted_log;
     104}
Note: See TracChangeset for help on using the changeset viewer.