[英]How to disable default log messages from lambda in python
I have an AWS Lambda function written in python, and i need only the messages I log in CloudWatch Logs.我有一个写在 python 中的 AWS Lambda function,我只需要在 CloudWatch Logs 中记录的消息。 I have tried the example given in watchtower, but it still didn't work.
我已经尝试了watchtower中给出的例子,但它仍然没有用。
START RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5 Version: $LATEST
(randomiser) Hello from Lambda
END RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5
REPORT RequestId: d0ba05dc-8506-11e8-82ab-afe2adba36e5
Duration: 0.44 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB*
From the above I only need (randomiser) Hello from Lambda
to be logged in CloudWatch, without the START
, END
and REPORT
lines.从上面我只需要
(randomiser) Hello from Lambda
登录 CloudWatch,没有START
, END
和REPORT
行。
If you have logs enabled, you are always going to get the default logs.如果您启用了日志,您将始终获得默认日志。 No way you can disable them.
您无法禁用它们。
However there might be cases where you want one specific Lambda function to not send logs at all.但是,在某些情况下,您可能希望一个特定的 Lambda 函数根本不发送日志。 You can solve this by creating a new role specifically for that Lambda function, and not have the logging permission there.
您可以通过专门为该 Lambda 函数创建一个新角色来解决此问题,并且在那里没有日志记录权限。
FWIW, if you need to toggle between logging and no logging frequently, you can have a policy file as the following. FWIW,如果您需要经常在记录和不记录之间切换,您可以拥有如下的策略文件。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": [ "arn:aws:logs:*:*:*" ] } ] }
and change the "Deny" to "Allow" when you require logging.并在需要记录时将“拒绝”更改为“允许”。
There is no direct way to disable these logs.没有直接的方法可以禁用这些日志。 However, a simple workaround is to remove the CloudWatch Logs permission from the Lambda execution role.
但是,一个简单的解决方法是从 Lambda 执行角色中删除 CloudWatch Logs 权限。 Lambda function uses this role to access other AWS services, if you remove CloudWatch permission it will not be able to push logs to CloudWatch.
Lambda 函数使用此角色访问其他 AWS 服务,如果您删除 CloudWatch 权限,它将无法将日志推送到 CloudWatch。
Note: if you do this you will not able to push any logs from lambda to CloudWatch注意:如果您这样做,您将无法将任何日志从 lambda 推送到 CloudWatch
In the AWS Lambda configuration you'll have a CloudWatch trigger configured so that the lambda is triggered by new log entries in CloudWatch.在 AWS Lambda 配置中,您将配置一个 CloudWatch 触发器,以便 lambda 由 CloudWatch 中的新日志条目触发。 In that trigger configuration, you can specify a filter pattern, and - if you do - only those log lines that match the filter will be forwarded to your lambda.
在该触发器配置中,您可以指定一个过滤器模式,并且 - 如果您这样做 - 只有那些与过滤器匹配的日志行才会转发到您的 lambda。
The caveat (according to https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax ) seems to be that the filter operates on JSON data only, I have not found a filter that operates on plain text (though, if you put your log message in quotes, it's potentially a valid JSON string and can be matched by the filter.警告(根据https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax )似乎是过滤器仅在 JSON 数据上运行,我还没有找到对纯文本进行操作的过滤器(但是,如果您将日志消息放在引号中,它可能是一个有效的 JSON 字符串并且可以被过滤器匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.