-
-
Notifications
You must be signed in to change notification settings - Fork 1
Documentation
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.
To use LAGRA in your Go project, you can install it using the go get
command:
go get -u github.com/simplyYan/LAGRA
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")
}
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."
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"
`)
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")
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")
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")
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")
Sets the log level (INFO, WARN, ERROR).
Parameters:
- level
: The log level to set. Can be "INFO," "WARN," or "ERROR."
logger.SetLogLevel("WARN")
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)
}
}