Skip to content

Instantly share code, notes, and snippets.

@brunerd
Created November 14, 2023 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunerd/fdfa35eff0d59d128f36464687dee228 to your computer and use it in GitHub Desktop.
Save brunerd/fdfa35eff0d59d128f36464687dee228 to your computer and use it in GitHub Desktop.
# 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
function jamflog(){
[ -n "${-//[^x]/}" ] && { local xTrace=1; set +x; } &>/dev/null
local logFile="${2:-/var/log/jamf.log}"
#if it exists but we cannot write to the log or it does not exist, unset and tee simply echoes
[ -e "${logFile}" -a ! -w "${logFile}" ] && unset logFile
#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:=$$}]: ${1}" | /usr/bin/tee -a "${logFile}" 2>/dev/null
[ "${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