Skip to content

Commit

Permalink
Add prefix to log message
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Apr 26, 2017
1 parent b91df2b commit f2f4e9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
39 changes: 37 additions & 2 deletions lib/core_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ make_temp_file () {
# Prints a message to a log file.
#
# Usage:
# $ print_log_message LOG_MESSAGE LOG_FILE
# $ print_log_message [-DWIE] LOG_MESSAGE LOG_FILE
#
# Computes a timestamp and prepends the timestamp to the message
# before appending the message to the log file.
Expand All @@ -185,11 +185,46 @@ make_temp_file () {
print_log_message () {

# local vars
local prefix
local log_message
local log_file
local tstamp

local opt
local OPTARG
local OPTIND
while getopts ":DWIE" opt; do
case $opt in
D)
prefix=DEBUG
;;
W)
prefix=WARN
;;
I)
prefix=INFO
;;
E)
prefix=ERROR
;;
\?)
echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2
return 2
;;
:)
echo "ERROR: $FUNCNAME: Option -$OPTARG requires an argument" >&2
return 2
;;
esac
done

# default to INFO
if [ -z "$prefix" ]; then
prefix=INFO
fi

# make sure there are two command-line arguments
shift $(( OPTIND - 1 ))
if [ $# -ne 2 ]; then
echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (2 required)" >&2
return 2
Expand All @@ -203,7 +238,7 @@ print_log_message () {
tstamp=$( /bin/date -u +%Y-%m-%dT%TZ )

# prepend timestamp to the log message
printf "%s %s\n" "$tstamp" "$log_message" >> "$log_file"
printf "%s %s %s\n" "$tstamp" "$prefix" "$log_message" >> "$log_file"
}

#######################################################################
Expand Down
4 changes: 2 additions & 2 deletions lib/http_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ conditional_get () {

# quiet failure mode
if $cache_only_mode; then
print_log_message "$FUNCNAME: ERROR: resource not cached: $location" "$log_file"
print_log_message -E "$FUNCNAME: resource not cached: $location" "$log_file"
return 1
fi

Expand Down Expand Up @@ -403,7 +403,7 @@ conditional_get () {
elif [ "$response_code" = "304" ]; then
# quiet failure mode
if $force_output_mode; then
print_log_message "$FUNCNAME: ERROR: resource not modified: $location" "$log_file"
print_log_message -E "$FUNCNAME: resource not modified: $location" "$log_file"
return 1
fi

Expand Down

0 comments on commit f2f4e9f

Please sign in to comment.