As per the anemyte's comment, --line-buffered with grep command fixed my problem. The final command is,
docker logs -f docker_container 2>&1 | grep --line-buffered "KafkaRecordGenerator:197" >> test.txt