logdna / nodejs

Node.js library for logging to LogDNA
MIT License
74 stars 66 forks source link

Using LogDNA in Electron #103

Closed jjalan closed 4 years ago

jjalan commented 4 years ago

Hello,

I have an electron app that is bundle with React application. I am trying to use logDNA from inside React views on Electron.

It works fine during development as Electron serves the react application via a local dev server as long as I have http://localhost in the whitelisted domain in the settings on LogDNA.

However, electron app simply serves the React index.html via file:// in production build. These logs are not showing up in LogDNA. See the error trace from network tab:

Screenshot 2020-08-21 at 4 29 35 PM

I tried to add file:// as whitelisted domain from LogDNA but it won't let me.

Screenshot 2020-08-21 at 4 31 38 PM

Error (from network tab): Invalid value for domain: file://

Any pointers in this regard would be greatly appreciated.

mdeltito commented 4 years ago

Hi @jjalan, thanks for reaching out

In the context of an Electron app, one option would be to exclude the Origin header for the logdna.com domain. You can do this with the onBeforeRequest method:

https://www.electronjs.org/docs/api/web-request#webrequestonbeforerequestfilter-listener

With the origin set to null we will not check for whitelisted domain(s) to apply CORS headers for in the response. This is likely what you want, as there is no "domain" in this context to restrict logging calls to.

jjalan commented 4 years ago

@mdeltito,

Thanks. That works. For future reference, here is full snippet to include in Electron app:

app.on("ready", () => {
  // Set Origin header to null for all requests out to LogDNA
  // otherwise the request fails due to CORS issue
  session.defaultSession.webRequest.onBeforeSendHeaders({
    urls: ['https://*.logdna.com/*']
  }, (details, callback) => {
    details.requestHeaders['Origin'] = null;
    callback({ requestHeaders: details.requestHeaders })
  });
});
darinspivey commented 4 years ago

Excellent, we're glad the suggestion worked. While you're at it, we just released a new version of the node client that you should try out. Thank you, and let us know if we can help in the future.