aws-powertools / powertools-lambda-dotnet

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/dotnet/
MIT No Attribution
158 stars 24 forks source link

Bug: Log events are being logged with "Information" level in CloudWatch despite setting the log level to "Debug" as an environment variable #159

Closed sunnygoel87 closed 1 year ago

sunnygoel87 commented 2 years ago

Expected Behaviour

Log events in CloudWatch with debug log level.

Current Behaviour

Log events are showing info log level despite setting the following environment variable in the Lambda fn.

POWERTOOLS_LOG_LEVEL: debug

Code snippet

Here is the code snippet from Function.cs file.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Logging;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace lambda_dotnet_core;

public class Function
{

    /// <summary>
    /// A simple function that takes a string and does a ToUpper
    /// </summary>
    /// <param name="input"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    [Logging]
    public string FunctionHandler(string input, ILambdaContext context)
    {
        Logger.LogInformation("Welcome to AWS Lambda Powertools for .NET");
        return input.ToUpper();
    }
}

Steps to Reproduce

1.) Create an empty .NET 6 based lambda function using the templates provided by Amazon.Lambda.Templates NuGet package. 2.) Use the code shared above in Function.cs file. 2.) Add the following nuget package - AWS.Lambda.Powertools.Logging 3.) Deploy and invoke the function a few times. 4.) You will notice the Log events with Log level "Info" 5.) Now, add the following environment variables in the Lambda fn. either manually using AWS console or AWS CLI command. POWERTOOLS_LOG_LEVEL: debug POWERTOOLS_SERVICE_NAME: <give any meaningful name>

6.) Invoke the function again a few times. You will again see the log events being logged with "Info" log level.

AWS Lambda Powertools for .NET version

latest

AWS Lambda function runtime

dotnet6

sunnygoel87 commented 2 years ago

Any update on this ?

sliedig commented 2 years ago

Apologies @sunnygoel87, we're reviewing your issue and will update soon.

amirkaws commented 2 years ago

Hi @sunnygoel87,

Thanks for raising this issue. The "POWERTOOLS_LOG_LEVEL" configuration specifies the minimum log level you desire to log. The methods such as LogTrace, LogDebug, LogInformation etc. are the ones actually specify the level of the logs.

LambdaPowertools ignores a log when the log level below the specified "POWERTOOLS_LOG_LEVEL". Here are some information about the Log level.

If you would like to specify the log level at runtime you can do so by using The Log method. The Log method's first parameter, LogLevel, indicates the severity of the log.

logger.Log(LogLevel.Information, "Welcome to AWS Lambda Powertools for .NET");
sunnygoel87 commented 2 years ago

@amirkaws - Thanks for your response and sharing the additional information about Log Level.

Can I say that If a log level is not specified explicitly (as shown in below mentioned statement), then by default Lambda Powertools logs the message with "information" Log severity.

logger.Log("Welcome to AWS Lambda Powertools for .NET");

And if in case "POWERTOOLS_LOG_LEVEL" environment variable is set to "error", then we will not even see anything in CloudWatch Log Group. Because "information" log level is below the "error" log level, correct ?

amirkaws commented 2 years ago

Hi @sunnygoel87,

No problem. no currently you need to pass the required log level or use one of the ILogger Extensions methods. However. However, I'll have a chat with other maintainer to see if we can add that method as well.

amirkaws commented 1 year ago

Close this ticket as decided to stay with ILogger Extensions methods which requires to pass the log level.