Initial commit

This commit is contained in:
david.hon
2023-04-17 10:20:39 +08:00
parent 164373aa05
commit 8c78c2ad98
54 changed files with 23165 additions and 2 deletions

View 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}\"}"
}