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
};
Since there's no native support for PHP in Lambda, we'll need to provide the PHP binary for Lambda to use so that we acn execute our custom runtime code.
To change the entire log level of microservice Lambda functions, you can update the CloudFormation stack parameter. When you change the Log Level parameter and update the AWS CloudFormation stack, it will automatically deploy the new log level to all AWS Lambda functions.
Using the CloudWatch console You can use the Amazon CloudWatch console to view logs for all Lambda function invocations. Open the Log groups page on the CloudWatch console. Choose the log group for your function (/aws/lambda/ your-function-name ). Choose a log stream.
This function handler name reflects the function name ( lambda_handler ) and the file where the handler code is stored ( lambda_function.py ). To change the function handler name in the Lambda console, on the Runtime settings pane, choose Edit.
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:
console.log('')
writes to lambda log, putLogEvents
writes to your custom log (i.e: myStreamName0.10141409975385463). Be aware of this.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.
You cannot change the Log Stream name. Each running container for your Lambda function logs into a different Log Stream.
If you want to easily see the logs together as one, you can stream them to Amazon ElasticSearch Service so you can use Kibana to browse through your logs or you can use CLI tools like awscli
to aggregate and browse them in your terminal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With