简体   繁体   English

使用Winston进行AWS Cloudwatch设置

[英]AWS Cloudwatch setup with Winston

I have been reading various articles/docs and watching some videos on this topic. 我一直在阅读各种文章/文档,并观看有关此主题的一些视频。 My issue is that they all conflict in one way or another. 我的问题是他们都以这种或那种方式发生冲突。

My goal is to use winston to send all console.logs/error messages from my ec2 server to Cloudwatch so that no logs are ever logged on the ec2 terminal itself. 我的目标是使用winston将我的ec2服务器上的所有console.logs /错误消息发送到Cloudwatch,这样就不会在ec2终端上记录任何日志。

Points of confusion: 困惑点:

  1. If I use winston-aws-cloudwatch or winston-cloudwatch , do I still need to setup an IAM user on AWS or will these auto generate logs within Cloudwatch? 如果我使用winston-aws-cloudwatchwinston-cloudwatch ,我还需要在AWS上设置IAM用户还是在Cloudwatch中自动生成日志?
  2. If I setup Cloudwatch as per AWS documentation will that automatically stream any would be console.logs from the EC2 server to Cloudwatch or will it do both? 如果我根据AWS文档设置Cloudwatch,那么会自动将任何将从EC2服务器的console.logs流式传输到Cloudwatch,还是会同时执行这两项操作? If the first one, then I don't need Winston? 如果是第一个,那么我不需要温斯顿?
  3. Can I send logs from my local development server to Cloudwatch (just for testing purposes, as soon as it is clear it works, then I would test on staging and finally move it to production) or must it come from an EC2 instance? 我可以将日志从我的本地开发服务器发送到Cloudwatch(仅用于测试目的,一旦它清楚可行,然后我将测试分段并最终将其移至生产)或者它必须来自EC2实例吗?
  4. I assume the AWS Cloudwatch key is the same as the AWS key I use for the rest of my account? 我假设AWS Cloudwatch密钥与我用于其余帐户的AWS密钥相同?

Present code: 现有代码:

var winston = require('winston'),
  CloudWatchTransport = require('winston-aws-cloudwatch');

const logger = new winston.Logger({
  transports: [
    new (winston.transports.Console)({
      timestamp: true,
      colorize: true
    })
  ]
});

const cloudwatchConfig = {
  logGroupName: 'groupName',
  logStreamName: 'streamName',
  createLogGroup: false,
  createLogStream: true,
  awsConfig: {
    aws_access_key_id: process.env.AWS_KEY_I_USE_FOR_AWS,
    aws_secret_access_key: process.env.AWS_SECRET_KEY_I_USE_FOR_AWS,
    region: process.env.REGION_CLOUDWATCH_IS_IN
  },
  formatLog: function (item) {
    return item.level + ': ' + item.message + ' ' + JSON.stringify(item.meta)
  }
};

logger.level = 3;

if (process.env.NODE_ENV === 'development') logger.add(CloudWatchTransport, cloudwatchConfig);

logger.stream = {
  write: function(message, encoding) {
    logger.info(message);
  }
};

logger.error('Test log');
  1. Yes
  2. Depends on the transports you configure. 取决于您配置的传输。 If you configure only CloudWatch than it will only end up there. 如果您只配置CloudWatch,那么它只会在那里结束。 Currently your code has 2 transports, the normal Console one and the CloudWatchTransport so with your current code, both. 目前,您的代码有两个传输,一个是普通的Console,另一个是CloudWatchTransport和当前代码。
  3. As long as you specify your keys as you would normally do with any AWS service (S3, DB, ...) you can push logs from your local/dev device to CloudWatch. 只要您像通常使用任何AWS服务(S3,DB,...)一样指定密钥,就可以将日志从本地/ dev设备推送到CloudWatch。
  4. Depends on your IAM user if he has the privileges or not. 取决于您的IAM用户是否拥有权限。 But it is possible yes. 但是可能是的。

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

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