Dependencies: "moreutils" should be installed on the os. use brew/apt-get/wget/yum install moreutils depends on you os. Add the code below to .sh file.
# Optional:
# exec 1<&- # Close STDOUT file descriptor
# exec 2<&- # Close STDERR FD
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
LOG_FILE=$parent_path'/err.log' #log file name
exec 1>> $LOG_FILE # Open STDOUT as $LOG_FILE file for read and write.
exec 2>&1 # Redirect STDERR to STDOUT
echo | ts '[%Y-%m-%d %H:%M:%S]'
# example:
echo "Time Stamp should be above this line"
cat nofile.example