ILambdaLogger
should support parameterized and structured logging
#1268
Labels
feature-request
A feature should be added or improved.
module/lambda-client-lib
p2
This is a standard priority issue
queued
Describe the feature
The
dotnet6
Lambda runtime should support parameterized and structured logging methods.Note: This issue continues the conversation in #1232. We believe structured logging would benefit customers, but we understand that we want to minimize the dependencies of
Amazon.Lambda.Core
. So we are proposing a different approach.Related issues:
Use Case
Currently, the
ILambdaLogger
interface only supports writing a message string to the log. While this is convienent for simple logging, sometimes it is desired to write logs in a structured format (a.k.a structured logging) to support processing and querying. For example, writing logs in JSON will allow users to have flexible queries with CloudWatch Logs insight. The current interface makes it difficult to do so because there is no support for parameterized logging.An example of parameterized logging is the methods defined for Microsoft's
ILogger
: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerExtensions.cs.Another benefit of parameterized logging is efficiency. Currently,
ILambdaLogger
requires forming log message strings, regardless of the configured log level. For example, even when we configureAWS_LAMBDA_HANDLER_LOG_LEVEL
toWarning
,Debug
andInfo
messages still need to be formed. By supporting parameterized logging methods, log forming actions can be skipped when the corresponding log level is disabled.Proposed Solution
First, an additional
LogEntry
method is added to theILambdaLogger
interface.The purpose of this method is to have a generic way of writing a log entry with state data attached to it. The
level
specifies the log level. Theentry
parameter is what contains the log entry's properties, which are not only the log message, but also other state information, such as key-value pair data, occured exception etc.TEntry
is made generic on purpose to allow different handlings based on different types of logs. For example, theMessageEntry
defined below would be one type of log, but if in the future we want to support CloudWatch EMF format, a different log type can be defined and handled separately.To support structured logging, a
MessageEntry
structure, which represents a typical log message, is defined as follows.When the implementation of
ILambdaLogger
sees aMessageEntry
, it serializes and writes the entry to the output. The specific serialization will be defined byAWS_LAMBDA_HANDLER_LOG_FORMAT
. For currently-defined options (Default
andUnformatted
) the output format will remain the same. We propose another option namedJSON
, which writes output in the following (JSON) format:Parameterized logging
To make it easier to write structured logs,
Amazon.Lambda.Core
also provides another set of extension methods to support parameterized logging.These methods will perform the follow actions:
MessageEntry
based on parameters. TheMessageEntry
should only be a holder object which is cheap to create, and should not perform the message formatting itself.ILambdaLogger.LogEntry()
with the appropriate log level and the formed entry. When a formatted log message is required,ILambdaLogger
invokesMessageEntry.ToString()
, at which point the message is formed.ILambdaLogger
also has access to other state information to support structured logging.For example, the following code the JSON sample output above:
Other Information
Why introduce
LambdaLoggerExtensions
instead of including all parameterized methods inILambdaLogger
?The
ILambdaLogger.LogEntry
is designed to be generic, to allow extensions later if required. Introducing other parameterized methods toILambdaLogger
would pollute the interface and imply that those methods do different things, while in fact, they all delegate toILambdaLogger.LogEntry
.Idea: support CloudWatch EMF log format?
Emitting metrics using CloudWatch EMF log format from Lambda is a common and supported use case. With the APIs defined in this proposal we can support EMF logs by defining a new entry type
EmfEntry
, and handle it in theILambdaLogger.LogEntry
:Specific details for this will probably be in a future proposal (if this one got accepted) but I hope this clarifies the purpose of defining a generic log interface :).
Acknowledgements
AWS .NET SDK and/or Package version used
"Amazon.Lambda.Core" Version="2.1.0"
Targeted .NET Platform
.NET 6
Operating System and version
Amazon Linux
The text was updated successfully, but these errors were encountered: