Files
CloudflareDDNS/logger.sh
T

146 lines
4.0 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:]'
}
# 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" "$$" \
"$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 "$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 "."
return
}
# $1: message, $2: level, $3: status, $4: operation, $5: code
writeJournalLog() {
# 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="${LOG_PROGRAM_NAME}"
PRIORITY=4
ERRNO="${LOG_DEFAULT_CODE}"
OPERATION="${LOG_DEFAULT_OPERATION}"
MESSAGE="${LOG_DEFAULT_MESSAGE}"
STATUS="${LOG_DEFAULT_STATUS}"
end
else
logger --journald <<end
MESSAGE_ID="$(systemd-id128 new)"
SYSLOG_TIMESTAMP="$(getTimeStamp)"
SYSLOG_FACILITY=3
SYSLOG_IDENTIFIER="${LOG_PROGRAM_NAME}"
PRIORITY="${LOG_LEVEL}"
ERRNO="${5:-$LOG_DEFAULT_CODE}"
OPERATION="${4:-$LOG_DEFAULT_OPERATION}"
MESSAGE="$1"
STATUS="${3:-$LOG_DEFAULT_STATUS}"
end
fi
return
}
LOG_PROGRAM_NAME="cfddns"
LOG_DEFAULT_CODE=99
LOG_DEFAULT_LEVEL="info"
LOG_DEFAULT_MESSAGE="An unspecified error has occurred"
LOG_DEFAULT_OPERATION="unspecified"
LOG_DEFAULT_STATUS="ERR"
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