Making WordPress.org


Ignore:
Timestamp:
01/25/2017 07:13:49 PM (9 years ago)
Author:
iandunn
Message:

WordCamp Logger: Add user and request context to log entries

File:
1 edited

Legend:

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

    r4595 r4785  
    1111 * passing the data to this function, though.
    1212 *
    13  * @todo add current username and request url to every entry, and update wp-cli command parsing regex to match
    1413 * @todo add a $type param with 'error' and 'info' values. errors continue to go to std error log. info is logged
    1514 *       to separate file, so they don't clutter error log. need to rotate that file. then update callers to use
     
    2221    $backtrace = debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, 2 );
    2322
    24     if ( $data && ! is_scalar( $data ) ) {
    25         $data = (array) $data;
    26         redact_keys( $data );
    27         $data = str_replace( "\n", '[newline]', wp_json_encode( $data ) );
     23    if ( 'cli' === php_sapi_name() ) {
     24        $data['command'] = sprintf( '%s %s', $_SERVER['_'], implode( ' ', $_SERVER['argv'] ) );
     25    } else {
     26        $data['request_url'] = sprintf( '%s://%s%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI'] );
     27
     28        // Fall back to IP address if it's too early to detect the user
     29        if ( did_action( 'after_setup_theme' ) > 0 ) {
     30            $data['current_user'] = get_current_user_id();
     31        } else {
     32            $data['remote_ip'] = $_SERVER['REMOTE_ADDR'];
     33        }
    2834    }
     35
     36    redact_keys( $data );
     37    $data = str_replace( "\n", '[newline]', wp_json_encode( $data ) );
    2938
    3039    $log_entry = sprintf(
     
    129138    foreach ( $raw_entries as $entry ) {
    130139        $is_native_entry = 1 === preg_match( $native_entry_pattern, $entry, $entry_parts );
     140        // todo bug: this should be recognized as native -- [18-Jan-2017 00:33:04 UTC] [b1955769] source-site-id-setting.php:29 - preview:post_val -- [null]
    131141
    132142        if ( $is_native_entry ) {
Note: See TracChangeset for help on using the changeset viewer.