Files
CloudflareDDNS/logger.sh
T

137 lines
3.9 KiB
Bash

#!/bin/sh
getTimeStamp() {
(date -u +"%Y-%m-%dT%H:%M:%SZ")
}
uppercase() {
echo "$1" | tr '[:lower:] [:upper:]'
}
# $1: message, $2: level, $3: status, $4: operation, $5: code
writeJsonLog() {
PROGRAM_NAME="cfddns"
DEFAULT_CODE=99
DEFAULT_LEVEL="info"
DEFAULT_MESSAGE="An unspecified error has occurred"
DEFAULT_OPERATION="unspecified"
DEFAULT_STATUS="ERR"
if [ -z "$1" ]; then
PROPS="$(
jq --null-input \
--arg level "err" \
--arg pName "$PROGRAM_NAME" \
--arg operation "$DEFAULT_OPERATION" \
--arg status "$DEFAULT_STATUS" \
--arg code "$DEFAULT_CODE" \
--arg message "$DEFAULT_MESSAGE" \
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
)"
else
PROPS="$(
jq --null-input \
--arg level "${2:-$DEFAULT_LEVEL}" \
--arg pName "$PROGRAM_NAME" \
--arg status "$(uppercase "${3:-$DEFAULT_STATUS}")" \
--arg operation "${4:-$DEFAULT_OPERATION}" \
--arg code "${5:-$DEFAULT_CODE}" \
--arg message "$1" \
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
)"
fi
echo "$PROPS" | jq "."
return
}
# $1: message, $2: level, $3: status, $4: operation, $5: code
writeJournalLog() {
PROGRAM_NAME="cfddns"
DEFAULT_CODE=99
DEFAULT_LEVEL="info"
DEFAULT_MESSAGE="An unspecified error has occurred"
DEFAULT_OPERATION="unspecified"
DEFAULT_STATUS="ERR"
# translate error levels
if [ -z "$2" ]; then
LOG_LEVEL=1
else
case "$2" in
"emergency" | "emerg")
LOG_LEVEL=7
;;
"alert")
LOG_LEVEL=6
;;
"critical" | "crit")
LOG_LEVEL=5
;;
"error" | "err")
LOG_LEVEL=4
;;
"warning" | "warn")
LOG_LEVEL=3
;;
"notice")
LOG_LEVEL=2
;;
"info")
LOG_LEVEL=1
;;
"debug")
LOG_LEVEL=0
;;
esac
fi
# log the message
if [ -z "$1" ]; then
logger --journald <<end
MESSAGE_ID="$(systemd-id128 new)"
SYSLOG_TIMESTAMP="$(getTimeStamp)"
SYSLOG_FACILITY=3
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
PRIORITY=4
ERRNO="${DEFAULT_CODE}"
OPERATION="${DEFAULT_OPERATION}"
MESSAGE="${DEFAULT_MESSAGE}"
STATUS="${DEFAULT_STATUS}"
end
else
logger --journald <<end
MESSAGE_ID="$(systemd-id128 new)"
SYSLOG_TIMESTAMP="$(getTimeStamp)"
SYSLOG_FACILITY=3
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
PRIORITY="${LOG_LEVEL}"
ERRNO="${5:-$DEFAULT_CODE}"
OPERATION="${4:-$DEFAULT_OPERATION}"
MESSAGE="$1"
STATUS="${3:-$DEFAULT_STATUS}"
end
fi
return
}
if [ "$1" = "json" ]; then
writeJsonLog # this will generate a generic error message
writeJsonLog "This is a test message" # generate this message at the default level with the default code
writeJsonLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
writeJsonLog "This is a test message" "error" "testing" # generate this message at the 'error' level, part of a 'test' operation, and with the default code
writeJsonLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
elif [ "$1" = "journal" ]; then
writeJournalLog # this will generate a generic error message
writeJournalLog "This is a test message" # generate this message at the default level with the default code
writeJournalLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
writeJournalLog "This is a test message" "error" "testing" # generate this message at the 'error' level as part of a 'test' operation, and with the default code
writeJournalLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
else
printf "\nPlease specify either 'json' or 'journal' after the script name. Exiting.\n\n"
exit 1
fi
exit 0