[英]How can i write log files from Angular (v 12)?
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'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.
我被要求写前端的每日日志(后端有它的日志,与 Winston 一起),并将它们保存在前端机器中,但我真的找不到任何方法。 Every search I've done take me to the conclusion that Angular cannot save to file, but that seems to me really strange.
我所做的每一次搜索都让我得出结论,Angular 无法保存到文件,但这在我看来真的很奇怪。
Is there any way to do what I need?有什么办法可以做我需要的吗? Thanks.
谢谢。
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.
您必须将日志写入某种形式的 localStorage 或 indexedDB,然后让用户以文件下载的形式获取日志内容。 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.请注意,localStorage、indexDB 和类似的客户端存储解决方案的空间有限,被视为临时存储,并且可能在任何给定时间消失。
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.由于 Angular 在浏览器中运行,并且每个用户在其本地计算机上都有自己的日志,分别在浏览器控制台中,您希望将日志聚合到一个中心点。 I would recommend you to extend you web-api with an additional endpoint (eg api/logs).
我建议你用一个额外的端点(例如 api/logs)来扩展你的 web-api。
With this approach you have the opportunity to send logs from you frontend to you backend and store them in any way you want.使用这种方法,您有机会将日志从前端发送到后端,并以您想要的任何方式存储它们。 (eg 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.除此之外,您不必自己编写相应的前端代码。看看ngx-logger它是一个简洁的小记录器 npm-package,可以采用后端端点,它应该传输日志文件至。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.