Last active
September 26, 2024 18:00
-
-
Save brunerd/fdfa35eff0d59d128f36464687dee228 to your computer and use it in GitHub Desktop.
a way to log to stdout and /var/log/jamf.log (or elsewhere) and have it match Jamf's log style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jamflog - Copyright (c) 2021 Joel Bruner, Licensed under the MIT License | |
# a way to log to stdout and /var/log/jamf.log (or elsewhere) and have it match Jamf's log style | |
##NOW MULTI-LINE an MULTI-MODE (parameter or PIPED) | |
function jamflog(){ | |
#unset xtrace if enabled to quiet this function | |
[ -n "${-//[^x]/}" ] && { local xTrace=1; set +x; } &>/dev/null | |
#take input either as a parameter or piped in | |
if [ -n "${1}" ]; then local input="${1}"; elif [ ! -t '0' ]; then local input=$(cat); else return; fi | |
#default destination is override-able | |
local logFile="${2:-/var/log/jamf.log}" | |
#if we cannot write to the log, unset and tee simply echoes | |
([ -e "${logFile}" ] && [ ! -w "${logFile}" ]) && unset logFile | |
#process each line | |
local IFS=$'\n' | |
for line in ${input}; do | |
#this will tee to jamf.log in the jamf log format: <Day> <Month> DD HH:MM:SS <Computer Name> ProcessName[PID]: <Message> | |
builtin echo "$(/bin/date +'%a %b %d %H:%M:%S') ${jamflog_myComputerName:="$(/usr/sbin/scutil --get ComputerName)"} ${jamflog_myName:="$(/usr/bin/basename "${0%.*}")"}[${myPID:=$$}]: ${line}" | /usr/bin/tee -a "${logFile}" 2>/dev/null | |
done | |
#re-enable xtrace if it was on | |
[ "${xTrace:-0}" = 1 ] && { set -x; } &>/dev/null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment