b8fb744623
Assignment to 'pid' argument was incorrect.
157 lines
4.8 KiB
Bash
157 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_TRANSLATE="$1"
|
|
case "$LEVEL_TO_TRANSLATE" 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")
|
|
return 5
|
|
;;
|
|
"ERR")
|
|
return 4
|
|
;;
|
|
"WARN")
|
|
return 3
|
|
;;
|
|
"INFO")
|
|
return 1
|
|
;;
|
|
"DEBUG")
|
|
return 0
|
|
;;
|
|
*)
|
|
return 0
|
|
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
|
|
|
|
logger --journald <<end
|
|
SYSLOG_TIMESTAMP="$(getTimeStamp)"
|
|
SYSLOG_FACILITY=3
|
|
SYSLOG_IDENTIFIER="${LOG_PROGRAM_NAME}"
|
|
SYSLOG_PID="$$"
|
|
PRIORITY="$(translateLogLevelsToJournalD "$2")"
|
|
STATUS="$(uppercase "$3")"
|
|
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
|