The problem is you have a foreach loop *inside* a while loop, so for each line of your log file, you're going through your @attributes array and printing the whole lot out $#attributes times.
I think it would work if you just took out the line starting foreach and the corresponding } at the end, though I haven't tried this out.