SBB
SBB

Reputation: 8990

Electron / Node file path location

I have an electron app that is using electron-log to handle creating some debugging information for the app. By default, it is saving the files to the following location per the module:

**on macOS:** ~/Library/Logs/<app name>/log.log

**on Windows:** %USERPROFILE%\AppData\Roaming\<app name>\log.log

I have added an option in the menu to "View Debug Info". My goal is to read this log into a textarea (on a renderer) so that they can provide it to support if needed.

In my renderer, I am using fs in order to access the file system but I can't find anything in process.env that points to these locations so I assume they are custom?

Is there a variable I am missing that contains these paths on the os?

const fs = require('fs');

if(process.platform == 'darwin'){
    // Path is ~/Library/Logs/<app name>/log.log
    // Read the file into the textarea
}else{
    // Path is %USERPROFILE%\AppData\Roaming\<app name>\log.log
    // Read the file into the textarea
}

Upvotes: 3

Views: 6854

Answers (2)

Neil P.
Neil P.

Reputation: 1114

I think you could use app.getPath(...) in your case.

import { app } from "electron";
let logFileName;

// If, darwin; PATH is: ~/Library/Logs/<app name>/log.log
if(process.platform == 'darwin'){
  logFileName = app.getPath("logs") + "/log.log",
} else if(process.platform == 'win32'){ {
  logFileName = app.getPath("userData") + "/log.log",
} else {
  // Handle other supported platforms ('aix','freebsd', 'linux', 'openbsd', 'sunos')
}

fs.readFile(logFileName, function read(err, data) {
  if (err) {
    throw err;
  }
  // Read the file data content into the text-area.
});

From Electron's getPath documentation;

app.getPath(name)

name String

Returns String - A path to a special directory or file associated with name. On failure, an Error is thrown.

You can request the following paths by the name:

home User's home directory.

appData Per-user application data directory, which by default points to: %APPDATA% on Windows, $XDG_CONFIG_HOME or ~/.config on Linux & ~/Library/Application Support on macOS

userData The directory for storing your app's configuration files, which by default it is the appData directory appended with your app's name.

temp Temporary directory.

exe The current executable file.

module The libchromiumcontent library.

desktop The current user's Desktop directory.

documents Directory for a user's "My Documents".

downloads Directory for a user's downloads.

music Directory for a user's music.

pictures Directory for a user's pictures.

videos Directory for a user's videos.

logs Directory for your app's log folder.

pepperFlashSystemPlugin Full path to the system version of the Pepper Flash plugin.

Upvotes: 0

hong4rc
hong4rc

Reputation: 4123

Try this:

const log = require('electron-log');

const path = log.transports.file.findLogPath();

findLogPath.js#L17

Upvotes: 1

Related Questions