time(), 'level' => $level, 'message' => self::truncate( $message, 500 ), ) ); } /** * Debug-level line. * * @param string $message Message. * @param array $context Context. * @return void */ public static function debug( $message, $context = array() ) { self::log( $message, $context, 'debug' ); } /** * Info-level line. * * @param string $message Message. * @param array $context Context. * @return void */ public static function info( $message, $context = array() ) { self::log( $message, $context, 'info' ); } /** * Warning-level line. * * @param string $message Message. * @param array $context Context. * @return void */ public static function warning( $message, $context = array() ) { self::log( $message, $context, 'warning' ); } /** * Error-level line. * * @param string $message Message. * @param array $context Context. * @return void */ public static function error( $message, $context = array() ) { self::log( $message, $context, 'error' ); } /** * The stored ring buffer, newest first. * * @return array */ public static function entries() { $buffer = get_option( self::BUFFER_KEY, array() ); if ( ! is_array( $buffer ) ) { return array(); } return array_reverse( $buffer ); } /** * Empty the ring buffer. * * @return void */ public static function clear() { delete_option( self::BUFFER_KEY ); } /** * Append to the ring buffer. * * @param array $entry Entry. * @return void */ protected static function push_buffer( $entry ) { $buffer = get_option( self::BUFFER_KEY, array() ); if ( ! is_array( $buffer ) ) { $buffer = array(); } $buffer[] = $entry; if ( count( $buffer ) > self::BUFFER_SIZE ) { $buffer = array_slice( $buffer, -self::BUFFER_SIZE ); } if ( false === get_option( self::BUFFER_KEY, false ) ) { add_option( self::BUFFER_KEY, $buffer, '', false ); return; } update_option( self::BUFFER_KEY, $buffer, false ); } /** * Replace the site secret with a marker wherever it appears. * * @param string $text Text. * @return string */ protected static function redact( $text ) { $secret = AdvisorPPC_Options::get_secret(); if ( '' !== $secret ) { $text = str_replace( $secret, '[redacted]', $text ); } return $text; } /** * Redact recursively through an array of context values. * * @param mixed $value Value. * @return mixed */ protected static function redact_deep( $value ) { if ( is_array( $value ) ) { $out = array(); foreach ( $value as $key => $item ) { if ( in_array( $key, array( 'site_secret', 'secret', 'authorization', 'password' ), true ) ) { $out[ $key ] = '[redacted]'; continue; } $out[ $key ] = self::redact_deep( $item ); } return $out; } if ( is_string( $value ) ) { return self::redact( $value ); } return $value; } /** * Hard-limit a string length. * * @param string $text Text. * @param int $limit Maximum characters. * @return string */ protected static function truncate( $text, $limit ) { if ( strlen( $text ) <= $limit ) { return $text; } return substr( $text, 0, $limit ) . '...'; } }