The code works, but it does only write data once and then closes the output-stream. So any data beeing recieved after that point wont be in your logfile. Try to close the output-stream on keypressed or exit maybe. Something like this:
- void exit() {
output.close();
}