Skip to content

Commit 69f7b0e

Browse files
committed
replace log error with raise exception when format output is invalid
1 parent 8ed3ffc commit 69f7b0e

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/PowertoolsLogger.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,25 +345,14 @@ private object GetFormattedLogEntry(LogLevel logLevel, DateTime timestamp, objec
345345
try
346346
{
347347
var logObject = logFormatter.FormatLogEntry(logEntry);
348-
if (logObject is not null)
349-
return logObject;
350-
351-
return new
352-
{
353-
logEntry.Timestamp,
354-
Level = LogLevel.Error.ToString(),
355-
Message = $"{logFormatter.GetType().FullName} returned null value"
356-
};
348+
if (logObject is null)
349+
throw new LogFormatException($"{logFormatter.GetType().FullName} returned Null value.");
350+
return logObject;
357351
}
358352
catch (Exception e)
359353
{
360-
return new
361-
{
362-
logEntry.Timestamp,
363-
Level = LogLevel.Error.ToString(),
364-
Message = $"{logFormatter.GetType().FullName} raised error: {e.Message}.",
365-
Exception = e
366-
};
354+
throw new LogFormatException(
355+
$"{logFormatter.GetType().FullName} raised an exception: {e.Message}.", e);
367356
}
368357
}
369358

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace AWS.Lambda.Powertools.Logging;
19+
20+
/// <summary>
21+
/// Exception thrown when LogFormatter returns invalid object or error:
22+
/// </summary>
23+
public class LogFormatException : Exception
24+
{
25+
/// <summary>
26+
/// Creates a new IdempotencyConfigurationException
27+
/// </summary>
28+
public LogFormatException()
29+
{
30+
}
31+
32+
/// <inheritdoc />
33+
public LogFormatException(string message) : base(message)
34+
{
35+
}
36+
37+
/// <inheritdoc />
38+
public LogFormatException(string message, Exception innerException) : base(message, innerException)
39+
{
40+
}
41+
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void Log_WhenCustomFormatter_LogsCustomFormat()
169169
public class LogFormatterNullTest
170170
{
171171
[Fact]
172-
public void Log_WhenCustomFormatterReturnNull_LogsError()
172+
public void Log_WhenCustomFormatterReturnNull_ThrowsLogFormatException()
173173
{
174174
// Arrange
175175
var loggerName = Guid.NewGuid().ToString();
@@ -193,12 +193,13 @@ public void Log_WhenCustomFormatterReturnNull_LogsError()
193193
});
194194

195195
// Act
196-
logger.LogInformation(message);
196+
void Act() => logger.LogInformation(message);
197197

198198
// Assert
199+
Assert.Throws<LogFormatException>(Act);
199200
logFormatter.Received(1).FormatLogEntry(Arg.Any<LogEntry>());
200-
systemWrapper.Received(1).LogLine(Arg.Is<string>(x => x.Contains("Error") && x.Contains("null")));
201-
201+
systemWrapper.DidNotReceiveWithAnyArgs().LogLine(Arg.Any<string>());
202+
202203
//Clean up
203204
Logger.UseDefaultFormatter();
204205
}
@@ -208,7 +209,7 @@ public void Log_WhenCustomFormatterReturnNull_LogsError()
208209
public class LogFormatterExceptionTest
209210
{
210211
[Fact]
211-
public void Log_WhenCustomFormatterRaisesException_LogsError()
212+
public void Log_WhenCustomFormatterRaisesException_ThrowsLogFormatException()
212213
{
213214
// Arrange
214215
var loggerName = Guid.NewGuid().ToString();
@@ -233,12 +234,13 @@ public void Log_WhenCustomFormatterRaisesException_LogsError()
233234
});
234235

235236
// Act
236-
logger.LogInformation(message);
237+
void Act() => logger.LogInformation(message);
237238

238239
// Assert
240+
Assert.Throws<LogFormatException>(Act);
239241
logFormatter.Received(1).FormatLogEntry(Arg.Any<LogEntry>());
240-
systemWrapper.Received(1).LogLine(Arg.Is<string>(x => x.Contains("Error") && x.Contains(errorMessage)));
241-
242+
systemWrapper.DidNotReceiveWithAnyArgs().LogLine(Arg.Any<string>());
243+
242244
//Clean up
243245
Logger.UseDefaultFormatter();
244246
}

0 commit comments

Comments
 (0)