Skip to content

Commit b06fa34

Browse files
authored
Merge pull request #832 from hjgraca/feature/logger-ilogger-instance
chore: Powertools Logger v2
2 parents ed8df93 + 399ba36 commit b06fa34

File tree

100 files changed

+11367
-2983
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+11367
-2983
lines changed

docs/core/logging-v2.md

+1,529
Large diffs are not rendered by default.

docs/core/logging.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Powertools for AWS Lambda (.NET) are available as NuGet packages. You can instal
1919

2020
* [AWS.Lambda.Powertools.Logging](https://www.nuget.org/packages?q=AWS.Lambda.Powertools.Logging):
2121

22-
`dotnet add package AWS.Lambda.Powertools.Logging`
22+
`dotnet add package AWS.Lambda.Powertools.Logging --version 1.6.5`
2323

2424
## Getting started
2525

libraries/AWS.Lambda.Powertools.sln

+15
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS.Lambda.Powertools.Metri
103103
EndProject
104104
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Metrics", "Metrics", "{A566F2D7-F8FE-466A-8306-85F266B7E656}"
105105
EndProject
106+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AOT-Function-ILogger", "tests\e2e\functions\core\logging\AOT-Function-ILogger\src\AOT-Function-ILogger\AOT-Function-ILogger.csproj", "{7FC6DD65-0352-4139-8D08-B25C0A0403E3}"
107+
EndProject
106108
Global
107109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
108110
Debug|Any CPU = Debug|Any CPU
@@ -548,6 +550,18 @@ Global
548550
{F8F80477-1EAD-4C5C-A329-CBC0A60C7CAB}.Release|x64.Build.0 = Release|Any CPU
549551
{F8F80477-1EAD-4C5C-A329-CBC0A60C7CAB}.Release|x86.ActiveCfg = Release|Any CPU
550552
{F8F80477-1EAD-4C5C-A329-CBC0A60C7CAB}.Release|x86.Build.0 = Release|Any CPU
553+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
554+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
555+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|x64.ActiveCfg = Debug|Any CPU
556+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|x64.Build.0 = Debug|Any CPU
557+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|x86.ActiveCfg = Debug|Any CPU
558+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Debug|x86.Build.0 = Debug|Any CPU
559+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
560+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|Any CPU.Build.0 = Release|Any CPU
561+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|x64.ActiveCfg = Release|Any CPU
562+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|x64.Build.0 = Release|Any CPU
563+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|x86.ActiveCfg = Release|Any CPU
564+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3}.Release|x86.Build.0 = Release|Any CPU
551565
EndGlobalSection
552566

553567
GlobalSection(NestedProjects) = preSolution
@@ -596,5 +610,6 @@ Global
596610
{A566F2D7-F8FE-466A-8306-85F266B7E656} = {1CFF5568-8486-475F-81F6-06105C437528}
597611
{F8F80477-1EAD-4C5C-A329-CBC0A60C7CAB} = {A566F2D7-F8FE-466A-8306-85F266B7E656}
598612
{A422C742-2CF9-409D-BDAE-15825AB62113} = {A566F2D7-F8FE-466A-8306-85F266B7E656}
613+
{7FC6DD65-0352-4139-8D08-B25C0A0403E3} = {4EAB66F9-C9CB-4E8A-BEE6-A14CD7FDE02F}
599614
EndGlobalSection
600615
EndGlobal

libraries/src/AWS.Lambda.Powertools.BatchProcessing/AWS.Lambda.Powertools.BatchProcessing.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
<PackageReference Include="Amazon.Lambda.DynamoDBEvents" />
1414
<PackageReference Include="Amazon.Lambda.KinesisEvents" />
1515
<PackageReference Include="Amazon.Lambda.SQSEvents" />
16-
<ProjectReference Include="..\AWS.Lambda.Powertools.Common\AWS.Lambda.Powertools.Common.csproj" Condition="'$(Configuration)'=='Debug'"/>
16+
<ProjectReference Include="..\AWS.Lambda.Powertools.Common\AWS.Lambda.Powertools.Common.csproj"/>
1717
</ItemGroup>
1818
</Project>

libraries/src/AWS.Lambda.Powertools.Common/Core/ConsoleWrapper.cs

+54-5
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,67 @@
1414
*/
1515

