Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nested context logging #1

Open
mirkobrombin opened this issue Apr 9, 2024 · 0 comments
Open

Implement nested context logging #1

mirkobrombin opened this issue Apr 9, 2024 · 0 comments

Comments

@mirkobrombin
Copy link
Member

mirkobrombin commented Apr 9, 2024

The idea is to extend the logging system by introducing contextual logging capabilities, allowing logs to be structured within hierarchical contexts. This feature is similar to what we do in the ABRoot logger, the difference lies in the different implementation, in ABRoot we used to manually assign an index to each log (introducing a risk of human error):

func main() {
    PrintVerboseInfo("Function", "Entering function")
    PrintVerboseInfo("Function.StepName", "Entering function")
    PrintVerboseErr("Function.StepName", 0, "Encoutered an error")
    PrintVerboseInfo("Function", "Exiting function")
}

the new implementation uses a context-based approach:

func main() {
    logger, err := NewLogger("myApp")
    if err != nil {
        fmt.Println("Error creating logger:", err)
        return
    }

    rootCtx := NewLogContext("Function", nil)
    subCtx := NewLogContext("StepName", rootCtx)

    logger.InfoCtx(rootCtx, "Entering function")
    logger.InfoCtx(subCtx, "Performing a step")
    logger.ErrorCtx(subCtx, "Encountered an error")
    logger.ErrorCtx(subCtx, "Encountered a second error")
    logger.InfoCtx(rootCtx, "Exiting function")
}

The writer will use both files and console based on our current implementation and it will look like almost the same as the ABRoot approach:

Function:info:Entering function
Function:StepName:info:Performing a step
Function:StepName:err(0):Encountered an error
Function:StepName:err(1):Encountered a second error
Function:info:Exiting function

the main difference is that in the new implementation we do not have to manually specify an index for the error since that get's automatically generated ensuring its consistency and progression, while the prefix is automatically constructed by the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant