25 Commits

Author SHA1 Message Date
asif 92e69d1b85 fix(logger): Fix JournalD log levels
Log levels were reversed in the translation function.
2026-07-23 00:20:30 -06:00
asif 00ea88780d feat(logger): Echo vs return values from Translate func 2026-07-23 00:18:03 -06:00
asif d9aa8973a3 fix(logger): Perform cmd subst outside heredoc
Perform function and command value substitution outside the heredoc forwarded to logger for JournalD entries.
2026-07-23 00:17:42 -06:00
asif ec75cffbf1 fix(logger): Ensure log levels are lcase before standardization 2026-07-23 00:16:19 -06:00
asif b8fb744623 fix(logger): Fix error in json logger
Assignment to 'pid' argument was incorrect.
2026-07-22 23:50:33 -06:00
asif d0b2c781bf feat(logger): Update test cases
Update JSON and JOURNALD tests to match text tests and use new functions.
2026-07-22 23:48:16 -06:00
asif c4d28d1f8c fix(logger): Fix default subst
Default parameter substitution in all functions was incorrect.
2026-07-22 23:44:08 -06:00
asif f085838781 feat(logger): Add test with operation, error code 2026-07-22 23:42:52 -06:00
asif 4a6c339a07 fix(logger): Fix default subst
Default variable substitution in plain-text function was incorrect.
2026-07-22 23:42:33 -06:00
asif c94f12e976 fix(logger): Fix error in uppercase func
'upper' and 'lower' translate strings were not supplied separately.
2026-07-22 23:38:45 -06:00
asif 5443612381 feat(logger): Add plain-text test output 2026-07-22 23:35:08 -06:00
asif df144a5211 fix(logger): Remove errant 'fi' statements
Remove 'fi' statements left over from previous logic checks.
2026-07-22 23:33:49 -06:00
asif 04811805c3 feat(logger): Remove unused global vars 2026-07-22 23:25:28 -06:00
asif aa446c00e6 feat(logger): Update journal logging function
Use new standardized format fields.
2026-07-22 23:24:52 -06:00
asif 56a5ad59fe refactor(logger): Rename writeJournalLog function
Rename from 'writeJournalLog' to 'writeJournalDLog' for specificity.
2026-07-22 23:19:51 -06:00
asif ce1ab86d22 fix(logger): Fix JSON logger
Level and status were swapped. JSON output was missing new prefix information (timestamp, hostname, etc.)
2026-07-22 23:18:36 -06:00
asif cae57ccf6b feat(logger): Use standardized log levels for JSON 2026-07-22 23:15:44 -06:00
asif 0fcd0d9c13 refactor(logger): Use standardized log levels for plain-text 2026-07-22 23:15:11 -06:00
asif e411a35b93 feat(logger): Add log level translator functions 2026-07-22 23:13:58 -06:00
asif 0e787be456 feat(logger): Add lowercase function 2026-07-22 23:06:34 -06:00
asif 17c33d237f fix(logger): Ensure plain-text status is uppercase 2026-07-22 22:54:25 -06:00
asif 5b55ac95a3 feat(logger): Update JSON logger
Require message, level, and status. Reorganize JSON output to mirror modified syslog-style structure.
2026-07-22 22:53:38 -06:00
asif f4dc8f44b1 feat(logger): Handle plain-text insufficient information
Return without logging if message, level and/or status is not supplied.
2026-07-22 22:49:13 -06:00
asif 3792881529 feature(logger): Add plain-text logger function 2026-07-22 22:42:49 -06:00
asif c1f54a619c refactor(logger): Use ISO8601 timestamp 2026-07-22 21:52:53 -06:00
+130 -105
View File
@@ -1,135 +1,160 @@
#!/bin/sh
getTimeStamp() {
(date +%F" "%T)
(date -u +"%Y-%m-%dT%H:%M:%SZ")
}
getHostname() {
(hostname -s)
}
lowercase() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}
uppercase() {
echo "$1" | tr '[:lower:] [:upper:]'
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"
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
}
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}' \
)"
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
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
writeJournalDLog() {
# not enough information to generate a meaningful log message
if [ -z "$1" ] || [ $# -lt 3 ]; then
return
fi
# log the message
if [ -z "$1" ]; then
J_TIMESTAMP="$(getTimeStamp)"
J_PID="$$"
J_PRIO="$(translateLogLevelsToJournalD "$2")"
J_STAT="$(uppercase "$3")"
logger --journald <<end
MESSAGE_ID="$(systemd-id128 new)"
SYSLOG_TIMESTAMP="$(getTimeStamp)"
SYSLOG_TIMESTAMP=${J_TIMESTAMP}
SYSLOG_FACILITY=3
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
PRIORITY=4
ERRNO="${DEFAULT_CODE}"
OPERATION="${DEFAULT_OPERATION}"
MESSAGE="${DEFAULT_MESSAGE}"
STATUS="${DEFAULT_STATUS}"
SYSLOG_IDENTIFIER=${LOG_PROGRAM_NAME}
SYSLOG_PID=${J_PID}
PRIORITY=${J_PRIO}
STATUS=${J_STAT}
MESSAGE=$1
OPERATION=${4:-UNSPECIFIED}
ERRNO=${5:-999}
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
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
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
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 'json' or 'journal' after the script name. Exiting.\n\n"
printf "\nPlease specify either 'text', 'json', or 'journal' after the script name. Exiting.\n\n"
exit 1
fi