Skip to content

Documentation

Wesley Yan Soares Brehmer edited this page Oct 9, 2023 · 15 revisions

LAGRA - Lightweight Asynchronous Go Logger with TOML Configuration

LAGRA is a simple and lightweight asynchronous logger for Go applications. It allows you to log messages with different log levels (INFO, WARN, ERROR) to the console and/or a log file. LAGRA also supports configuration via TOML files.

Installation

To use LAGRA in your Go project, you can install it using the go get command:

go get -u github.com/simplyYan/LAGRA

Usage

To use LAGRA, you need to create an instance of the logger using lagra.New(). You can optionally provide a TOML configuration to customize the logger’s behavior.

package main

import (
    "context"
    "fmt"
    "github.com/simplyYan/LAGRA"
)

func main() {
    // Create a new LAGRA logger
    logger, err := lagra.New(`
        log_file = "app.log"
        log_level = "INFO"
    `)
    if err != nil {
        fmt.Println("Error creating logger:", err)
        return
    }

    // Log an informational message
    logger.Info(context.Background(), "This is an informational message")

    // Log a warning message
    logger.Warn(context.Background(), "This is a warning message")

    // Log an error message
    logger.Error(context.Background(), "This is an error message")
}

Configuration

LAGRA can be configured using a TOML file. Here’s an example TOML configuration:

log_file = "app.log"
log_level = "INFO"

You can customize the following configuration options:

  • log_file: Specifies the path to the log file. Leave it empty to disable logging to a file.

  • log_level: Sets the log level, which can be "INFO," "WARN," or "ERROR."

API Reference

New(tomlConfig string) (*Lagra, error)

Creates a new Lagra logger instance with optional TOML configuration.

Parameters: - tomlConfig: A TOML-formatted string containing logger configuration.

Returns: - A pointer to the Lagra logger instance. - An error if there was an issue parsing the TOML configuration.

logger, err := lagra.New(`
    log_file = "app.log"
    log_level = "INFO"
`)

Info(ctx context.Context, message string, customLogPath …​string) error

Logs an informational message with an optional custom log file path.

Parameters: - ctx: A context.Context instance (ignored in this version). - message: The message to be logged. - customLogPath: An optional custom log file path. If provided, this path will override the configured log file path.

Returns: - An error if there was an issue writing to the log file, or nil if successful.

logger.Info(context.Background(), "This is an informational message")
logger.Info(context.Background(), "This is a custom log message", "custom.log")

Warn(ctx context.Context, message string, customLogPath …​string) error

Logs a warning message with an optional custom log file path. Parameters and return value are the same as Info().

logger.Warn(context.Background(), "This is a warning message")
logger.Warn(context.Background(), "This is a custom warning message", "custom.log")

Error(ctx context.Context, message string, customLogPath …​string) error

Logs an error message with an optional custom log file path. Parameters and return value are the same as Info().

logger.Error(context.Background(), "This is an error message")
logger.Error(context.Background(), "This is a custom error message", "custom.log")

SetLogFile(filePath string)

Sets the log file path. This can be used to change the log file dynamically during runtime.

Parameters: - filePath: The new log file path.

logger.SetLogFile("new.log")

SetLogLevel(level string)

Sets the log level (INFO, WARN, ERROR).

Parameters: - level: The log level to set. Can be "INFO," "WARN," or "ERROR."

logger.SetLogLevel("WARN")

Handling errors more easily, efficiently and quickly with LAGRA

er := lagra.Tracker() //Create a new LAGRER instance

// An example of an operation that will return an error
err := someOperation()
er.N(err) //Nominate a new bug tracker

// Another example of an operation that will return an error
err = anotherOperation()
er.N(err) //Nominate a new bug tracker

if er.Handle() {
    // Deal with mistakes somehow
    for _, e := range er.Errors() {
        fmt.Println(e)
    }
}

Selecting strings

    strselect := NewStrSelect()
    data := "Hello ::World::"
    strselect.SetStr("data", data)

    selectdata := strselect.SelectStr("data", "::")
    fmt.Println(selectdata) // print "world"

Conclusion

LAGRA is a lightweight logger for Go that provides flexibility and simplicity in logging messages with different log levels. You can configure it using TOML files, and it supports both console and file-based logging. Feel free to use and adapt LAGRA for your specific logging needs.