feat(logger): Update JSON logger
Require message, level, and status. Reorganize JSON output to mirror modified syslog-style structure.
This commit is contained in:
@@ -24,30 +24,26 @@ writePlainTextLog() {
|
||||
"$2" "$3" "$1" "${4:UNSPECIFIED}" "${5:999}"
|
||||
}
|
||||
|
||||
# $1: message, $2: level, $3: status, $4: operation, $5: code
|
||||
# $1: message, $2: level, $3: status, [$4: operation], [$5: code]
|
||||
writeJsonLog() {
|
||||
if [ -z "$1" ]; then
|
||||
PROPS="$(
|
||||
jq --null-input \
|
||||
--arg level "err" \
|
||||
--arg pName "$LOG_PROGRAM_NAME" \
|
||||
--arg operation "$LOG_DEFAULT_OPERATION" \
|
||||
--arg status "$LOG_DEFAULT_STATUS" \
|
||||
--arg code "$LOG_DEFAULT_CODE" \
|
||||
--arg message "$LOG_DEFAULT_MESSAGE" \
|
||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
||||
)"
|
||||
else
|
||||
PROPS="$(
|
||||
jq --null-input \
|
||||
--arg level "${2:-$LOG_DEFAULT_LEVEL}" \
|
||||
--arg pName "$LOG_PROGRAM_NAME" \
|
||||
--arg status "$(uppercase "${3:-$LOG_DEFAULT_STATUS}")" \
|
||||
--arg operation "${4:-$LOG_DEFAULT_OPERATION}" \
|
||||
--arg code "${5:-$LOG_DEFAULT_CODE}" \
|
||||
--arg message "$1" \
|
||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
||||
)"
|
||||
# not enough information to generate a meaningful log message
|
||||
if [ -z "$1" ] || [ $# -lt 3 ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
PROPS="$(
|
||||
jq --null-input \
|
||||
--arg timestamp "$(getTimeStamp)" \
|
||||
--arg hostname "$(getHostname)" \
|
||||
--arg pName "$LOG_PROGRAM_NAME" \
|
||||
--arg pid = "$$" \
|
||||
--arg level "$2" \
|
||||
--arg status "$(uppercase "$3")" \
|
||||
--arg message "$1" \
|
||||
--arg operation "${4:UNSPECIFIED}" \
|
||||
--arg code "${5:999}" \
|
||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
||||
)"
|
||||
fi
|
||||
|
||||
echo "$PROPS" | jq "."
|
||||
|
||||
Reference in New Issue
Block a user