feat(func): Add JSON logging func
Update central logging steering function to reference this new JSON logging backend function.
This commit is contained in:
@@ -113,6 +113,31 @@ standardizeLogLevels() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# $1: message, $2: level, $3: status, [$4: operation], [$5: code]
|
||||||
|
writeJsonLog() {
|
||||||
|
# not enough information to generate a meaningful log message
|
||||||
|
if [ -z "$1" ] || [ $# -lt 3 ]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
JSON_PROPS="$(
|
||||||
|
jq --null-input \
|
||||||
|
--arg timestamp "$(getTimeStamp)" \
|
||||||
|
--arg hostname "$(getHostname)" \
|
||||||
|
--arg pName "$LOG_PROGRAM_NAME" \
|
||||||
|
--arg pid "$$" \
|
||||||
|
--arg level "$(standardizeLogLevels "$2")" \
|
||||||
|
--arg status "$(uppercase "$3")" \
|
||||||
|
--arg message "$1" \
|
||||||
|
--arg operation "${4:-UNSPECIFIED}" \
|
||||||
|
--arg code "${5:-0}" \
|
||||||
|
'{"timestamp":$timestamp, "hostname":$hostname, "programName":$pName, "pid":$pid, "level":$level, "status":$status, "message":$message, "operation":$operation, "code":$code}' \
|
||||||
|
)"
|
||||||
|
|
||||||
|
printf "%s\n" "$JSON_PROPS" >>"$logFile"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
# modified syslog fmt: <ISO-8601 timestamp> <hostname> <tag>[pid]: [LEVEL:$2] [STATUS:$3] <MESSAGE:$1> (<OPERATION:$4>:<CODE:$5>)
|
# modified syslog fmt: <ISO-8601 timestamp> <hostname> <tag>[pid]: [LEVEL:$2] [STATUS:$3] <MESSAGE:$1> (<OPERATION:$4>:<CODE:$5>)
|
||||||
writePlainTextLog() {
|
writePlainTextLog() {
|
||||||
# not enough information to generate a meaningful log message
|
# not enough information to generate a meaningful log message
|
||||||
@@ -127,13 +152,13 @@ writePlainTextLog() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeLog() {
|
writeLog() {
|
||||||
([ "$logToNone" ]) && return
|
([ "$logToNone" -eq 1 ]) && return
|
||||||
|
|
||||||
PT_LOG_LEVEL="$(standardizeLogLevels "$2")"
|
PT_LOG_LEVEL="$(standardizeLogLevels "$2")"
|
||||||
([ "$logDebug" -eq 0 ] && [ "$PT_LOG_LEVEL" = "DEBUG" ]) && return
|
([ "$logDebug" -eq 0 ] && [ "$PT_LOG_LEVEL" = "DEBUG" ]) && return
|
||||||
|
|
||||||
([ "$logToJournal" ]) && return # TODO: replace this with function when implemented
|
([ "$logToJournal" -eq 1 ]) && return # TODO: replace this with function when implemented
|
||||||
([ "$useJsonLogFmt" -eq 1 ]) && return # TODO: replace this with function when implemented
|
([ "$useJsonLogFmt" -eq 1 ]) && writeJsonLog "$@"
|
||||||
([ "$useSyslogLogFmt" -eq 1 ]) && writePlainTextLog "$@"
|
([ "$useSyslogLogFmt" -eq 1 ]) && writePlainTextLog "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user