doesCommandExist( 'tail' ) ) { try { shell_exec( sprintf( 'tail -n %d %s > debug.log.tmp', escapeshellarg( $amountOfLines ), escapeshellarg( $debugLogPath ) ) ); unlink( $debugLogPath ); rename( WP_CONTENT_DIR . '/debug.log.tmp', $debugLogPath ); } catch( \Exception $e ) { // Do nothing. } } // If the filesize is still too big, we use a PHP approach. We'll stream the contents and keep the last 1.000 lines. $fileSize = filesize( $debugLogPath ); if ( $fileSize < $fileSizeTreshold ) { return; } $fp = fopen( $debugLogPath, 'r' ); // Initialize the circular buffer. $buffer = array_fill( 0, $amountOfLines, '' ); $bufferIndex = 0; // Iterate over each line. while ( $line = fgets( $fp ) ) { // Add the line to the buffer. $buffer[ $bufferIndex ] = $line; // Increment the buffer index. $bufferIndex = ( $bufferIndex + 1 ) % 1000; } fclose( $fp ); // Write the last 1000 lines to the file. file_put_contents( $debugLogPath, implode( '', $buffer ) ); } /** * Checks if a command exists. * * @since 1.0.0 */ private function doesCommandExist( string $commandExist ) : bool { $return = shell_exec( sprintf( "which %s", escapeshellarg( $commandExist ) )) ; return ! empty( $return ); } }