From 7befc8f448d7a2fdda28c882da0346f8ec193b9f Mon Sep 17 00:00:00 2001 From: Matt Simpson Date: Wed, 9 Sep 2020 13:30:45 +1200 Subject: [PATCH 1/4] feature:logging scaffold basic log class --- .../Amazon.LambdaPowertools.Logging.csproj | 4 +++ .../Amazon.LambdaPowertools.Logging/Class1.cs | 8 ----- .../ILogEntry.cs | 15 ++++++++++ .../ILoggerOptions.cs | 25 ++++++++++++++++ .../LoggerOptions.cs | 9 ++++++ .../PowertoolsLogger.cs | 29 +++++++++++++++++++ 6 files changed, 82 insertions(+), 8 deletions(-) delete mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/Class1.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj index 8642d92f3..85abdc76d 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj +++ b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj @@ -4,4 +4,8 @@ netcoreapp3.1 + + + + diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Class1.cs b/libraries/src/Amazon.LambdaPowertools.Logging/Class1.cs deleted file mode 100644 index 8c9f9c764..000000000 --- a/libraries/src/Amazon.LambdaPowertools.Logging/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace Amazon.LambdaPowertools.Logging -{ - public class Class1 - { - } -} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs b/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs new file mode 100644 index 000000000..14f160e5d --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs @@ -0,0 +1,15 @@ +namespace Amazon.LambdaPowertools.Logging +{ + public interface ILogEntry { + bool ColdStart { get; set; } + string FunctionArn { get; set; } + string FunctionName { get; set; } + string FunctionMemorySize { get; set; } + string FunctionRequestId { get; set; } + string Level { get; set; } + string Location { get; set; } + string Message { get; set; } + double SamplingRate { get; set; } + string Timestamp { get; set; } + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs new file mode 100644 index 000000000..249fb44da --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs @@ -0,0 +1,25 @@ +using System; + +namespace Amazon.LambdaPowertools.Logging +{ + public interface ILoggerOptions + { + /// + /// Logging level for Logger, and also avialable as a logging key + /// Default "INFO", env:LOG_LEVEL + /// + string Level { get; } + + /// + /// Sampling rate for dynamically set log level as DEBUG for a given request + /// Default 0.0, env:POWERTOOLS_LOGGER_SAMPLE_RATE + /// + double SamplingRate { get; set; } + + /// + /// Service metadata that will be used as a logging key + /// Default "service_undefined", env:POWERTOOLS_SERVICE_NAME + /// + string Service { get; set; } + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs new file mode 100644 index 000000000..b50a3bc3e --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs @@ -0,0 +1,9 @@ +namespace Amazon.LambdaPowertools.Logging +{ + public class LoggerOptions : ILoggerOptions + { + public string Level { get; } + public double SamplingRate { get; set; } + public string Service { get; set; } + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs new file mode 100644 index 000000000..1fa45f74b --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs @@ -0,0 +1,29 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace Amazon.LambdaPowertools.Logging +{ + public class PowertoolsLogger : ILogger + { + private readonly LoggerOptions _loggerOptions; + private PowertoolsLogger(LoggerOptions loggerOptions) + { + _loggerOptions = loggerOptions; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + throw new NotImplementedException(); + } + + public bool IsEnabled(LogLevel logLevel) + { + throw new NotImplementedException(); + } + + public IDisposable BeginScope(TState state) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file From 48a5f6d5be41d1b2818a8d11f173fdf01af3fc8b Mon Sep 17 00:00:00 2001 From: Matt Simpson Date: Wed, 9 Sep 2020 20:57:40 +1200 Subject: [PATCH 2/4] fix(logging): refactor with most basic minimal version --- .../Amazon.LambdaPowertools.Logging.csproj | 1 - .../ILogEntry.cs | 15 ----- .../ILoggerOptions.cs | 4 +- .../LogEntry.cs | 15 +++++ .../LogLevel.cs | 13 ++++ .../Amazon.LambdaPowertools.Logging/Logger.cs | 33 ++++++++++ .../LoggerOptions.cs | 2 +- .../PowertoolsLogger.cs | 61 ++++++++++--------- ...azon.LambdaPowertools.Logging.Tests.csproj | 10 +++ .../Class1.cs | 10 --- .../TestPowertoolsLogger.cs | 39 ++++++++++++ 11 files changed, 145 insertions(+), 58 deletions(-) delete mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs create mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs delete mode 100644 libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Class1.cs create mode 100644 libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj index 85abdc76d..b923edaee 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj +++ b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj @@ -5,7 +5,6 @@ - diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs b/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs deleted file mode 100644 index 14f160e5d..000000000 --- a/libraries/src/Amazon.LambdaPowertools.Logging/ILogEntry.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Amazon.LambdaPowertools.Logging -{ - public interface ILogEntry { - bool ColdStart { get; set; } - string FunctionArn { get; set; } - string FunctionName { get; set; } - string FunctionMemorySize { get; set; } - string FunctionRequestId { get; set; } - string Level { get; set; } - string Location { get; set; } - string Message { get; set; } - double SamplingRate { get; set; } - string Timestamp { get; set; } - } -} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs index 249fb44da..d0f37c3d3 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs @@ -8,8 +8,8 @@ public interface ILoggerOptions /// Logging level for Logger, and also avialable as a logging key /// Default "INFO", env:LOG_LEVEL /// - string Level { get; } - + LogLevel Level { get; } + /// /// Sampling rate for dynamically set log level as DEBUG for a given request /// Default 0.0, env:POWERTOOLS_LOGGER_SAMPLE_RATE diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs new file mode 100644 index 000000000..1139177dc --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs @@ -0,0 +1,15 @@ +namespace Amazon.LambdaPowertools.Logging +{ + public class LogEntry { + public bool ColdStart { get; set; } + public string FunctionArn { get; set; } + public string FunctionName { get; set; } + public string FunctionMemorySize { get; set; } + public string FunctionRequestId { get; set; } + public LogLevel Level { get; set; } + public string Location { get; set; } + public string Message { get; set; } + public double SamplingRate { get; set; } + public string Timestamp { get; set; } + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs new file mode 100644 index 000000000..19d3dd5a6 --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs @@ -0,0 +1,13 @@ +namespace Amazon.LambdaPowertools.Logging +{ + public enum LogLevel + { + Trace = 0, + Debug = 1, + Information = 2, + Warning = 3, + Error = 4, + Critical = 5, + None = 6, + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs new file mode 100644 index 000000000..7c7d955d5 --- /dev/null +++ b/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs @@ -0,0 +1,33 @@ +using System; +using System.Reflection; +using System.Text.Json; + +namespace Amazon.LambdaPowertools.Logging +{ + public class Logger + { + private bool _coldStart; + public Logger() + { + } + + public void Log(string message, LogLevel logLevel = LogLevel.Information) + { + LogEntry logEntry = new LogEntry() + { + ColdStart = _coldStart, + FunctionArn = "functionArn", + FunctionName = "functionName", + FunctionMemorySize = "FunctionMemorySize", + FunctionRequestId = "FunctionRequestId", + Level = logLevel, + Location = Assembly.GetCallingAssembly().FullName, + Message = message, + SamplingRate = 0.0, + Timestamp = DateTime.UtcNow.ToString("o") + }; + + Console.WriteLine(JsonSerializer.Serialize(logEntry)); + } + } +} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs index b50a3bc3e..2d8baeb7d 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs @@ -2,7 +2,7 @@ namespace Amazon.LambdaPowertools.Logging { public class LoggerOptions : ILoggerOptions { - public string Level { get; } + public LogLevel Level { get; } public double SamplingRate { get; set; } public string Service { get; set; } } diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs index 1fa45f74b..04999ff92 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs @@ -1,29 +1,32 @@ -using System; -using Microsoft.Extensions.Logging; - -namespace Amazon.LambdaPowertools.Logging -{ - public class PowertoolsLogger : ILogger - { - private readonly LoggerOptions _loggerOptions; - private PowertoolsLogger(LoggerOptions loggerOptions) - { - _loggerOptions = loggerOptions; - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) - { - throw new NotImplementedException(); - } - - public bool IsEnabled(LogLevel logLevel) - { - throw new NotImplementedException(); - } - - public IDisposable BeginScope(TState state) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file +// using System; +// using Microsoft.Extensions.Logging; +// +// namespace Amazon.LambdaPowertools.Logging +// { +// public class PowertoolsLogger : ILogger +// { +// // private readonly LoggerOptions _loggerOptions; +// // public PowertoolsLogger(LoggerOptions loggerOptions) +// // { +// // _loggerOptions = loggerOptions; +// // } +// public PowertoolsLogger() +// { +// } +// +// public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) +// { +// Console.WriteLine(formatter); +// } +// +// public bool IsEnabled(LogLevel logLevel) +// { +// throw new NotImplementedException(); +// } +// +// public IDisposable BeginScope(TState state) +// { +// throw new NotImplementedException(); +// } +// } +// } \ No newline at end of file diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Amazon.LambdaPowertools.Logging.Tests.csproj b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Amazon.LambdaPowertools.Logging.Tests.csproj index 8642d92f3..4014376f0 100644 --- a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Amazon.LambdaPowertools.Logging.Tests.csproj +++ b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Amazon.LambdaPowertools.Logging.Tests.csproj @@ -4,4 +4,14 @@ netcoreapp3.1 + + + + + + + + + + diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Class1.cs b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Class1.cs deleted file mode 100644 index 95514fdd8..000000000 --- a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/Class1.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace Amazon.LambdaPowertools.Logging.Tests -{ - - - public class Class1 - { - } -} \ No newline at end of file diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs new file mode 100644 index 000000000..2e46048c9 --- /dev/null +++ b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using Amazon.LambdaPowertools.Logging; +using Microsoft.Extensions.Logging; +using Xunit; +using Xunit.Abstractions; + +namespace Amazon.LambdaPowertools.Logging.Tests +{ + public class TestPowertoolsLogger + { + private Logger _logger; + private readonly ITestOutputHelper _testOutput; + public TestPowertoolsLogger(ITestOutputHelper testOutput) + { + _logger = new Logger(); + _testOutput = testOutput; + } + + + + + + + [Fact] + public void TestFunction() + { + var output = new StringWriter(); + Console.SetOut(output); + //LogLevel level = LogLevel.Debug; + _logger.Log("TEST"); + + _testOutput.WriteLine(output.ToString()); + Assert.Contains("\"Message\":\"TEST\"", output.ToString()); + } + + + } +} \ No newline at end of file From 774df4a357d227c1e18a4e5a1314bd1f2de20794 Mon Sep 17 00:00:00 2001 From: Matt Simpson Date: Thu, 10 Sep 2020 13:34:20 +1200 Subject: [PATCH 3/4] fix(logging): refactor to use ILogger --- .../Amazon.LambdaPowertools.Logging.csproj | 4 + .../ILoggerOptions.cs | 25 ------ .../LogEntry.cs | 4 +- .../LogLevel.cs | 13 --- .../Amazon.LambdaPowertools.Logging/Logger.cs | 33 ------- .../LoggerOptions.cs | 10 ++- .../PowertoolsLogger.cs | 87 ++++++++++++------- .../TestFunction.cs | 7 ++ .../TestPowertoolsLogger.cs | 24 +++-- 9 files changed, 94 insertions(+), 113 deletions(-) delete mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs delete mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs delete mode 100644 libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs create mode 100644 libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestFunction.cs diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj index b923edaee..2a94ef895 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj +++ b/libraries/src/Amazon.LambdaPowertools.Logging/Amazon.LambdaPowertools.Logging.csproj @@ -7,4 +7,8 @@ + + + + diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs deleted file mode 100644 index d0f37c3d3..000000000 --- a/libraries/src/Amazon.LambdaPowertools.Logging/ILoggerOptions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace Amazon.LambdaPowertools.Logging -{ - public interface ILoggerOptions - { - /// - /// Logging level for Logger, and also avialable as a logging key - /// Default "INFO", env:LOG_LEVEL - /// - LogLevel Level { get; } - - /// - /// Sampling rate for dynamically set log level as DEBUG for a given request - /// Default 0.0, env:POWERTOOLS_LOGGER_SAMPLE_RATE - /// - double SamplingRate { get; set; } - - /// - /// Service metadata that will be used as a logging key - /// Default "service_undefined", env:POWERTOOLS_SERVICE_NAME - /// - string Service { get; set; } - } -} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs index 1139177dc..923784c39 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.Logging; + namespace Amazon.LambdaPowertools.Logging { public class LogEntry { @@ -6,7 +8,7 @@ public class LogEntry { public string FunctionName { get; set; } public string FunctionMemorySize { get; set; } public string FunctionRequestId { get; set; } - public LogLevel Level { get; set; } + public string Level { get; set; } public string Location { get; set; } public string Message { get; set; } public double SamplingRate { get; set; } diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs deleted file mode 100644 index 19d3dd5a6..000000000 --- a/libraries/src/Amazon.LambdaPowertools.Logging/LogLevel.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Amazon.LambdaPowertools.Logging -{ - public enum LogLevel - { - Trace = 0, - Debug = 1, - Information = 2, - Warning = 3, - Error = 4, - Critical = 5, - None = 6, - } -} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs deleted file mode 100644 index 7c7d955d5..000000000 --- a/libraries/src/Amazon.LambdaPowertools.Logging/Logger.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Reflection; -using System.Text.Json; - -namespace Amazon.LambdaPowertools.Logging -{ - public class Logger - { - private bool _coldStart; - public Logger() - { - } - - public void Log(string message, LogLevel logLevel = LogLevel.Information) - { - LogEntry logEntry = new LogEntry() - { - ColdStart = _coldStart, - FunctionArn = "functionArn", - FunctionName = "functionName", - FunctionMemorySize = "FunctionMemorySize", - FunctionRequestId = "FunctionRequestId", - Level = logLevel, - Location = Assembly.GetCallingAssembly().FullName, - Message = message, - SamplingRate = 0.0, - Timestamp = DateTime.UtcNow.ToString("o") - }; - - Console.WriteLine(JsonSerializer.Serialize(logEntry)); - } - } -} \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs index 2d8baeb7d..bd7f94a52 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs @@ -1,9 +1,11 @@ +using Microsoft.Extensions.Logging; + namespace Amazon.LambdaPowertools.Logging { - public class LoggerOptions : ILoggerOptions + public class LoggerOptions { - public LogLevel Level { get; } - public double SamplingRate { get; set; } - public string Service { get; set; } + public LogLevel LogLevel { get; set; } = LogLevel.Warning; + public double SamplingRate { get; set; } = 0.0; + public string Service { get; set; } = "service_undefined"; } } \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs index 04999ff92..a695121f3 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs @@ -1,32 +1,55 @@ -// using System; -// using Microsoft.Extensions.Logging; -// -// namespace Amazon.LambdaPowertools.Logging -// { -// public class PowertoolsLogger : ILogger -// { -// // private readonly LoggerOptions _loggerOptions; -// // public PowertoolsLogger(LoggerOptions loggerOptions) -// // { -// // _loggerOptions = loggerOptions; -// // } -// public PowertoolsLogger() -// { -// } -// -// public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) -// { -// Console.WriteLine(formatter); -// } -// -// public bool IsEnabled(LogLevel logLevel) -// { -// throw new NotImplementedException(); -// } -// -// public IDisposable BeginScope(TState state) -// { -// throw new NotImplementedException(); -// } -// } -// } \ No newline at end of file +using System; +using System.Reflection; +using System.Text.Json; +using Microsoft.Extensions.Logging; + +namespace Amazon.LambdaPowertools.Logging +{ + public class PowertoolsLogger : ILogger + { + private readonly LoggerOptions _loggerOptions; + private bool ColdStart = true; + public PowertoolsLogger(LoggerOptions loggerOptions) + { + _loggerOptions = loggerOptions; + } + + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + LogEntry logEntry = new LogEntry() + { + ColdStart = ColdStart, + FunctionArn = "functionArn", + FunctionName = "functionName", + FunctionMemorySize = "FunctionMemorySize", + FunctionRequestId = "FunctionRequestId", + Level = logLevel.ToString(), + Location = Assembly.GetCallingAssembly().FullName, + Message = formatter(state, exception), + SamplingRate = 0.0, + Timestamp = DateTime.UtcNow.ToString("o") + }; + + Console.WriteLine(JsonSerializer.Serialize(logEntry)); + + //Console.WriteLine($"{logLevel.ToString()}, {eventId.ToString()}, {state.ToString()}, {exception.ToString()}, {formatter}"); + } + + public bool IsEnabled(LogLevel logLevel) + { + return logLevel == _loggerOptions.LogLevel; + } + + public IDisposable BeginScope(TState state) + { + throw new NotImplementedException(); + } + } +} + diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestFunction.cs b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestFunction.cs new file mode 100644 index 000000000..54fc7b8a9 --- /dev/null +++ b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestFunction.cs @@ -0,0 +1,7 @@ +namespace Amazon.LambdaPowertools.Logging.Tests +{ + public class TestFunction + { + + } +} \ No newline at end of file diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs index 2e46048c9..e9a8c2cbb 100644 --- a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs +++ b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs @@ -9,11 +9,15 @@ namespace Amazon.LambdaPowertools.Logging.Tests { public class TestPowertoolsLogger { - private Logger _logger; + private ILogger _logger; private readonly ITestOutputHelper _testOutput; public TestPowertoolsLogger(ITestOutputHelper testOutput) { - _logger = new Logger(); + LoggerOptions loggerOptions = new LoggerOptions() + { + LogLevel = LogLevel.Information + }; + _logger = new PowertoolsLogger(loggerOptions); _testOutput = testOutput; } @@ -23,16 +27,26 @@ public TestPowertoolsLogger(ITestOutputHelper testOutput) [Fact] - public void TestFunction() + public void TestLogInformationOutput() { var output = new StringWriter(); Console.SetOut(output); - //LogLevel level = LogLevel.Debug; - _logger.Log("TEST"); + _logger.LogInformation("TEST"); _testOutput.WriteLine(output.ToString()); Assert.Contains("\"Message\":\"TEST\"", output.ToString()); } + + [Fact] + public void TestLogDebugOutputShouldFail() + { + var output = new StringWriter(); + Console.SetOut(output); + _logger.LogDebug("TEST"); + + _testOutput.WriteLine(output.ToString()); + Assert.Empty(output.ToString()); + } } From 187c419c33c33ca24749c9333b90aa3eeb85785b Mon Sep 17 00:00:00 2001 From: Matt Simpson Date: Thu, 10 Sep 2020 21:50:44 +1200 Subject: [PATCH 4/4] fix(logger): Logger Options from env and update test coverage --- .../LoggerOptions.cs | 21 ++++- .../PowertoolsLogger.cs | 6 +- .../TestPowertoolsLogger.cs | 89 ++++++++++++++++--- 3 files changed, 96 insertions(+), 20 deletions(-) diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs index bd7f94a52..f54b05c3a 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs @@ -1,11 +1,26 @@ +using System; +using System.Globalization; using Microsoft.Extensions.Logging; namespace Amazon.LambdaPowertools.Logging { public class LoggerOptions { - public LogLevel LogLevel { get; set; } = LogLevel.Warning; - public double SamplingRate { get; set; } = 0.0; - public string Service { get; set; } = "service_undefined"; + public LogLevel LogLevel { get; set; } + public double SamplingRate { get; set; } + public string Service { get; set; } + public LoggerOptions() + { + string envLogLevel = Environment.GetEnvironmentVariable("LOG_LEVEL");// ?? "Information"; + var isLogLevel = Enum.TryParse(envLogLevel, out LogLevel parseLogLevel) & Enum.IsDefined(typeof(LogLevel), parseLogLevel); + LogLevel = (isLogLevel) ? parseLogLevel : LogLevel.Information; + + string envSamplingRate = Environment.GetEnvironmentVariable("POWERTOOLS_LOGGER_SAMPLE_RATE");// ?? "0.0"; + SamplingRate = Double.Parse(envSamplingRate ?? "0.0"); + + Service = Environment.GetEnvironmentVariable("POWERTOOLS_SERVICE_NAME") ?? "service_undefined"; + } + + } } \ No newline at end of file diff --git a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs index a695121f3..621d3a8ca 100644 --- a/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs +++ b/libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs @@ -8,7 +8,7 @@ namespace Amazon.LambdaPowertools.Logging public class PowertoolsLogger : ILogger { private readonly LoggerOptions _loggerOptions; - private bool ColdStart = true; + private static bool ColdStart = true; public PowertoolsLogger(LoggerOptions loggerOptions) { _loggerOptions = loggerOptions; @@ -32,7 +32,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except Level = logLevel.ToString(), Location = Assembly.GetCallingAssembly().FullName, Message = formatter(state, exception), - SamplingRate = 0.0, + SamplingRate = _loggerOptions.SamplingRate, Timestamp = DateTime.UtcNow.ToString("o") }; @@ -43,7 +43,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except public bool IsEnabled(LogLevel logLevel) { - return logLevel == _loggerOptions.LogLevel; + return logLevel >= _loggerOptions.LogLevel; } public IDisposable BeginScope(TState state) diff --git a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs index e9a8c2cbb..b4e1db470 100644 --- a/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs +++ b/libraries/tests/Amazon.LambdaPowertools.Logging.Tests/TestPowertoolsLogger.cs @@ -13,39 +13,100 @@ public class TestPowertoolsLogger private readonly ITestOutputHelper _testOutput; public TestPowertoolsLogger(ITestOutputHelper testOutput) { - LoggerOptions loggerOptions = new LoggerOptions() - { - LogLevel = LogLevel.Information - }; - _logger = new PowertoolsLogger(loggerOptions); _testOutput = testOutput; } - + [Fact] + public void TestLogInformationOutput() + { + // Initialize + var output = new StringWriter(); + Console.SetOut(output); + Environment.SetEnvironmentVariable("LOG_LEVEL", "Information"); + + var loggerOptions = new LoggerOptions(); + _logger = new PowertoolsLogger(loggerOptions); + const string logInfo = "TEST INFO ??!?"; + const string logDebug = "TEST DEBUG ##11"; + + // Assert + _logger.LogInformation(logInfo); + _logger.LogDebug(logDebug); + + _testOutput.WriteLine(output.ToString()); + Assert.Contains(logInfo, output.ToString()); + Assert.DoesNotContain(logDebug, output.ToString()); + + } + [Fact] + public void TestLogDebugOutput() + { + // Initialize + var output = new StringWriter(); + Console.SetOut(output); + Environment.SetEnvironmentVariable("LOG_LEVEL", "Debug"); + + var loggerOptions = new LoggerOptions(); + _logger = new PowertoolsLogger(loggerOptions); + const string logInfo = "TEST INFO ??!?"; + const string logDebug = "TEST DEBUG ##11"; + const string logTrace = "TEST TRACE ##1875"; + + // Assert + _logger.LogInformation(logInfo); + _logger.LogDebug(logDebug); + _logger.LogTrace(logTrace); + + _testOutput.WriteLine(output.ToString()); + Assert.Contains(logInfo, output.ToString()); + Assert.Contains(logDebug, output.ToString()); + Assert.DoesNotContain(logTrace, output.ToString()); + } - - [Fact] - public void TestLogInformationOutput() + public void TestLogLevelFromEnvironmentVariableMissing() { + // Initialize var output = new StringWriter(); Console.SetOut(output); - _logger.LogInformation("TEST"); + Environment.SetEnvironmentVariable("LOG_LEVEL", null); + //Environment.SetEnvironmentVariable("LOG_LEVEL", "Missing"); + + var loggerOptions = new LoggerOptions(); + _logger = new PowertoolsLogger(loggerOptions); + const string logInfo = "TEST INFO ??!?"; + const string logDebug = "TEST DEBUG ##11"; + + // Assert + _logger.LogInformation(logInfo); + _logger.LogDebug(logDebug); _testOutput.WriteLine(output.ToString()); - Assert.Contains("\"Message\":\"TEST\"", output.ToString()); + Assert.Contains(logInfo, output.ToString()); + Assert.DoesNotContain(logDebug, output.ToString()); } [Fact] - public void TestLogDebugOutputShouldFail() + public void TestLogLevelFromEnvironmentVariableTypo() { + // Initialize var output = new StringWriter(); Console.SetOut(output); - _logger.LogDebug("TEST"); + Environment.SetEnvironmentVariable("LOG_LEVEL", "Typo"); + + var loggerOptions = new LoggerOptions(); + _logger = new PowertoolsLogger(loggerOptions); + const string logInfo = "TEST INFO ??!?"; + const string logDebug = "TEST DEBUG ##11"; + + // Assert + _logger.LogInformation(logInfo); + _logger.LogDebug(logDebug); _testOutput.WriteLine(output.ToString()); - Assert.Empty(output.ToString()); + Assert.Contains(logInfo, output.ToString()); + Assert.DoesNotContain(logDebug, output.ToString()); }