为AWS Lambda调用设置LogStreamName [英] Set LogStreamName for AWS Lambda call

查看:104
本文介绍了为AWS Lambda调用设置LogStreamName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将node.js函数部署到AWS Lambda上。调用它们时,它们会自动生成一个AWS CloudWatch日志。日志组设置为Lambda函数的名称,这很有用。但是日志流的命名如下: 2018/02/14 / [$ LATEST] 794dbaf40a7846c4984ad80ebf110544



< a href = https://i.stack.imgur.com/71QU6.png rel = nofollow noreferrer>



这在搜索错误时没有帮助,因为我需要检查多个日志流,因为我不知道哪个日志流是正确的。



有什么方法可以定义日志流名称,以使人类更容易理解?



node.js代码类似于以下内容:

  exports.handler = function(事件,上下文){
console.log('使用AWS-Request-Id'+ context.awsRequestId +'')调用'+ context.functionName +');
//做某事。这里
};


解决方案

检查函数的上下文。它具有属性 context.logStreamName 。您可以将其更改为任何唯一的名称。



请记住,如果要将日志附加到现有流中,则必须保留其令牌。这就是为什么我为每个lambda调用创建新的日志流的原因。另外,我正在使用guid创建流名称(例如:原因为何创建日志+ guid(),时间戳也可以)。



在下一篇文章中查看更多详细信息-



我的作品流名称更有意义,而不是名称+随机。并且他们使用guid或时间戳作为后缀。我认为随机使用是不安全的。


We deploy node.js functions onto AWS Lambda. When calling them, they auto-generate an AWS CloudWatch log. The log group is set to the name of the Lambda function, that´s helpful. But the log stream is named like this: 2018/02/14/[$LATEST]794dbaf40a7846c4984ad80ebf110544.

That is not helpful when searching for errors since I need to check multiple log streams, because I do not know which one is the correct one.

Is there any way to define the log stream name, so that it is more readable for a human being?

The node.js code looks similar to this:

exports.handler = function (event, context) {
    console.log('Called "' + context.functionName + '" with AWS-Request-Id "' + context.awsRequestId + '"');
    // do sth. here
};

解决方案

Check out the context of your function. It has property context.logStreamName. You could change it to any unique name.

Keep in mind, if you want to append your logs to existed stream, you have to persist its token. It was the reason why I created new log stream for each lambda call. Also, I am using guid for creating stream name (like: reason why log created + guid(), timestamp is ok as well).

Check more details in the next article - The Context Object (Node.js)

EDIT: Don't pay attention to my previous answer. It was completely wrong.

I checked my code how I implemented the same functionality. It isn't possible to change group or stream name from the context. But you could use CloudWatchLogs in your code for putting logs in specific group and stream.

My code:

var AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    // TODO implement
    var cloudwatchlogs = new AWS.CloudWatchLogs({ apiVersion: '2014-03-28' });

    // create new stream name for each request
    // because I don't persist sequenceToken
    var logStreamName = "myStreamName" + Math.random()

    var params = {
        logGroupName: 'myLogGroup',
        logStreamName: logStreamName
    };
    cloudwatchlogs.createLogStream(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else {
            var params = {
                logEvents: [{
                        message: 'log message',
                        timestamp: new Date().getTime()
                    },
                    {
                        message: 'one more log message',
                        timestamp: new Date().getTime()
                    }
                ],
                logGroupName: 'myLogGroup',
                logStreamName: logStreamName
            };

            cloudwatchlogs.putLogEvents(params, function(err, data) {
                if (err) console.log(err, err.stack); // an error occurred
                else console.log(data); // successful response
            });
        }
    });
    callback(null, 'Hello from Lambda');
};

Disadvantages of this approach are:

  • Your lambda creates two logGroups and two logStreams in accordance. One of your lambda, another custom. console.log('') writes to lambda log, putLogEvents writes to your custom log (i.e: myStreamName0.10141409975385463). Be aware of this.
  • You have to create manually 'myLogGroup' (how I did) or implement code that will create a logGroup if it doesn't exist.

The result of this testing code looks like:

My productions stream names have more sense rather name + random. And they are using guid or timestamp as a suffix. I don't think that it is safe to use random.

这篇关于为AWS Lambda调用设置LogStreamName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