improved logic for extracting log excerpts