feat: Add log formating ability

This commit is contained in:
ge 2022-05-15 02:09:27 +03:00
parent d1a5eefb7a
commit 444b6df4da

View File

@ -34,6 +34,9 @@ log() {
# If no arguments AND '0' descriptor is closed, then 'message' will not be set.
[[ "$#" == 0 || -t 0 ]] || message="$(cat <&0)"
# Set log date format
[ -n "$log_date_fmt" ] || log_date_fmt='%d/%b/%Y:%H:%M:%S %z'
while (( "$#" )); do
case "$1" in
-p|--print) print=1; shift;;
@ -45,6 +48,6 @@ log() {
[[ "$print" == 1 ]] && echo "$message"
while read line; do
printf '[%s] [%s] %s\n' "$(date +'%d %b %Y %H:%M:%S %z')" "$0" "$line" >> "$log_file"
printf '[%s] [%s] %s\n' "$(date +"$log_date_fmt")" "$0" "$line" >> "$log_file"
done <<< "$(sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g' <<< "$message")"
}