If you are familiar with the Unix command line and shell scripting, then you will be right at home using the OS X Terminal, and can throw together a number of scripts to automate routines, and customize a number of functions on your Mac. When creating your scripts, as is common practice, you might wish to log various actions and activity, and while you might consider doing so in classic ways, there is an easy approach to simply log messages to the OS X system console.
The classic approach to logging behavior is to use a simple redirect to a designated log file, where you can quickly create such a file and tack on messages to it using the greater-than character, such as the following:
echo "my message" > /Library/Logs/mylog.log
To append data to an existing log instead of replacing the current file, simply use two greater-than symbols:
echo "my message" >> /Library/Logs/mylog.log
- Piping command output to logger
echo "my message" | logger
- Logging specific phrases
logger "my message"
Finally, if you want to output messages to the console in addition to redirecting them to your own log files, you can do so by using the “-s” flag for the logger command, which will have it show in the console but also output it to standard output so you can redirect it elsewhere. For instance, the following two commands will both output to the console and append the output to a specified log file:
echo "my message" | logger -s >> /Library/Logs/mylog.log
logger -s "my message" >> /Library/Logs/mylog.log