self::MAX_BYTES ) { @rename( $path, $path . '.1' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.rename_rename } $entry = array( 'time' => time(), 'type' => $type, 'message' => $message, ); if ( $context ) { $entry['context'] = $context; } file_put_contents( $path, wp_json_encode( $entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n", FILE_APPEND | LOCK_EX ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents } /** * Reads the last N lines of a file without loading all of it. * * @param string $path Absolute path. * @param int $lines Lines. * @return string[] */ private static function tail_lines( $path, $lines ) { if ( ! is_readable( $path ) ) { return array(); } $size = filesize( $path ); $read = min( $size, max( 8192, $lines * 400 ) ); $fh = fopen( $path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( ! $fh ) { return array(); } fseek( $fh, $size - $read ); $chunk = (string) fread( $fh, $read ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $all = preg_split( "/\r?\n/", trim( $chunk ) ); if ( $read < $size ) { array_shift( $all ); // First line is probably partial. } return array_slice( array_filter( $all, 'strlen' ), -$lines ); } }