-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathLambdaLogger.cs
More file actions
137 lines (125 loc) · 6.6 KB
/
LambdaLogger.cs
File metadata and controls
137 lines (125 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Reflection.Emit;
using System.Runtime.Versioning;
using System.Text;
namespace Amazon.Lambda.Core
{
/// <summary>
/// Static class which sends a message to AWS CloudWatch Logs.
/// When used outside of a Lambda environment, logs are written to
/// Console.Out.
/// </summary>
public static class LambdaLogger
{
// The name of this field must not change or be readonly because Amazon.Lambda.RuntimeSupport will use reflection to replace the
// value with an Action that directs the logging into its logging system.
#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)
{
Console.WriteLine(message);
}
/// <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="message"></param>
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.Lambda.RuntimeSupport will use reflection to replace the
// value with an Action that directs the logging into its logging system.
#pragma warning disable IDE0044 // Add readonly modifier
private static Action<string, string, object[]> _loggingWithLevelAction = LogWithLevelToConsole;
private static Action<string, Exception, string, object[]> _loggingWithLevelAndExceptionAction = LogWithLevelAndExceptionToConsole;
#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.Lambda.RuntimeSupport will change the value of _loggingWithLevelAction
// to an Action inside it's logging system to handle the real formatting.
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 static void LogWithLevelAndExceptionToConsole(string level, Exception exception, string message, params object[] args)
{
// Formatting here is not important, it is used for debugging Amazon.Lambda.Core only.
// In a real scenario Amazon.Lambda.RuntimeSupport will change the value of _loggingWithLevelAction
// to an Action inside it's logging system to handle the real formatting.
LogWithLevelToConsole(level, message, args);
Console.WriteLine(exception);
}
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 while in preview if the Lambda function is deployed as an executable and uses the latest version of Amazon.Lambda.RuntimeSupport.";
/// <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>
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>
public static void Log(LogLevel level, string message, params object[] args) => Log(level.ToString(), 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="exception">Exception to include with the logging.</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, Exception exception, string message, params object[] args)
{
_loggingWithLevelAndExceptionAction(level, exception, 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="exception">Exception to include with the logging.</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, Exception exception, string message, params object[] args) => Log(level.ToString(), exception, message, args);
#endif
}
}