I have set up a custom .plist
in ~/Library/LaunchAgents
to run a process (offlineimap) on a periodic basis. I've specified both the StandardOutPath
and StandardErrorPath
keys to point to files in my home directory, which works. However, this doesn't really provide a complete logging solution from what I can see. The logs are not rotated, there are no timestamps (addressed by this question, although there was no clear answer), and the log is not integrated into any other logging on the system. I think I would prefer it if the logs went to Unified Logging.
I'm used to systemd logging on Linux, which is a similar mechanism, and it would be great if there was a similar way I could redirect that output into the system-wide log, viewable with the log
command. Is there any way to do that with a custom LaunchAgent?
I should add that I'm using MacOS 10.14.5.
Best Answer
You could use
blowhole
.blowhole
is a command-line tool that takes a string as an argument and sends it to the unified logging system. It is supported from Sierra (10.12) up to Catalina (10.15).How to use it
(Tested on macOS Catalina 10.15.5)
Modify the
ProgramArguments
array in your.plist
file like this:where
COMMAND OPTIONS
is the command you want to execute, followed by any desired options.Here, I make use of
bash
's support for redirection (2>
), process substitution (>()
) and pipelines (|
) to:process them separately inside two
while
loops. The firstwhile
loop runsblowhole -e
to log standard error with an "Error" level:and the second one runs
blowhole -d
to log standard output with a "Default" level:(Since
blowhole
can't read from standard input, we needwhile
loops to feed it a line of input at a time.)The
blowhole: [co.eclecticlight.blowhole:general] Blowhole:
string is not configurable, but you can add a prefix of your choice to the logged messages. For example, since you mentionofflineimap
in your question:and:
You can read log entries with
sudo log show | grep blowhole:general
orsudo log show | grep offlineimap
, if you added the customized prefix. To read log entries as they are generated, in a manner similar totail -f
, useshow stream
instead.Alternatively, you can wrap the command you want to execute in a shell script so that
blowhole
logs the command's standard output and error in a way similar to above. This is convenient if you want to run some code prior to executing the actual command:You can then configure the
ProgramArguments
array of your.plist
file to run the script instead of your command:Where to get it from
You can download
blowhole
from its project page or directly from here. The program is provided as a signed, hardened and notarized executable (as required by Catalina) and as an Installer package.