From 444b6df4dabdf0fa39b59771f2b65654249cc247 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 15 May 2022 02:09:27 +0300 Subject: [PATCH] feat: Add log formating ability --- src/lib/common.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/common.sh b/src/lib/common.sh index 89c4e87..0cc8d97 100644 --- a/src/lib/common.sh +++ b/src/lib/common.sh @@ -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")" }