feature(logger): Add plain-text logger function
This commit is contained in:
@@ -4,38 +4,47 @@ getTimeStamp() {
|
||||
(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
}
|
||||
|
||||
getHostname() {
|
||||
(hostname -s)
|
||||
}
|
||||
|
||||
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() {
|
||||
# if no message is supplied, there is no purpose in generating a log message!
|
||||
if [ -z "$1" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
printf "%s %s %s[%s]: [%s] [%s] %s (%s:%s)\n" \
|
||||
"$(getTimeStamp)" "$(getHostname)" "$LOG_PROGRAM_NAME" "$$" \
|
||||
"$2" "$3" "$1" "${4:UNSPECIFIED}" "${5:999}"
|
||||
}
|
||||
|
||||
# $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" \
|
||||
--arg pName "$LOG_PROGRAM_NAME" \
|
||||
--arg operation "$LOG_DEFAULT_OPERATION" \
|
||||
--arg status "$LOG_DEFAULT_STATUS" \
|
||||
--arg code "$LOG_DEFAULT_CODE" \
|
||||
--arg message "$LOG_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 level "${2:-$LOG_DEFAULT_LEVEL}" \
|
||||
--arg pName "$LOG_PROGRAM_NAME" \
|
||||
--arg status "$(uppercase "${3:-$LOG_DEFAULT_STATUS}")" \
|
||||
--arg operation "${4:-$LOG_DEFAULT_OPERATION}" \
|
||||
--arg code "${5:-$LOG_DEFAULT_CODE}" \
|
||||
--arg message "$1" \
|
||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
||||
)"
|
||||
@@ -47,13 +56,6 @@ writeJsonLog() {
|
||||
|
||||
# $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
|
||||
@@ -92,30 +94,37 @@ writeJournalLog() {
|
||||
MESSAGE_ID="$(systemd-id128 new)"
|
||||
SYSLOG_TIMESTAMP="$(getTimeStamp)"
|
||||
SYSLOG_FACILITY=3
|
||||
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
|
||||
SYSLOG_IDENTIFIER="${LOG_PROGRAM_NAME}"
|
||||
PRIORITY=4
|
||||
ERRNO="${DEFAULT_CODE}"
|
||||
OPERATION="${DEFAULT_OPERATION}"
|
||||
MESSAGE="${DEFAULT_MESSAGE}"
|
||||
STATUS="${DEFAULT_STATUS}"
|
||||
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="${PROGRAM_NAME}"
|
||||
SYSLOG_IDENTIFIER="${LOG_PROGRAM_NAME}"
|
||||
PRIORITY="${LOG_LEVEL}"
|
||||
ERRNO="${5:-$DEFAULT_CODE}"
|
||||
OPERATION="${4:-$DEFAULT_OPERATION}"
|
||||
ERRNO="${5:-$LOG_DEFAULT_CODE}"
|
||||
OPERATION="${4:-$LOG_DEFAULT_OPERATION}"
|
||||
MESSAGE="$1"
|
||||
STATUS="${3:-$DEFAULT_STATUS}"
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user