Timestamp whenever an item changes state

Does it have to be in a text file? What’s it for?

You can use Persistence to store the state of a switch.

Or you could use another Item which follows your switch Item, and sets itself to a timestamp of the last update: