Home > database >  How can i write log files from Angular (v 12)?
How can i write log files from Angular (v 12)?

Time:12-09

I'm working on a site, with the Backend written in node.js on a linux machine, and a Frontend done in Angular that runs on another linux machine. I've been asked to write daily logs of the frontend (backend has its logs, with Winston), and save them in the frontend machine, but I really cannot find any way to do it. Every search I've done take me to the conclusion that Angular cannot save to file, but that seems to me really strange.

Is there any way to do what I need? Thanks.

CodePudding user response:

A browser is not allowed to modify the file system for security. You will have to either write the log to some form of localStorage or indexedDB, and then offer the user to get the log contents as a file download. That, or you must save the log server-side.

Be aware that localStorage, indexDB and similar client-side storage solutions have limited space and is considered temporary storage, and may disappear at any given time.

CodePudding user response:

We had a similar case in our company.

Due to the fact, that Angular runs in the Browser and and each user has its own log on his local machine, respectively in the browser console, you want to aggregate the logs in a central point. I would recommend you to extend you web-api with an additional endpoint (e.g. api/logs).

With this approach you have the opportunity to send logs from you frontend to you backend and store them in any way you want. (e.g. Files for each user, in the database)

Aside from that, you don't have to write the corresponding frontend code by yourself.Have a look at ngx-logger It is a neat little logger npm-package, that can take a backend endpoint, where it is supposed to transmit the logfiles to.

  • Related