Rahul Kumar
Rahul Kumar

Reputation: 101

How to log react console errors to a file?

I am new in front-end/client-side app development for website. I am setting up a new react project. I used create-react-app.

How should I handle the console errors(other than network call errors)? Is there a way to log them in any file?

What are the best logging practices?

Upvotes: 5

Views: 16845

Answers (1)

jeanfrg
jeanfrg

Reputation: 2351

Even though there are means and ways to store data on the browser (not to actual client files) that's not really the general strategy for logging browser errors (which I believe is what you're referring to).

In my experience we'd have a dedicated logging server with a simple API with adequate security that filters traffic and applies rate limiting. That would finally enhance and write logs into a document database that can later be analysed.

A naive JavaScript solution would be using the following to capture errors and send them out to a logging server.

window.onerror = function(message, url, lineNumber) {  
  // make ajax call to api to save error on server
  return true;
};  

I should also mention Sentry.io - they provide a service that does this and even though there are some limitations they're usually enough for a small to mid app.

https://github.com/getsentry

enter image description here

Upvotes: 5

Related Questions