Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
</ItemGroup>

</Project>
8 changes: 0 additions & 8 deletions libraries/src/Amazon.LambdaPowertools.Logging/Class1.cs

This file was deleted.

17 changes: 17 additions & 0 deletions libraries/src/Amazon.LambdaPowertools.Logging/LogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.Logging;

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 string Level { get; set; }
public string Location { get; set; }
public string Message { get; set; }
public double SamplingRate { get; set; }
public string Timestamp { get; set; }
}
}
26 changes: 26 additions & 0 deletions libraries/src/Amazon.LambdaPowertools.Logging/LoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Globalization;
using Microsoft.Extensions.Logging;

namespace Amazon.LambdaPowertools.Logging
{
public class LoggerOptions
{
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";
}


}
}
55 changes: 55 additions & 0 deletions libraries/src/Amazon.LambdaPowertools.Logging/PowertoolsLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 static bool ColdStart = true;
public PowertoolsLogger(LoggerOptions loggerOptions)
{
_loggerOptions = loggerOptions;
}


public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> 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 = _loggerOptions.SamplingRate,
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>(TState state)
{
throw new NotImplementedException();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Amazon.LambdaPowertools.Logging\Amazon.LambdaPowertools.Logging.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;

namespace Amazon.LambdaPowertools.Logging.Tests
{


public class Class1
public class TestFunction
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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 ILogger _logger;
private readonly ITestOutputHelper _testOutput;
public TestPowertoolsLogger(ITestOutputHelper testOutput)
{
_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 TestLogLevelFromEnvironmentVariableMissing()
{
// Initialize
var output = new StringWriter();
Console.SetOut(output);
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(logInfo, output.ToString());
Assert.DoesNotContain(logDebug, output.ToString());
}

[Fact]
public void TestLogLevelFromEnvironmentVariableTypo()
{
// Initialize
var output = new StringWriter();
Console.SetOut(output);
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.Contains(logInfo, output.ToString());
Assert.DoesNotContain(logDebug, output.ToString());
}


}
}