From d36b42acf4839aa51107a993bf252bcab2a17312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Theilgaard?= Date: Wed, 2 Feb 2022 19:39:48 +0100 Subject: [PATCH] Isaac logging improvements/fixes Moved the variable declarations from `functions.sh` to `arguments.sh`. We need the argument parsing before setting the logging level and other variables. --- fragments/arguments.sh | 32 ++++++++++++++++++++++++++++++++ fragments/functions.sh | 41 ++++------------------------------------- fragments/header.sh | 2 +- fragments/main.sh | 5 +++-- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/fragments/arguments.sh b/fragments/arguments.sh index dfd26e4..45a933c 100644 --- a/fragments/arguments.sh +++ b/fragments/arguments.sh @@ -43,6 +43,38 @@ if [[ $label == "version" ]]; then exit 0 fi +# MARK: Logging +log_location="/private/var/log/Installomator.log" + +# Check if we're in debug mode, if so then set logging to DEBUG, otherwise default to INFO +# if no log level is specified. +if [[ $DEBUG -ne 0 ]]; then + LOGGING=DEBUG +elif [[ -z $LOGGING ]]; then + LOGGING=INFO + datadogLoggingLevel=INFO +fi + +# Associate logging levels with a numerical value so that we are able to identify what +# should be removed. For example if the LOGGING=ERROR only printlog statements with the +# level REQ and ERROR will be displayed. LOGGING=DEBUG will show all printlog statements. +# If a printlog statement has no level set it's automatically assigned INFO. + +declare -A levels=(DEBUG 0 INFO 1 WARN 2 ERROR 3 REQ 4) + +# If we are able to detect an MDM URL (Jamf Pro) or another identifier for a customer/instance we grab it here, this is useful if we're centrally logging multiple MDM instances. +if [[ -f /Library/Preferences/com.jamfsoftware.jamf.plist ]]; then + mdmURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url) +elif [[ -n "$MDMProfileName" ]]; then + mdmURL=$(sudo profiles show | grep -A3 "$MDMProfileName" | sed -n -e 's/^.*organization: //p') +else + mdmURL="Unknown" +fi + +# Generate a session key for this run, this is useful to idenify streams when we're centrally logging. +SESSION=$RANDOM + +# Mark: START printlog "################## Start Installomator v. $VERSION, date $VERSIONDATE" REQ printlog "################## Version: $VERSION" INFO printlog "################## Date: $VERSIONDATE" INFO diff --git a/fragments/functions.sh b/fragments/functions.sh index 2e1fc8a..efdcead 100644 --- a/fragments/functions.sh +++ b/fragments/functions.sh @@ -67,38 +67,6 @@ displaynotification() { # $1: message $2: title fi } - -# MARK: Logging -log_location="/private/var/log/Installomator.log" - -# Check if we're in debug mode, if so then set logging to DEBUG, otherwise default to INFO -# if no log level is specified. -if [[ $DEBUG -ne 0 ]]; then - LOGGING=DEBUG -elif [[ -z $LOGGING ]]; then - LOGGING=INFO - datadogLoggingLevel=INFO -fi - -# Associate logging levels with a numerical value so that we are able to identify what -# should be removed. For example if the LOGGING=ERROR only printlog statements with the -# level REQ and ERROR will be displayed. LOGGING=DEBUG will show all printlog statements. -# If a printlog statement has no level set it's automatically assigned INFO. - -declare -A levels=(DEBUG 0 INFO 1 WARN 2 ERROR 3 REQ 4) - -# If we are able to detect an MDM URL (Jamf Pro) or another identifier for a customer/instance we grab it here, this is useful if we're centrally logging multiple MDM instances. -if [[ -f /Library/Preferences/com.jamfsoftware.jamf.plist ]]; then - mdmURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url) -elif [[ -n "$MDMProfileName" ]]; then - mdmURL=$(sudo profiles show | grep -A3 "$MDMProfileName" | sed -n -e 's/^.*organization: //p') -else - mdmURL="Unknown" -fi - -# Generate a session key for this run, this is useful to idenify streams when we're centrally logging. -SESSION=$RANDOM - printlog(){ [ -z "$2" ] && 2=INFO log_message=$1 @@ -106,11 +74,10 @@ printlog(){ timestamp=$(date +%F\ %T) # Check to make sure that the log isn't the same as the last, if it is then don't log and increment a timer. - if [[ ${log_message} == ${previous_log_message} ]];then + if [[ ${log_message} == ${previous_log_message} ]]; then let logrepeat=$logrepeat+1 return fi - previous_log_message=$log_message # Once we finally stop getting duplicate logs output the number of times we got a duplicate. @@ -123,7 +90,7 @@ printlog(){ logrepeat=0 fi - # If the datadogAPI key value is set and our logging level is greaterthan or equal to our set level + # If the datadogAPI key value is set and our logging level is greater than or equal to our set level # then post to Datadog's HTTPs endpoint. if [[ -n $datadogAPI && ${levels[$log_priority]} -ge ${levels[$datadogLoggingLevel]} ]]; then while IFS= read -r logmessage; do @@ -143,8 +110,8 @@ printlog(){ fi } -# Used to remove dupplicate lines in large log output, for example from msupdate command -# after it finishes running. +# Used to remove dupplicate lines in large log output, +# for example from msupdate command after it finishes running. deduplicatelogs() { loginput=${1:-"Log"} logoutput="" diff --git a/fragments/header.sh b/fragments/header.sh index 25dfaf4..cc19927 100644 --- a/fragments/header.sh +++ b/fragments/header.sh @@ -266,7 +266,7 @@ IGNORE_DND_APPS="" # ### Logging # Logging behavior -LOGGING="" +LOGGING="INFO" # options: # - DEBUG Everything is logged # - INFO (default) normal logging level diff --git a/fragments/main.sh b/fragments/main.sh index f77335f..3f75085 100644 --- a/fragments/main.sh +++ b/fragments/main.sh @@ -24,6 +24,7 @@ fi printlog "BLOCKING_PROCESS_ACTION=${BLOCKING_PROCESS_ACTION}" printlog "NOTIFY=${NOTIFY}" +printlog "LOGGING=${LOGGING}" # Finding LOGO to use in dialogs case $LOGO in @@ -191,7 +192,6 @@ else curlDownload=$(curl -v -fsL --show-error ${curlOptions} "$downloadURL" -o "$archiveName" 2>&1) curlDownloadStatus=$(echo $?) deduplicatelogs "$curlDownload" - printlog "curl output was: $logoutput" DEBUG if [[ $curlDownloadStatus -ne 0 ]]; then #if ! curl --location --fail --silent "$downloadURL" -o "$archiveName"; then printlog "error downloading $downloadURL" @@ -204,8 +204,9 @@ else displaynotification "$message" "Error installing $name" ERROR fi fi - cleanupAndExit 2 "Error downloading $downloadURL error: $logoutput" ERROR + cleanupAndExit 2 "Error downloading $downloadURL error:\n$logoutput" ERROR fi + printlog "curl output was:\n$logoutput" DEBUG fi # MARK: when user is logged in, and app is running, prompt user to quit app