Where to save CLI application log file that will work cross-platform?

Viewed 147

I have an application that saves error logs into the same directory where the script was run. This is bad because it creates error.log files in random places. So I want to save the file into /var/log on GNU/Linux and I would like to have the same for Windows and macOS (and probably any other OS where NodeJS runs).

Is there a cross-platform way to get a log directory? So I can write logs there? Also how to work with that directory if it's owned by root. Or is there a better place to save NodeJS application logs?

The only directory cross-platform NodeJS directory is /tmp using:

const log_filename = path.join(os.tmpdir(), 'lips.error.log');

What is the best place to save logs where users can look them up? Same directory and /tmp directory are not good options in my option. What are the other options to save logs files?

What is the usual place where log files should be saved cross-platform in any CLI application?

1 Answers

That's part of the fun -- there is no single directory to store logs, so you'll need to handle each supported OS separately.

  • Linux has options, but it would be good to go for the XDG specification way, as it's a reasonable standard that won't fill up system logs, and it will make it simple to isolate the applications logs.

  • macOS can use the ~/Library/logs folder.

  • Windows can use the AppData folder, usually AppData/Local.

Related