Files
CloudflareDDNS/logger.sh
T
asif 92e69d1b85 fix(logger): Fix JournalD log levels
Log levels were reversed in the translation function.
2026-07-23 00:20:30 -06:00

162 lines
4.8 KiB
Bash

#!/bin/sh
getTimeStamp() {
(date -u +"%Y-%m-%dT%H:%M:%SZ")
}
getHostname() {
(hostname -s)
}
lowercase() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}
uppercase() {
echo "$1" | tr '[:lower:]' '[:upper:]'
}
standardizeLogLevels() {
LEVEL_TO_STANDARDIZE="$(lowercase "$1")"
case "$LEVEL_TO_STANDARDIZE" in
"critical" | "crit" | "fatal")
echo "CRIT"
;;
"error" | "err")
echo "ERR"
;;
"warning" | "warn")
echo "WARN"
;;
"information" | "info")
echo "INFO"
;;
"debug" | "verbose")
echo "DEBUG"
;;
*)
echo "INFO"
;;
esac
}
translateLogLevelsToJournalD() {
LEVEL_TO_TRANSLATE="$(standardizeLogLevels "$1")"
case "$LEVEL_TO_TRANSLATE" in
"CRIT")
echo 2
;;
"ERR")
echo 3
;;
"WARN")
echo 4
;;
"INFO")
echo 6
;;
"DEBUG")
echo 7
;;
*)
echo 6
esac
}
# modified syslog fmt: <ISO-8601 timestamp> <hostname> <tag>[pid]: [LEVEL:$2] [STATUS:$3] <MESSAGE:$1> (<OPERATION:$4>:<CODE:$5>)
writePlainTextLog() {
# not enough information to generate a meaningful log message
if [ -z "$1" ] || [ $# -lt 3 ]; then
return
fi
printf "%s %s %s[%s]: [%s] [%s] %s (%s:%s)\n" \
"$(getTimeStamp)" "$(getHostname)" "$LOG_PROGRAM_NAME" "$$" \
"$(standardizeLogLevels "$2")" "$(uppercase "$3")" "$1" "${4:-UNSPECIFIED}" "${5:-999}"
}
# $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
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:-999}" \
'{"timestamp":$timestamp, "hostname":$hostname, "programName":$pName, "pid":$pid, "level":$level, "status":$status, "message":$message, "operation":$operation, "code":$code}' \
)"
echo "$PROPS" | jq "."
return
}
# $1: message, $2: level, $3: status, $4: operation, $5: code
writeJournalDLog() {
# not enough information to generate a meaningful log message
if [ -z "$1" ] || [ $# -lt 3 ]; then
return
fi
J_TIMESTAMP="$(getTimeStamp)"
J_PID="$$"
J_PRIO="$(translateLogLevelsToJournalD "$2")"
J_STAT="$(uppercase "$3")"
logger --journald <<end
SYSLOG_TIMESTAMP=${J_TIMESTAMP}
SYSLOG_FACILITY=3
SYSLOG_IDENTIFIER=${LOG_PROGRAM_NAME}
SYSLOG_PID=${J_PID}
PRIORITY=${J_PRIO}
STATUS=${J_STAT}
MESSAGE=$1
OPERATION=${4:-UNSPECIFIED}
ERRNO=${5:-999}
end
return
}
LOG_PROGRAM_NAME="cfddns"
if [ "$1" = "text" ]; then
writePlainTextLog # this will do nothing
writePlainTextLog "Testing plain-text logging output" # this will also do nothing
writePlainTextLog "Informational plain-text log message" "information" "ok" # info message
writePlainTextLog "Plain-text message forced to info level" "whoops" "ok" # level translated to default
writePlainTextLog "Warning! This is plain-text." "warning" "flagged" # warning level with 'flagged' status
writePlainTextLog "Error sample! This is plain-text." "err" "error" # error level message
writePlainTextLog "Unable to continue, critical failure. (This is plain-text)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
elif [ "$1" = "json" ]; then
writeJsonLog # this will do nothing
writeJsonLog "This is a test message" # this will also do nothing
writeJsonLog "Informational JSON log message" "information" "ok" # info message
writeJsonLog "JSON message forced to info level" "whoops" "ok" # level translated to default
writeJsonLog "Warning! This is JSON." "warning" "flagged" # warning level with 'flagged' status
writeJsonLog "Error sample! This is JSON." "err" "error" # error level message
writeJsonLog "Unable to continue, critical failure. (This is JSON)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
elif [ "$1" = "journal" ]; then
writeJournalDLog # this will do nothing
writeJournalDLog "This is a test message" # this will also do nothing
writeJournalDLog "Informational JOURNALD log message" "information" "ok" # info message
writeJournalDLog "JOURNALD message forced to info level" "whoops" "ok" # level translated to default
writeJournalDLog "Warning! This is a JOURNALD message." "warning" "flagged" # warning level with 'flagged' status
writeJournalDLog "Error sample! This is a JOURNALD." "err" "error" # error level message
writeJournalDLog "Unable to continue, critical failure. (This is JOURNALD message)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
else
printf "\nPlease specify either 'text', 'json', or 'journal' after the script name. Exiting.\n\n"
exit 1
fi
exit 0