Making WordPress.org

Changeset 3273


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

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

Location:
sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins
Files:
2 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}
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wp-cli-commands/miscellaneous.php

    r2419 r3273  
    9090        }
    9191    }
     92
     93    /**
     94     * Print a log with our custom entries formatted for humans
     95     *
     96     * ## OPTIONS
     97     *
     98     * <raw_log>
     99     * : The raw log contents, or the filename of the raw log
     100     *
     101     * [--foreign=<action>]
     102     * : Include foreign log entries from the output, or ignore them
     103     * ---
     104     * default: include
     105     * options:
     106     *   - include
     107     *   - ignore
     108     * ---
     109     *
     110     * ## EXAMPLES
     111     *
     112     * wp wc-misc format-log /var/log/php-errors.log
     113     * wp wc-misc format-log "$(grep 'foo' /var/log/php-errors.log -A 10 -B 10)" |less -S
     114     * wp wc-misc format-log "$(grep 'bar' /var/log/php-errors.log)" --foreign=ignore
     115     *
     116     * @subcommand format-log
     117     *
     118     * @param array $args
     119     * @param array $assoc_args
     120     */
     121    public function format_log( $args, $assoc_args ) {
     122        list( $raw_log ) = $args;
     123
     124        if ( is_file( $raw_log ) ) {
     125            $raw_log = file_get_contents( $raw_log );
     126        }
     127
     128        $formatted_log = \WordCamp\Logger\format_log( $raw_log, $assoc_args['foreign'] );
     129
     130        WP_CLI::line( "\n" . $formatted_log );
     131    }
    92132}
Note: See TracChangeset for help on using the changeset viewer.