简体   繁体   English

如何从 Angular (v 12) 写入日志文件?

[英]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.

相关问题 每当我输入输入时如何显示一个 div? Angular 12 - How can I show a div whenever I write in an input? Angular 12 如何从Javascript读取和编写Angular组件变量? - How can I read and write an Angular component variable from Javascript? Angular 从 v12 迁移到 v14 - Angular migration from v12 to v14 我如何添加外部js文件以在angular cli V 7.2.1上工作 - How can i add external js files to work on angular cli V 7.2.1 "Angular v12 组件样式文件不编译tailwindcss @apply" - Angular v12 Component style files not compiling tailwindcss @apply 2 行 12 个 Angular Material Mat 选项卡如何实现? - 12 Angular Material Mat tabs in 2 rows how can I achieve it? 如何在 angular 12 中使用 jsPdf 创建 pdf? - How can i create pdf in with jsPdf in angular 12? Angular 从 v12 迁移到 v13:模块<typescript>只能使用“allowSyntheticDefaultImports”标志默认导入</typescript> - Angular Migrate from v12 to v13: Module <typescript> can only be default-imported using the 'allowSyntheticDefaultImports' flag 我怎样才能console.log从有角度的Material表格和表格中选择的选项? 角度4 - How can I console.log the selected option from angular Material form and table? ANGULAR 4 如何从输入文件中删除重复文件(Angular) - How Can I remove repeated files from input file (Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM