Prefix stderr redirect with timestamp

foo() { while IFS='' read -r line; do echo "$(date) $line" >> file.txt; done; };

It creates a function which reads one line of raw (-r) input from stdin. IFS='' prevents ignoring blank lines. It, then, prepends each line with a timestamp.

Test it

echo 42 > >(foo)

or with tee

echo 42 | tee >(foo)