blob: 9bb5aa183c4e95c8ec7b56ac61453f8e6a3d9496 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/bin/sh
set -ue
# Wrapper for logging to file and prepending a timestamp.
# By default writes both stdout and stderr to the log file.
usage() {
echo "Usage: logto [-s|-e] <log_file> <command>"
echo "Flags:"
echo " -s: Write only stdout to the log file."
echo " -e: Write only stderr to the log file."
echo ""
echo "Example usage:"
echo " logto /var/log/my.log run some command"
exit 1
}
mode="all"
while getopts "se" opt; do
case $opt in
s) mode="stdout" ;;
e) mode="stderr" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 2 ]; then
usage
fi
log_file="$1"
shift
if [ "$mode" = "stdout" ]; then
out=$(2>/dev/null $@)
elif [ "$mode" = "stderr" ]; then
out=$(2>&1 >/dev/null $@)
else
out=$(2>&1 $@)
fi
if [ ! -z "$out" ]; then
echo "$(date +"%Y-%m-%dT%H:%M:%S%z")" "$out" >>"$log_file"
fi
|