Skip to content

Commit 0d40609

Browse files
authored
fix(logging): POWERTOOLS_LOG_LEVEL=Trace/Debug no longer ignored (#1213)
1 parent 24a1db0 commit 0d40609

5 files changed

Lines changed: 152 additions & 1 deletion

File tree

libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/LoggerFactoryHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ internal static ILoggerFactory CreateAndConfigureFactory(PowertoolsLoggerConfigu
4545
builder.AddFilter(null, configuration.MinimumLogLevel);
4646
builder.SetMinimumLevel(configuration.MinimumLogLevel);
4747
}
48+
else
49+
{
50+
// No explicit level configured — let everything through the factory
51+
// so the provider can filter based on POWERTOOLS_LOG_LEVEL env var
52+
builder.AddFilter(null, LogLevel.Trace);
53+
builder.SetMinimumLevel(LogLevel.Trace);
54+
}
4855
});
4956

5057
LoggerFactoryHolder.SetFactory(factory);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public static ILoggingBuilder AddPowertoolsLogger(
9797
builder.Services.TryAddSingleton<ILogger>(provider =>
9898
provider.GetRequiredService<ILoggerFactory>().CreatePowertoolsLogger());
9999

100+
// Default the factory minimum to Trace so the framework doesn't filter
101+
// before our provider. The provider handles env-var-derived level filtering.
102+
builder.SetMinimumLevel(LogLevel.Trace);
103+
100104
builder.Services.TryAddEnumerable(
101105
ServiceDescriptor.Singleton<ILoggerProvider, PowertoolsLoggerProvider>(provider =>
102106
{
@@ -174,7 +178,6 @@ public static ILoggingBuilder AddPowertoolsLogger(
174178
var options = new PowertoolsLoggerConfiguration();
175179
configure(options);
176180

177-
// IMPORTANT: Set the minimum level directly on the builder
178181
if (options.MinimumLogLevel != LogLevel.None)
179182
{
180183
builder.SetMinimumLevel(options.MinimumLogLevel);

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Attributes/LoggingAttributeTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,44 @@ public void LoggingAspect_ShouldImmediatelyApplyFilterLevelChanges()
727727
s.Contains("\"message\":\"This should NOT be logged\"")));
728728
}
729729

730+
[Theory]
731+
// Reproduces issue #1205 test matrix
732+
[InlineData(null, "information,warning,error,critical", "trace,debug")]
733+
[InlineData("Trace", "trace,debug,information,warning,error,critical", "")]
734+
[InlineData("Debug", "debug,information,warning,error,critical", "trace")]
735+
[InlineData("Information", "information,warning,error,critical", "trace,debug")]
736+
[InlineData("Warning", "warning,error,critical", "trace,debug,information")]
737+
[InlineData("Error", "error,critical", "trace,debug,information,warning")]
738+
[InlineData("Critical", "critical", "trace,debug,information,warning,error")]
739+
public void LoggingAttribute_ShouldRespectEnvVarLogLevel(string envLogLevel, string expectedCsv, string missingCsv)
740+
{
741+
// Reproduces issue #1205: POWERTOOLS_LOG_LEVEL=Trace/Debug ignored when using [Logging] attribute
742+
Environment.SetEnvironmentVariable("POWERTOOLS_LOG_LEVEL", envLogLevel);
743+
744+
var consoleOut = new TestLoggerOutput();
745+
Logger.Configure(options =>
746+
{
747+
options.LogOutput = consoleOut;
748+
});
749+
750+
_testHandlers.TestMethodAllLevels();
751+
752+
var logOutput = consoleOut.ToString();
753+
754+
foreach (var level in expectedCsv.Split(','))
755+
{
756+
Assert.Contains($"LEVELTEST {level}", logOutput);
757+
}
758+
759+
if (!string.IsNullOrEmpty(missingCsv))
760+
{
761+
foreach (var level in missingCsv.Split(','))
762+
{
763+
Assert.DoesNotContain($"LEVELTEST {level}", logOutput);
764+
}
765+
}
766+
}
767+
730768
public void Dispose()
731769
{
732770
ResetAllState();

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,98 @@ public void EnvironmentVariableSampling_HandlerWithFullSampling_ShouldAlwaysElev
517517
}
518518
}
519519

520+
[Theory]
521+
[InlineData("Trace", "LEVELTEST trace", true)]
522+
[InlineData("Trace", "LEVELTEST debug", true)]
523+
[InlineData("Trace", "LEVELTEST information", true)]
524+
[InlineData("Debug", "LEVELTEST debug", true)]
525+
[InlineData("Debug", "LEVELTEST information", true)]
526+
[InlineData("Debug", "LEVELTEST trace", false)]
527+
[InlineData("Information", "LEVELTEST trace", false)]
528+
[InlineData("Information", "LEVELTEST debug", false)]
529+
[InlineData("Information", "LEVELTEST information", true)]
530+
public void PowertoolsLogLevel_EnvVar_ShouldFilterCorrectly(string envLogLevel, string expectedMessage, bool shouldAppear)
531+
{
532+
var originalLogLevel = Environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL");
533+
534+
try
535+
{
536+
Environment.SetEnvironmentVariable("POWERTOOLS_LOG_LEVEL", envLogLevel);
537+
538+
var output = new TestLoggerOutput();
539+
Logger.Reset();
540+
Logger.Configure(options =>
541+
{
542+
options.LogOutput = output;
543+
options.Service = "LevelTestService";
544+
});
545+
546+
Logger.LogTrace("LEVELTEST trace");
547+
Logger.LogDebug("LEVELTEST debug");
548+
Logger.LogInformation("LEVELTEST information");
549+
550+
var logOutput = output.ToString();
551+
_output.WriteLine(logOutput);
552+
553+
if (shouldAppear)
554+
{
555+
Assert.Contains(expectedMessage, logOutput);
556+
}
557+
else
558+
{
559+
Assert.DoesNotContain(expectedMessage, logOutput);
560+
}
561+
}
562+
finally
563+
{
564+
Environment.SetEnvironmentVariable("POWERTOOLS_LOG_LEVEL", originalLogLevel);
565+
Logger.Reset();
566+
}
567+
}
568+
569+
[Fact]
570+
public void PowertoolsLogLevel_Trace_ViaLoggerFactory_ShouldEmitAllLevels()
571+
{
572+
var originalLogLevel = Environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL");
573+
574+
try
575+
{
576+
Environment.SetEnvironmentVariable("POWERTOOLS_LOG_LEVEL", "Trace");
577+
578+
var output = new TestLoggerOutput();
579+
var logger = LoggerFactory.Create(builder =>
580+
{
581+
builder.AddPowertoolsLogger(config =>
582+
{
583+
config.Service = "LevelTestService";
584+
config.LogOutput = output;
585+
});
586+
}).CreatePowertoolsLogger();
587+
588+
logger.LogTrace("LEVELTEST trace");
589+
logger.LogDebug("LEVELTEST debug");
590+
logger.LogInformation("LEVELTEST information");
591+
logger.LogWarning("LEVELTEST warning");
592+
logger.LogError("LEVELTEST error");
593+
logger.LogCritical("LEVELTEST critical");
594+
595+
var logOutput = output.ToString();
596+
_output.WriteLine(logOutput);
597+
598+
Assert.Contains("LEVELTEST trace", logOutput);
599+
Assert.Contains("LEVELTEST debug", logOutput);
600+
Assert.Contains("LEVELTEST information", logOutput);
601+
Assert.Contains("LEVELTEST warning", logOutput);
602+
Assert.Contains("LEVELTEST error", logOutput);
603+
Assert.Contains("LEVELTEST critical", logOutput);
604+
}
605+
finally
606+
{
607+
Environment.SetEnvironmentVariable("POWERTOOLS_LOG_LEVEL", originalLogLevel);
608+
Logger.Reset();
609+
}
610+
}
611+
520612
/// <summary>
521613
/// Test with 0% sampling rate to ensure info logs are not elevated
522614
/// </summary>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public void TestMethodDebug()
2424
{
2525
}
2626

27+
[Logging(ClearState = true)]
28+
public void TestMethodAllLevels()
29+
{
30+
Logger.LogTrace("LEVELTEST trace");
31+
Logger.LogDebug("LEVELTEST debug");
32+
Logger.LogInformation("LEVELTEST information");
33+
Logger.LogWarning("LEVELTEST warning");
34+
Logger.LogError("LEVELTEST error");
35+
Logger.LogCritical("LEVELTEST critical");
36+
}
37+
2738
[Logging(LogEvent = true)]
2839
public void LogEventNoArgs()
2940
{

0 commit comments

Comments
 (0)