Skip to content

Commit 4c8cec9

Browse files
authored
Merge pull request #869 from hjgraca/chore/logger-formatting-update
chore: Logging improved message formatting
2 parents b015816 + 5da8fbb commit 4c8cec9

File tree

6 files changed

+88
-951
lines changed

6 files changed

+88
-951
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Logger.ExtraKeysLogs.cs

Lines changed: 0 additions & 439 deletions
This file was deleted.

libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs

Lines changed: 1 addition & 471 deletions
Large diffs are not rendered by default.

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using AWS.Lambda.Powertools.Logging.Internal;
2626
using AWS.Lambda.Powertools.Logging.Serializers;
2727
using AWS.Lambda.Powertools.Logging.Tests.Handlers;
28+
using Microsoft.Extensions.Logging;
2829
using Microsoft.Extensions.Options;
2930
using NSubstitute;
3031
using NSubstitute.ExceptionExtensions;
@@ -193,7 +194,7 @@ public void Log_WhenCustomFormatter_LogsCustomFormat()
193194
};
194195

195196
// Act
196-
logger.LogInformation(scopeExtraKeys, message);
197+
logger.LogInformation(message, scopeExtraKeys);
197198

198199
// Assert
199200
logFormatter.Received(1).FormatLogEntry(Arg.Is<LogEntry>

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormattingTests.cs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,9 @@ public void Should_Log_Multiple_Formats_No_Duplicates()
502502
TimeStamp = "FakeTime"
503503
};
504504

505-
Logger.LogInformation<User>(user, "{Name} and is {Age} years old", new object[]{user.Name, user.Age});
506-
Assert.Contains("\"first_name\":\"John\"", output.ToString());
507-
Assert.Contains("\"last_name\":\"Doe\"", output.ToString());
508-
Assert.Contains("\"age\":42", output.ToString());
505+
Logger.LogInformation("{Name} is {Age} years old", user.Name, user.Age);
506+
507+
Assert.Contains("\"message\":\"John Doe is 42 years old\"", output.ToString());
509508
Assert.Contains("\"name\":\"AWS.Lambda.Powertools.Logging.Logger\"", output.ToString()); // does not override name
510509

511510
output.Clear();
@@ -564,16 +563,13 @@ public void Should_Log_Multiple_Formats()
564563
Age = 42
565564
};
566565

567-
Logger.LogInformation<User>(user, "{Name} is {Age} years old", new object[]{user.FirstName, user.Age});
566+
Logger.LogInformation("{Name} is {Age} years old", user.FirstName, user.Age);
568567

569568
var logOutput = output.ToString();
570569
Assert.Contains("\"level\":\"Information\"", logOutput);
571570
Assert.Contains("\"message\":\"John is 42 years old\"", logOutput);
572571
Assert.Contains("\"service\":\"log-level-test-service\"", logOutput);
573572
Assert.Contains("\"name\":\"AWS.Lambda.Powertools.Logging.Logger\"", logOutput);
574-
Assert.Contains("\"first_name\":\"John\"", logOutput);
575-
Assert.Contains("\"last_name\":\"Doe\"", logOutput);
576-
Assert.Contains("\"age\":42", logOutput);
577573

578574
output.Clear();
579575

@@ -630,8 +626,63 @@ public void Should_Log_Multiple_Formats()
630626
Assert.Contains("\"level\":\"Information\"", logOutput);
631627
Assert.DoesNotContain("\"level\":\"fakeLevel\"", logOutput);
632628

