diff --git a/cfddns.sh b/cfddns.sh index 2dcb284..c43ba68 100644 --- a/cfddns.sh +++ b/cfddns.sh @@ -113,6 +113,31 @@ standardizeLogLevels() { 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: [pid]: [LEVEL:$2] [STATUS:$3] (:) writePlainTextLog() { # not enough information to generate a meaningful log message @@ -127,13 +152,13 @@ writePlainTextLog() { } writeLog() { - ([ "$logToNone" ]) && return + ([ "$logToNone" -eq 1 ]) && return PT_LOG_LEVEL="$(standardizeLogLevels "$2")" ([ "$logDebug" -eq 0 ] && [ "$PT_LOG_LEVEL" = "DEBUG" ]) && return - ([ "$logToJournal" ]) && return # TODO: replace this with function when implemented - ([ "$useJsonLogFmt" -eq 1 ]) && return # TODO: replace this with function when implemented + ([ "$logToJournal" -eq 1 ]) && return # TODO: replace this with function when implemented + ([ "$useJsonLogFmt" -eq 1 ]) && writeJsonLog "$@" ([ "$useSyslogLogFmt" -eq 1 ]) && writePlainTextLog "$@" }