Last active
April 29, 2023 13:22
-
-
Save reduardo7/282a2dd0445645ee7cd78e092a203f91 to your computer and use it in GitHub Desktop.
Bash output prefix and redirect to a file with timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[ -f test.log ] && rm -f test.log | |
{ | |
{ | |
echo foo 1 | |
sleep 1 | |
echo bar 1 >&2 | |
echo foo 2 | |
sleep 1 | |
echo bar 2 >&2 | |
} \ | |
2> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [ERR] /" >&2) \ | |
> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [INF] /") \ | |
2>&1 | |
} >>test.log | |
echo "-------------------" | |
cat test.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment