Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw data logging support for LambdaLogger #1238

Closed
wants to merge 2 commits into from

Conversation

dhhoang
Copy link

@dhhoang dhhoang commented Jul 11, 2022

Issue #, if available: : #1232

Description of changes: Add an overload to LambdaLogger.Log() to allow logging using raw data.

Overview

In #1232 we brought up our use case of ILogger interface. Before we can implement such a logger we need to have a way to forward raw UTF8 data to the output. In this PR we propose that an overload is added to LambdaLogger (for .NET 6 runtime only)

#if NET6_0_OR_GREATER
/// <summary>
/// Logs a message to AWS CloudWatch Logs. <br/>
/// Logging will not be done:
/// If the role provided to the function does not have sufficient permissions.
/// </summary>
/// <param name="utf8Message">The message as UTF-8 encoded data.</param>
public static void Log(ReadOnlySpan<byte> utf8Message)
#endif

This will be implemented in a similar fashion to Log(string): it's delegated to a default method but LogLevelLoggerWriter will replace that method with the formatted version using reflection.
To do this the output Stream is passed to WrapperTextWriter. If the Telemetry logging file descriptor is available this will be the FileStream wrapper for that FD, otherwise the stream is set to NULL and the logging action is delegated back to FormattedWriteLine()

Testing

Because this new function is only supported on .NET 6 and higher we added net6.0 target to Amazon.Lambda.RuntimeSupport.UnitTests.csproj.
We added equivalent tests to existing tests for FileDescriptorLogFactory.FileDescriptorLogStream (raw data writing variant). We also added several unit tests to LogLevelLoggerWriter.
We also fixed a failing test : HandlerTests.PositiveHandlerTestsAsync. This test is failing for .NET 6 because by default the LambdaBootstrap always initializes the LogLevelLoggerWriter instance, which in turn redirects the LambdaLogger output, causing the logs to not be redirected to the test. This is fixed by creating another constructor for LambdaBootstrap to accept a IConsoleLoggerWriter instance which the test itself provides.

Remarks

There are two places whether the logging action of LambdaLogger is set. The first place is LogLevelLoggerWriter which we include in this PR. The second place is in UserCodeLoader.SetCustomerLoggerLogAction(). We are still not sure what the usage of this method. I'm happy to hear about this usage and amend the PR.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dhhoang
Copy link
Author

dhhoang commented Aug 8, 2022

Closing in favor or #1232

@dhhoang dhhoang closed this Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant