Skip to content

Commit 3cd704a

Browse files
authored
Merge pull request #936 from aws-powertools/fix/override-lambda-console
chore: Console output overrides lambda console out
2 parents c2711e2 + 49b66e8 commit 3cd704a

File tree

2 files changed

+291
-142
lines changed

2 files changed

+291
-142
lines changed

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

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class ConsoleWrapper : IConsoleWrapper
88
{
99
private static bool _override;
1010
private static TextWriter _testOutputStream;
11-
private static bool _outputResetPerformed = false;
1211
private static bool _inTestMode = false;
1312

1413
/// <inheritdoc />
@@ -20,7 +19,7 @@ public void WriteLine(string message)
2019
}
2120
else
2221
{
23-
EnsureConsoleOutputOnce();
22+
EnsureConsoleOutput();
2423
Console.WriteLine(message);
2524
}
2625
}
@@ -34,7 +33,7 @@ public void Debug(string message)
3433
}
3534
else
3635
{
37-
EnsureConsoleOutputOnce();
36+
EnsureConsoleOutput();
3837
System.Diagnostics.Debug.WriteLine(message);
3938
}
4039
}
@@ -50,9 +49,9 @@ public void Error(string message)
5049
{
5150
if (!_override)
5251
{
53-
var errordOutput = new StreamWriter(Console.OpenStandardError());
54-
errordOutput.AutoFlush = true;
55-
Console.SetError(errordOutput);
52+
var errorOutput = new StreamWriter(Console.OpenStandardError());
53+
errorOutput.AutoFlush = true;
54+
Console.SetError(errorOutput);
5655
}
5756
Console.Error.WriteLine(message);
5857
}
@@ -70,23 +69,69 @@ public static void SetOut(TextWriter consoleOut)
7069
Console.SetOut(consoleOut);
7170
}
7271

73-
private static void EnsureConsoleOutputOnce()
72+
private static void EnsureConsoleOutput()
7473
{
75-
if (_outputResetPerformed) return;
76-
OverrideLambdaLogger();
77-
_outputResetPerformed = true;
74+
// Check if we need to override console output for Lambda environment
75+
if (ShouldOverrideConsole())
76+
{
77+
OverrideLambdaLogger();
78+
}
79+
}
80+
81+
private static bool ShouldOverrideConsole()
82+
{
83+
// Don't override if we're in test mode
84+
if (_inTestMode) return false;
85+
86+
// Always override in Lambda environment to prevent Lambda's log wrapping
87+
var isLambda = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME"));
88+
89+
return isLambda && (!_override || HasLambdaReInterceptedConsole());
90+
}
91+
92+
internal static bool HasLambdaReInterceptedConsole()
93+
{
94+
return HasLambdaReInterceptedConsole(() => Console.Out);
7895
}
7996

80-
private static void OverrideLambdaLogger()
97+
internal static bool HasLambdaReInterceptedConsole(Func<TextWriter> consoleOutAccessor)
8198
{
82-
if (_override)
99+
// Lambda might re-intercept console between init and handler execution
100+
try
101+
{
102+
var currentOut = consoleOutAccessor();
103+
// Check if current output stream looks like it might be Lambda's wrapper
104+
var typeName = currentOut.GetType().FullName ?? "";
105+
return typeName.Contains("Lambda") || typeName == "System.IO.TextWriter+SyncTextWriter";
106+
}
107+
catch
108+
{
109+
return true; // Assume re-interception if we can't determine
110+
}
111+
}
112+
113+
internal static void OverrideLambdaLogger()
114+
{
115+
OverrideLambdaLogger(() => Console.OpenStandardOutput());
116+
}
117+
118+
internal static void OverrideLambdaLogger(Func<Stream> standardOutputOpener)
119+
{
120+
try
121+
{
122+
// Force override of LambdaLogger
123+
var standardOutput = new StreamWriter(standardOutputOpener())
124+
{
125+
AutoFlush = true
126+
};
127+
Console.SetOut(standardOutput);
128+
_override = true;
129+
}
130+
catch (Exception)
83131
{
84-
return;
132+
// Log the failure but don't throw - degraded functionality is better than crash
133+
_override = false;
85134
}
86-
// Force override of LambdaLogger
87-
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
88-
standardOutput.AutoFlush = true;
89-
Console.SetOut(standardOutput);
90135
}
91136

92137
internal static void WriteLine(string logLevel, string message)
@@ -102,14 +147,14 @@ public static void ResetForTest()
102147
_override = false;
103148
_inTestMode = false;
104149
_testOutputStream = null;
105-
_outputResetPerformed = false;
106150
}
107151

108152
/// <summary>
109153
/// Clear the output reset flag
110154
/// </summary>
111155
public static void ClearOutputResetFlag()
112156
{
113-
_outputResetPerformed = false;
157+
// This method is kept for backward compatibility but no longer needed
158+
// since we removed the _outputResetPerformed flag
114159
}
115160
}

0 commit comments

Comments
 (0)