Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Access to logger from classes outside the function #104

@onema

Description

@onema

Motivation

Access from other classes

Logging inside functions can be done using the methods LogInfo, LogWarn, LogError, and others.
Although, there is no access to these methods from outside the Function classes.

Structured logs

Logging is also not structured, in many instances it is helpful to have access to additional metadata when debugging code such as module version, class, method, etc.

Decoupled from the LambdaSharp framework

Having a dependency on the framework is not ideal, instead, having access to a single log object or class without worrying on how it was configured would be ideal.

Proposal

One way to do this is by using a logging framework such as Serilog. Lambda sharp could configure the logger, and this can be available from other classes:

Example:

In LambdaSharp the logger could be configured like this:

using Serilog;
using Serilog.Formatting.Json;
// ...
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .Enrich.WithProperty("moduleName", ModuleName)
    .Enrich.WithProperty("moduleVersion", ModuleVersion)
    .Enrich.WithProperty("functionName", FunctionName)
    .Enrich.WithProperty("functionId", FunctionId)
    // Other default properties
    .WriteTo.Console(outputTemplate: "*** {Level:u4}: ")
    .WriteTo.Console(new JsonFormatter())
    .CreateLogger();

Then this configuration can be used from any class that uses the Log:

using Serilog;
// ...
Log.Warning("Foo");
Log.Information("Bar");

The log Output of this log configuration will look like this:

*** WARN:
{
    "Timestamp": "2019-03-07T03:01:41.5085376+00:00",
    "Level": "Warning",
    "MessageTemplate": "Foo",
    "Properties": {
        "moduleName": "TheModuleName",
        "moduleVersion": "1.0.1-LATEST",
        "functionName": "TheFunctionName",
        "functionId": "tier-NameSapce-TheModuleName-TheFunctionName-1FUN4LOG2"
    }
}
*** INFO:
{
    "Timestamp": "2019-03-07T03:01:41.5085376+00:00",
    "Level": "Information",
    "MessageTemplate": "Bar",
    "Properties": {
        "moduleName": "TheModuleName",
        "moduleVersion": "1.0.1-LATEST",
        "functionName": "TheFunctionName",
        "functionId": "tier-NameSapce-TheModuleName-TheFunctionName-1FUN4LOG2"
    }
}

Serilog provides a number of layouts and configurations that can be added without a lot of setup, it is also very flexible and extensible.

Considerations

I'm not sure at this point what would be the overhead of using a logging framework would be, but Serilog seems to be quite performant compared to the alternatives (NLog and Log4Net).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions