简体   繁体   English

如何从前端使用 AWS CloudWatch Logs 提交简单日志?

[英]How to submit the simple log with AWS CloudWatch Logs from frontend?

After about 1 hour of searching, I didn't find anything about 'how to submit a simple log to AWS CloudWatch Logs' from the frontend side .经过大约 1 小时的搜索,我没有从前端找到任何关于“如何将简单日志提交到 AWS CloudWatch Logs”的信息 Almost all examples are for Node.js, but I need to submit the errors from the frontend, not form backend.几乎所有示例都是针对 Node.js 的,但我需要从前端提交错误,而不是从后端提交错误。 I even did not found which package which I should use for the frontend.我什至没有找到我应该为前端使用哪个包。

To save, your time, I prepared the template of solution.为了节省您的时间,我准备了解决方案模板。

import { AWSCloudWatch } from "?????";

AWSCloudWatch.config({
  // minimal config
});

if (__IS_DEVELOPMENT_BUILDING_MODE__ || __IS_TESTING_BUILDING_MODE__) {
  console.error(errorMessage);
  return;
}

if (__IS_PRODUCTION_BUILDING_MODE__) {
  // Submit 'errorMessage' to AWS CloudWatch
  // It would be something like
  // AWSCloudWatch.submit(errorMessage)
}

You can use AWS SDK for JavaScript directly from your browser.您可以直接从浏览器使用适用于 JavaScript 的 AWS 开发工具包。 Visit https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/ for the guide.访问https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/获取指南。

Then you can call the putLogEvents method of AWS CloudWatchLogs API, assuming you already created log group and log stream.然后您可以调用 AWS CloudWatchLogs API 的putLogEvents方法,假设您已经创建了日志组和日志流。 For guide visit https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#putLogEvents-property如需指南,请访问https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#putLogEvents-property

We're not supposed to be dumping browser-side error logs directly from the browser to CloudWatch.我们不应该将浏览器端错误日志直接从浏览器转储到 CloudWatch。 This presents an issue because posting logs to CloudWatch using SDK outside the AWS ecosystem requires API secret key and secret IDs, which means that it could be exposed to bad elements that could run MITM attacks and intercept our AWS credentials.这带来了一个问题,因为在 AWS 生态系统之外使用 SDK 将日志发布到 CloudWatch 需要 API 密钥和秘密 ID,这意味着它可能会暴露给可能运行 MITM 攻击并拦截我们的 AWS 凭证的不良元素。

You will have two options to proceed with what you desire to do with less risk:您将有两种选择来以较低的风险继续您想要做的事情:

  1. Use client-side libraries (Sentry) that were specifically designed to log errors and debugging information.使用专门设计用于记录错误和调试信息的客户端库 (Sentry)。

  2. You can implement the sending of logs via an API that then forwards/proxies the logs towards CloudWatch.您可以通过 API 发送日志,然后将日志转发/代理到 CloudWatch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM