-
Notifications
You must be signed in to change notification settings - Fork 489
Update global logger to support logging with level and arguments. #2005
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "Amazon.Lambda.RuntimeSupport", | ||
"Type": "Minor", | ||
"ChangelogMessages": [ | ||
"Add support for parameterized logging method to global logger LambdaLogger in Amazon.Lambda.Core" | ||
] | ||
}, | ||
{ | ||
"Name": "Amazon.Lambda.RuntimeSupport", | ||
"Type": "Patch", | ||
"ChangelogMessages": [ | ||
"Add support for parameterized logging method to global logger LambdaLogger. Method is marked as preview till new version of Amazon.Lambda.RuntimeSupport is deployed to managed runtime." | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
using System; | ||
using System; | ||
using System.Reflection.Emit; | ||
using System.Runtime.Versioning; | ||
using System.Text; | ||
|
||
namespace Amazon.Lambda.Core | ||
{ | ||
|
@@ -9,8 +12,11 @@ namespace Amazon.Lambda.Core | |
/// </summary> | ||
public static class LambdaLogger | ||
{ | ||
// Logging action, logs to Console by default | ||
// The name of this field must not change or be readonly because Amazon.Runtime.Support will use reflection to replace the | ||
// value with an Action that directs the logging into its logging system. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong name "Amazon.Runtime.Support" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
#pragma warning disable IDE0044 // Add readonly modifier | ||
private static Action<string> _loggingAction = LogToConsole; | ||
#pragma warning restore IDE0044 // Add readonly modifier | ||
|
||
// Logs message to console | ||
private static void LogToConsole(string message) | ||
|
@@ -29,5 +35,66 @@ public static void Log(string message) | |
{ | ||
_loggingAction(message); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
// The name of this field must not change or be readonly because Amazon.Runtime.Support will use reflection to replace the | ||
// value with an Action that directs the logging into its logging system. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong name "Amazon.Runtime.Support" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
#pragma warning disable IDE0044 // Add readonly modifier | ||
private static Action<string, string, object[]> _loggingWithLevelAction = LogWithLevelToConsole; | ||
#pragma warning restore IDE0044 // Add readonly modifier | ||
|
||
// Logs message to console | ||
private static void LogWithLevelToConsole(string level, string message, params object[] args) | ||
{ | ||
// Formatting here is not important, it is used for debugging Amazon.Lambda.Core only. | ||
// In a real scenario Amazon.Runtime.Support will change the value of _loggingWithLevelAction | ||
// to an Action inside it's logging system to handle the real formatting. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong name "Amazon.Runtime.Support" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
var sb = new StringBuilder(); | ||
sb.Append(level).Append(": ").Append(message); | ||
if (args?.Length > 0) | ||
{ | ||
sb.Append(" Arguments:"); | ||
foreach(var arg in args) | ||
{ | ||
sb.Append(" \""); | ||
sb.Append(arg); | ||
sb.Append("\""); | ||
} | ||
} | ||
Console.WriteLine(sb.ToString()); | ||
} | ||
|
||
private const string ParameterizedPreviewMessage = | ||
"This method has been mark as preview till the Lambda .NET Managed runtime has been updated with the backing implementation of this method. " + | ||
"It is possible to use this method whilein preview if the Lambda function is deployed as an executable and uses the latest version of Amazon.Lambda.RuntimeSupport."; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: typo in "whilein" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
/// <summary> | ||
/// Logs a message to AWS CloudWatch Logs. | ||
/// | ||
/// Logging will not be done: | ||
/// If the role provided to the function does not have sufficient permissions. | ||
/// </summary> | ||
/// <param name="level">The log level of the message</param> | ||
/// <param name="message">Message to log. The message may have format arguments.</param> | ||
/// <param name="args">Arguments to format the message with.</param> | ||
[RequiresPreviewFeatures(ParameterizedPreviewMessage)] | ||
public static void Log(string level, string message, params object[] args) | ||
{ | ||
_loggingWithLevelAction(level, message, args); | ||
} | ||
|
||
/// <summary> | ||
/// Logs a message to AWS CloudWatch Logs. | ||
/// | ||
/// Logging will not be done: | ||
/// If the role provided to the function does not have sufficient permissions. | ||
/// </summary> | ||
/// <param name="level">The log level of the message</param> | ||
/// <param name="message">Message to log. The message may have format arguments.</param> | ||
/// <param name="args">Arguments to format the message with.</param> | ||
[RequiresPreviewFeatures(ParameterizedPreviewMessage)] | ||
public static void Log(LogLevel level, string message, params object[] args) => Log(level.ToString(), message, args); | ||
#endif | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to have 2 entries for RuntimeSupport? Seems like both can be combined into 1.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. The second one is supposed to be
Amazon.Lambda.Core
which I marked aspreview
since the API is in preview status. It will get a minor version bump once the API is out of preview.