1616
using System;
17+
using System.IO;
1718

1819
namespace AWS.Lambda.Powertools.Common;
1920

2021
/// <inheritdoc />
2122
public class ConsoleWrapper : IConsoleWrapper
2223
{
24+
private static bool _override;
25+
2326
/// <inheritdoc />
24-
public void WriteLine(string message) => Console.WriteLine(message);
25-
/// <inheritdoc />
26-
public void Debug(string message) => System.Diagnostics.Debug.WriteLine(message);
27+
public void WriteLine(string message)
28+
{
29+
OverrideLambdaLogger();
30+
Console.WriteLine(message);
31+
}
32+
2733
/// <inheritdoc />
28-
public void Error(string message) => Console.Error.WriteLine(message);
34+
public void Debug(string message)
35+
{
36+
OverrideLambdaLogger();
37+
System.Diagnostics.Debug.WriteLine(message);
38+
}
39+
2940
/// <inheritdoc />
30-
public string ReadLine() => Console.ReadLine();
41+
public void Error(string message)
42+
{
43+
if (!_override)
44+
{
45+
var errordOutput = new StreamWriter(Console.OpenStandardError());
46+
errordOutput.AutoFlush = true;
47+
Console.SetError(errordOutput);
48+
}
49+
50+
Console.Error.WriteLine(message);
51+
}
52+
53+
internal static void SetOut(StringWriter consoleOut)
54+
{
55+
_override = true;
56+
Console.SetOut(consoleOut);
57+
}
58+
59+
private void OverrideLambdaLogger()
60+
{
61+
if (_override)
62+
{
63+
return;
64+
}
65+
// Force override of LambdaLogger
66+
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
67+
standardOutput.AutoFlush = true;
68+
Console.SetOut(standardOutput);
69+
}
70+
71+
internal static void WriteLine(string logLevel, string message)
72+
{
73+
Console.WriteLine($"{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}\t{logLevel}\t{message}");
74+
}
75+
76+
public static void ResetForTest()
77+
{
78+
_override = false;
79+
}
3180
}

libraries/src/AWS.Lambda.Powertools.Common/Core/IConsoleWrapper.cs

-6
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,4 @@ public interface IConsoleWrapper
3737
/// </summary>
3838
/// <param name="message">The error message to write.</param>
3939
void Error(string message);
40-
41-
/// <summary>
42-
/// Reads the next line of characters from the standard input stream.
43-
/// </summary>
44-
/// <returns>The next line of characters from the input stream, or null if no more lines are available.</returns>
45-
string ReadLine();
4640
}

libraries/src/AWS.Lambda.Powertools.Common/Core/IPowertoolsEnvironment.cs

+6
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public interface IPowertoolsEnvironment
3434
/// <typeparam name="T"></typeparam>
3535
/// <returns>Assembly Version in the Major.Minor.Build format</returns>
3636
string GetAssemblyVersion<T>(T type);
37+
38+
/// <summary>
39+
/// Sets the execution Environment Variable (AWS_EXECUTION_ENV)
40+
/// </summary>
41+
/// <param name="type"></param>
42+
void SetExecutionEnvironment<T>(T type);
3743
}

libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsConfigurations.cs

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
66
* A copy of the License is located at
7-
*
7+
*
88
* http://aws.amazon.com/apache2.0
9-
*
9+
*
1010
* or in the "license" file accompanying this file. This file is distributed
1111
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212
* express or implied. See the License for the specific language governing
@@ -25,6 +25,8 @@ namespace AWS.Lambda.Powertools.Common;
2525
/// <seealso cref="IPowertoolsConfigurations" />
2626
public class PowertoolsConfigurations : IPowertoolsConfigurations
2727
{
28+
private readonly IPowertoolsEnvironment _powertoolsEnvironment;
29+
2830
/// <summary>
2931
/// The maximum dimensions
3032
/// </summary>
@@ -40,26 +42,21 @@ public class PowertoolsConfigurations : IPowertoolsConfigurations
4042
/// </summary>
4143
private static IPowertoolsConfigurations _instance;
4244

43-
/// <summary>
44-
/// The system wrapper
45-
/// </summary>
46-
private readonly ISystemWrapper _systemWrapper;
47-
4845
/// <summary>
4946
/// Initializes a new instance of the <see cref="PowertoolsConfigurations" /> class.
5047
/// </summary>
51-
/// <param name="systemWrapper">The system wrapper.</param>
52-
internal PowertoolsConfigurations(ISystemWrapper systemWrapper)
48+
/// <param name="powertoolsEnvironment"></param>
49+
internal PowertoolsConfigurations(IPowertoolsEnvironment powertoolsEnvironment)
5350
{
54-
_systemWrapper = systemWrapper;
51+
_powertoolsEnvironment = powertoolsEnvironment;
5552
}
5653

5754
/// <summary>
5855
/// Gets the instance.
5956
/// </summary>
6057
/// <value>The instance.</value>
6158
public static IPowertoolsConfigurations Instance =>
62-
_instance ??= new PowertoolsConfigurations(SystemWrapper.Instance);
59+
_instance ??= new PowertoolsConfigurations(PowertoolsEnvironment.Instance);
6360

6461
/// <summary>
6562
/// Gets the environment variable.
@@ -68,7 +65,7 @@ internal PowertoolsConfigurations(ISystemWrapper systemWrapper)
6865
/// <returns>System.String.</returns>
6966
public string GetEnvironmentVariable(string variable)
7067
{
71-
return _systemWrapper.GetEnvironmentVariable(variable);
68+
return _powertoolsEnvironment.GetEnvironmentVariable(variable);
7269
}
7370

7471
/// <summary>
@@ -79,7 +76,7 @@ public string GetEnvironmentVariable(string variable)
7976
/// <returns>System.String.</returns>
8077
public string GetEnvironmentVariableOrDefault(string variable, string defaultValue)
8178
{
82-
var result = _systemWrapper.GetEnvironmentVariable(variable);
79+
var result = _powertoolsEnvironment.GetEnvironmentVariable(variable);
8380
return string.IsNullOrWhiteSpace(result) ? defaultValue : result;
8481
}
8582

@@ -91,7 +88,7 @@ public string GetEnvironmentVariableOrDefault(string variable, string defaultVal
9188
/// <returns>System.Int32.</returns>
9289
public int GetEnvironmentVariableOrDefault(string variable, int defaultValue)
9390
{
94-
var result = _systemWrapper.GetEnvironmentVariable(variable);
91+
var result = _powertoolsEnvironment.GetEnvironmentVariable(variable);
9592
return int.TryParse(result, out var parsedValue) ? parsedValue : defaultValue;
9693
}
9794

@@ -103,7 +100,7 @@ public int GetEnvironmentVariableOrDefault(string variable, int defaultValue)
103100
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
104101
public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
105102
{
106-
return bool.TryParse(_systemWrapper.GetEnvironmentVariable(variable), out var result)
103+
return bool.TryParse(_powertoolsEnvironment.GetEnvironmentVariable(variable), out var result)
107104
? result
108105
: defaultValue;
109106
}
@@ -161,7 +158,8 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
161158
/// </summary>
162159
/// <value>The logger sample rate.</value>
163160
public double LoggerSampleRate =>
164-
double.TryParse(_systemWrapper.GetEnvironmentVariable(Constants.LoggerSampleRateNameEnv), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var result)
161+
double.TryParse(_powertoolsEnvironment.GetEnvironmentVariable(Constants.LoggerSampleRateNameEnv),
162+
NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var result)
165163
? result
166164
: 0;
167165

@@ -191,7 +189,7 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
191189
/// </summary>
192190
/// <value><c>true</c> if this instance is Lambda; otherwise, <c>false</c>.</value>
193191
public bool IsLambdaEnvironment => GetEnvironmentVariable(Constants.LambdaTaskRoot) is not null;
194-
192+
195193
/// <summary>
196194
/// Gets a value indicating whether [tracing is disabled].
197195
/// </summary>
@@ -202,28 +200,32 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
202200
/// <inheritdoc />
203201
public void SetExecutionEnvironment<T>(T type)
204202
{
205-
_systemWrapper.SetExecutionEnvironment(type);
203+
_powertoolsEnvironment.SetExecutionEnvironment(type);
206204
}
207205

208206
/// <inheritdoc />
209207
public bool IdempotencyDisabled =>
210208
GetEnvironmentVariableOrDefault(Constants.IdempotencyDisabledEnv, false);
211209

212210
/// <inheritdoc />
213-
public string BatchProcessingErrorHandlingPolicy => GetEnvironmentVariableOrDefault(Constants.BatchErrorHandlingPolicyEnv, "DeriveFromEvent");
211+
public string BatchProcessingErrorHandlingPolicy =>
212+
GetEnvironmentVariableOrDefault(Constants.BatchErrorHandlingPolicyEnv, "DeriveFromEvent");
214213

215214
/// <inheritdoc />
216-
public bool BatchParallelProcessingEnabled => GetEnvironmentVariableOrDefault(Constants.BatchParallelProcessingEnabled, false);
215+
public bool BatchParallelProcessingEnabled =>
216+
GetEnvironmentVariableOrDefault(Constants.BatchParallelProcessingEnabled, false);
217217

218218
/// <inheritdoc />
219-
public int BatchProcessingMaxDegreeOfParallelism => GetEnvironmentVariableOrDefault(Constants.BatchMaxDegreeOfParallelismEnv, 1);
219+
public int BatchProcessingMaxDegreeOfParallelism =>
220+
GetEnvironmentVariableOrDefault(Constants.BatchMaxDegreeOfParallelismEnv, 1);
220221

221222
/// <inheritdoc />
222-
public bool BatchThrowOnFullBatchFailureEnabled => GetEnvironmentVariableOrDefault(Constants.BatchThrowOnFullBatchFailureEnv, true);
223+
public bool BatchThrowOnFullBatchFailureEnabled =>
224+
GetEnvironmentVariableOrDefault(Constants.BatchThrowOnFullBatchFailureEnv, true);
223225

224226
/// <inheritdoc />
225227
public bool MetricsDisabled => GetEnvironmentVariableOrDefault(Constants.PowertoolsMetricsDisabledEnv, false);
226-
228+
227229
/// <inheritdoc />
228230
public bool IsColdStart => LambdaLifecycleTracker.IsColdStart;
229231

libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsEnvironment.cs

+49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23

34
namespace AWS.Lambda.Powertools.Common;
45

@@ -40,4 +41,52 @@ public string GetAssemblyVersion<T>(T type)
4041
var version = type.GetType().Assembly.GetName().Version;
4142
return version != null ? $"{version.Major}.{version.Minor}.{version.Build}" : string.Empty;
4243
}
44+
45+
/// <inheritdoc />
46+
public void SetExecutionEnvironment<T>(T type)
47+
{
48+
const string envName = Constants.AwsExecutionEnvironmentVariableName;
49+
var envValue = new StringBuilder();
50+
var currentEnvValue = GetEnvironmentVariable(envName);
51+
var assemblyName = ParseAssemblyName(GetAssemblyName(type));
52+
53+
// If there is an existing execution environment variable add the annotations package as a suffix.
54+
if (!string.IsNullOrEmpty(currentEnvValue))
55+
{
56+
// Avoid duplication - should not happen since the calling Instances are Singletons - defensive purposes
57+
if (currentEnvValue.Contains(assemblyName))
58+
{
59+
return;
60+
}
61+
62+
envValue.Append($"{currentEnvValue} ");
63+
}
64+
65+
var assemblyVersion = GetAssemblyVersion(type);
66+
67+
envValue.Append($"{assemblyName}/{assemblyVersion}");
68+
69+
SetEnvironmentVariable(envName, envValue.ToString());
70+
}
71+
72+
/// <summary>
73+
/// Parsing the name to conform with the required naming convention for the UserAgent header (PTFeature/Name/Version)
74+
/// Fallback to Assembly Name on exception
75+
/// </summary>
76+
/// <param name="assemblyName"></param>
77+
/// <returns></returns>
78+
private string ParseAssemblyName(string assemblyName)
79+
{
80+
try
81+
{
82+
var parsedName = assemblyName.Substring(assemblyName.LastIndexOf(".", StringComparison.Ordinal) + 1);
83+
return $"{Constants.FeatureContextIdentifier}/{parsedName}";
84+
}
85+
catch
86+
{
87+
//NOOP
88+
}
89+
90+
return $"{Constants.FeatureContextIdentifier}/{assemblyName}";
91+
}
4392
}

0 commit comments

Comments
 (0)