Takuya HARA
Takuya HARA

Reputation: 657

Which directory is the best for saving logs?

I'm new to MacOS. After making .app app I'm about to add logging feature but wondering which directory should I use. I found that the followings are common to save log files but how they differ and which one is the best?

Upvotes: 6

Views: 2692

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125798

Short answer: It depends on what your software is and what it's logging. For a .app that's creating its own log, ~/Library/Logs is almost certainly the best place. But if you're logging debugging/tracing info about your app, use Apple's unified logging system instead.

Long answer: The first relevant distinction is whether the events being logged are relevant to you as the developer, or to the actual users running the app. If if's for you, use Apple's unified logging.

The next relevant distinction is whether the log is relevant the system as a whole, or just to the individual user running the software. You probably use macOS as a single-user operating system, but it's organized so that multiple users can share the same computer. So ask yourself: should this log be visible to all users on the computer (or maybe just administrators), or just the user who's actually using the software? If each user should see just their own log history, then it clearly belongs in the user's home folder, so ~/Library/Logs is the right place.

A related issue is that normal users don't have permissions to write to /Library/Logs and /var/log (aka /private/var/log), so an app running as a normal user can't log to those places.

The last distinction is whether your software is Mac-ish or Unix-ish. macOS sort of has two layers, the Unix layer and the Mac-specific layer built on top of that. /Library/Logs is the Mac-ish place for non-user-specific logs, and /var/log is the analogous Unix-ish place. Put it this way: if your program installs into /Applications, you're part of the Mac-ish world, and would use /Library/Logs for system-wide logs. On the other hand, if your program installs into /usr/local or /opt, you're part of the Unix-ish world, and should use /var/log.

So, to summarize:

  • Unified logging system for developer-oriented debugging and tracing info
  • ~/Library/Logs for user-oriented info from non-system software
  • /Library/Logs for Mac-ish system-wide event logging
  • /var/log for Unix-ish system-wide event logging

BTW, the unified log info is actually stored in /var/log. But don't access it that way -- use the API.

Upvotes: 14

Related Questions