629+
output.Clear();
630+
631+
Logger.LogInformation("{Name} is {Age} years old and {@user}", user.FirstName, user.Age, user);
632+
633+
logOutput = output.ToString();
634+
635+
Assert.Contains("\"message\":\"John is 42 years old and Doe, John (42)\"", logOutput);
636+
// Verify serialized user object with all properties
637+
Assert.Contains("\"user\":{", logOutput);
638+
Assert.Contains("\"first_name\":\"John\"", logOutput);
639+
Assert.Contains("\"last_name\":\"Doe\"", logOutput);
640+
Assert.Contains("\"age\":42", logOutput);
641+
Assert.Contains("\"name\":\"John Doe\"", logOutput);
642+
Assert.Contains("\"time_stamp\":null", logOutput);
643+
Assert.Contains("}", logOutput);
644+
633645
_output.WriteLine(logOutput);
646+
}
647+
648+
[Fact]
649+
public void TestMessageTemplateFormatting()
650+
{
651+
var output = new TestLoggerOutput();
652+
var logger = LoggerFactory.Create(builder =>
653+
{
654+
builder.AddPowertoolsLogger(config =>
655+
{
656+
config.Service = "template-format-service";
657+
config.MinimumLogLevel = LogLevel.Debug;
658+
config.LoggerOutputCase = LoggerOutputCase.SnakeCase;
659+
config.LogOutput = output;
660+
});
661+
}).CreatePowertoolsLogger();
662+
663+
// Simple template with one parameter
664+
logger.LogInformation("This is a test with {param}", "Hello");
665+
666+
var logOutput = output.ToString();
667+
_output.WriteLine(logOutput);
668+
669+
// Verify full formatted message appears correctly
670+
Assert.Contains("\"message\":\"This is a test with Hello\"", logOutput);
671+
// Verify parameter is also included separately
672+
Assert.Contains("\"param\":\"Hello\"", logOutput);
673+
674+
output.Clear();
675+
676+
// Multiple parameters
677+
logger.LogInformation("Test with {first} and {second}", "One", "Two");
678+
679+
logOutput = output.ToString();
680+
_output.WriteLine(logOutput);
634681

682+
// Verify message with multiple parameters
683+
Assert.Contains("\"message\":\"Test with One and Two\"", logOutput);
684+
Assert.Contains("\"first\":\"One\"", logOutput);
685+
Assert.Contains("\"second\":\"Two\"", logOutput);
635686
}
636687

637688
public class ParentClass

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/ExceptionFunctionHandler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public string HandlerLoggerForExceptions(string input, ILambdaContext context)
3434
Logger.LogDebug("Hello {input}", input);
3535
Logger.LogTrace("Hello {input}", input);
3636

37-
Logger.LogInformation("Testing with parameter Log Information Method {company}", new[] { "AWS" });
37+
Logger.LogInformation("Testing with parameter Log Information Method {company}", "AWS" );
3838

3939
var customKeys = new Dictionary<string, string>
4040
{
4141
{"test1", "value1"},
4242
{"test2", "value2"}
4343
};
44-
Logger.LogInformation(customKeys, "Retrieved data for city {cityName} with count {company}", "AWS");
44+
Logger.LogInformation("Retrieved data for city {cityName} with count {company}", "AWS", customKeys);
4545

4646
Logger.AppendKey("aws",1);
4747
Logger.AppendKey("aws",3);
@@ -52,10 +52,4 @@ public string HandlerLoggerForExceptions(string input, ILambdaContext context)
5252

