Initial commit
This commit is contained in:
49
image_files/fsroot/usr/local/bin/script_utils.sh
Normal file
49
image_files/fsroot/usr/local/bin/script_utils.sh
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Utility functions for shell script
|
||||
|
||||
|
||||
# Very simple 'logger' for shell script support JSON output
|
||||
# To turn on JSON output, set env LOG_IN_JSON_FMT=1
|
||||
# ---------------------
|
||||
logging_info () {
|
||||
msg="${1}"
|
||||
logging_msg "INFO" "\${msg}"
|
||||
}
|
||||
|
||||
logging_warning () {
|
||||
msg="${1}"
|
||||
logging_msg "WARNING" "\${msg}"
|
||||
}
|
||||
|
||||
logging_error () {
|
||||
msg="${1}"
|
||||
logging_msg "ERROR" "\${msg}"
|
||||
}
|
||||
|
||||
logging_msg () {
|
||||
eval level="${1}"
|
||||
eval msg="${2}"
|
||||
#TODO, handle 4 env or detect if it is k8s
|
||||
if [ ! -z "$LOG_IN_JSON_FMT" ]; then
|
||||
echo_json_log "INFO" "\${msg}"
|
||||
else
|
||||
# echo "$ENV - $msg"
|
||||
echo "$msg"
|
||||
fi
|
||||
}
|
||||
|
||||
echo_json_log () {
|
||||
eval level="$1"
|
||||
eval msg="${2}"
|
||||
|
||||
# Handle double quote
|
||||
msg="${msg//[\"]/\\\"}"
|
||||
# Handle mutliline
|
||||
msg=$(echo "$msg" | sed -e '1h;2,$H;$!d;g' -re 's/([^\n]*)\n([^\n]*)/\1\\n\2/g' )
|
||||
|
||||
#Replace %N with %3N for milliseconds, %6N for micro-seconds...
|
||||
# now=`date '+%FT%T.%3N%:z'`
|
||||
now=`date '+%FT%T%z'`
|
||||
echo "{\"time\": \"${now}\", \"level\": \"${level}\", \"message\": \"${msg}\"}"
|
||||
}
|
||||
Reference in New Issue
Block a user