How do I write to a log file, not just print to the serial monitor?
With the arduino there is only one form of hardware serial output so if you want another stream you have to do something different.
You can use a Mega as that has four serial ports or you can use the software serial library to give you another serial output. However you then have to connect this serial output back into your computer. You can't do it over the same USB / serial link as you use for the serial monitor. This means using another USB to TTL serial adaptor, or USB to RS232 adaptor and converting the TTL output of the arduino into RS232 voltage levels with something like a MAX202 chip.