5353
return "OK";
5454
}
55-
56-
[Logging(LogEvent = true)]
57-
public string HandleOk(string input)
58-
{
59-
return input.ToUpper(CultureInfo.InvariantCulture);
60-
}
6155
}

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ public void Log_WhenExtraKeysIsObjectDictionary_AppendExtraKeys(LogLevel logLeve
811811
// Arrange
812812
var loggerName = Guid.NewGuid().ToString();
813813
var service = Guid.NewGuid().ToString();
814-
var message = Guid.NewGuid().ToString();
814+
var message = "{@keys}";
815815

816816
var configurations = Substitute.For<IPowertoolsConfigurations>();
817817
configurations.Service.Returns(service);
@@ -836,29 +836,29 @@ public void Log_WhenExtraKeysIsObjectDictionary_AppendExtraKeys(LogLevel logLeve
836836

837837
if (logMethod)
838838
{
839-
logger.Log(logLevel, scopeKeys, message);
839+
logger.Log(logLevel, message,scopeKeys);
840840
}
841841
else
842842
{
843843
switch (logLevel)
844844
{
845845
case LogLevel.Trace:
846-
logger.LogTrace(scopeKeys, message);
846+
logger.LogTrace(message,scopeKeys);
847847
break;
848848
case LogLevel.Debug:
849-
logger.LogDebug(scopeKeys, message);
849+
logger.LogDebug(message,scopeKeys);
850850
break;
851851
case LogLevel.Information:
852-
logger.LogInformation(scopeKeys, message);
852+
logger.LogInformation(message,scopeKeys);
853853
break;
854854
case LogLevel.Warning:
855-
logger.LogWarning(scopeKeys, message);
855+
logger.LogWarning(message,scopeKeys);
856856
break;
857857
case LogLevel.Error:
858-
logger.LogError(scopeKeys, message);
858+
logger.LogError(message,scopeKeys);
859859
break;
860860
case LogLevel.Critical:
861-
logger.LogCritical(scopeKeys, message);
861+
logger.LogCritical(message,scopeKeys);
862862
break;
863863
case LogLevel.None:
864864
break;
@@ -895,7 +895,7 @@ public void Log_WhenExtraKeysIsStringDictionary_AppendExtraKeys(LogLevel logLeve
895895
// Arrange
896896
var loggerName = Guid.NewGuid().ToString();
897897
var service = Guid.NewGuid().ToString();
898-
var message = Guid.NewGuid().ToString();
898+
var message = "{@keys}";
899899

900900
var configurations = Substitute.For<IPowertoolsConfigurations>();
901901
configurations.Service.Returns(service);
@@ -921,29 +921,29 @@ public void Log_WhenExtraKeysIsStringDictionary_AppendExtraKeys(LogLevel logLeve
921921

922922
if (logMethod)
923923
{
924-
logger.Log(logLevel, scopeKeys, message);
924+
logger.Log(logLevel, message,scopeKeys);
925925
}
926926
else
927927
{
928928
switch (logLevel)
929929
{
930930
case LogLevel.Trace:
931-
logger.LogTrace(scopeKeys, message);
931+
logger.LogTrace(message,scopeKeys);
932932
break;
933933
case LogLevel.Debug:
934-
logger.LogDebug(scopeKeys, message);
934+
logger.LogDebug(message,scopeKeys);
935935
break;
936936
case LogLevel.Information:
937-
logger.LogInformation(scopeKeys, message);
937+
logger.LogInformation(message,scopeKeys);
938938
break;
939939
case LogLevel.Warning:
940-
logger.LogWarning(scopeKeys, message);
940+
logger.LogWarning(message,scopeKeys);
941941
break;
942942
case LogLevel.Error:
943-
logger.LogError(scopeKeys, message);
943+
logger.LogError(message,scopeKeys);
944944
break;
945945
case LogLevel.Critical:
946-
logger.LogCritical(scopeKeys, message);
946+
logger.LogCritical(message,scopeKeys);
947947
break;
948948
case LogLevel.None:
949949
break;
@@ -980,7 +980,7 @@ public void Log_WhenExtraKeysAsObject_AppendExtraKeys(LogLevel logLevel, bool lo
980980
// Arrange
981981
var loggerName = Guid.NewGuid().ToString();
982982
var service = Guid.NewGuid().ToString();
983-
var message = Guid.NewGuid().ToString();
983+
var message = "{@keys}";
984984

985985
var configurations = Substitute.For<IPowertoolsConfigurations>();
986986
configurations.Service.Returns(service);
@@ -1006,29 +1006,29 @@ public void Log_WhenExtraKeysAsObject_AppendExtraKeys(LogLevel logLevel, bool lo
10061006

10071007
if (logMethod)
10081008
{
1009-
logger.Log(logLevel, scopeKeys, message);
1009+
logger.Log(logLevel, message, scopeKeys);
10101010
}
10111011
else
10121012
{
10131013
switch (logLevel)
10141014
{
10151015
case LogLevel.Trace:
1016-
logger.LogTrace(scopeKeys, message);
1016+
logger.LogTrace(message,scopeKeys);
10171017
break;
10181018
case LogLevel.Debug:
1019-
logger.LogDebug(scopeKeys, message);
1019+
logger.LogDebug(message,scopeKeys);
10201020
break;
10211021
case LogLevel.Information:
1022-
logger.LogInformation(scopeKeys, message);
1022+
logger.LogInformation(message,scopeKeys);
10231023
break;
10241024
case LogLevel.Warning:
1025-
logger.LogWarning(scopeKeys, message);
1025+
logger.LogWarning(message,scopeKeys);
10261026
break;
10271027
case LogLevel.Error:
1028-
logger.LogError(scopeKeys, message);
1028+
logger.LogError(message,scopeKeys);
10291029
break;
10301030
case LogLevel.Critical:
1031-
logger.LogCritical(scopeKeys, message);
1031+
logger.LogCritical(message,scopeKeys);
10321032
break;
10331033
case LogLevel.None:
10341034
break;

0 commit comments

Comments
 (0)