A flexible and structured logging library for Go with dynamic controls.
go get github.com/IonicHealthUsa/ionlog
package main
import "github.com/IonicHealthUsa/ionlog"
func main() {
ionlog.SetLogAttributes(
ionlog.WithTargets(ionlog.DefaultOutput()), // Log to console
ionlog.WithStaticFields(map[string]string{"service": "my-app"}),
ionlog.WithLogFileRotation("logs", 10*ionlog.Mebibyte, ionlog.Daily),
)
ionlog.Start()
defer ionlog.Stop()
ionlog.Info("Application started")
}
package main
import (
"github.com/IonicHealthUsa/ionlog"
)
func main() {
// Set the log attributes, and other configurations
ionlog.SetLogAttributes(
// WithTargets sets the write targets for the logger, every log will be written
// to these targets.
ionlog.WithTargets(
ionlog.DefaultOutput(),
// a websocket
// a file
// your custom writer
),
// (Optional) WithStaticFields sets the static fields for the logger, every log will have these fields.
ionlog.WithStaticFields(map[string]string{
"computer-id": "1234",
// your custom fields
}),
// (Optional) WithLogFileRotation enables log file rotation, specifying the directory where log files will be stored, the maximum size of the log folder in bytes, and the rotation frequency.
// This internal log rotation system appends the log file to the specified targets and automatically rotates logs based on the provided configuration,
// ensuring the total size of the log folder does not exceed the specified maximum (e.g., 10MB in this case).
ionlog.WithLogFileRotation("logs", 10*ionlog.Mebibyte, ionlog.Daily),
)
// Start the logger service
ionlog.Start()
// Stops the logger service when the main function ends
defer ionlog.Stop()
// output: {"time":"2024-12-06T20:59:47.252944832-03:00","level":"INFO","msg":"This log level is: info","computer-id":"1234","package":"main","function":"main","file":"main.go","line":38}
ionlog.Infof("This log level is: %v", "info")
ionlog.Errorf("This log level is: %v", "error")
ionlog.Warnf("This log level is: %v", "warn")
ionlog.Debugf("This log level is: %v", "debug")
ionlog.Info("This log level is a simple info log")
ionlog.Error("This log level is a simple error log")
ionlog.Warn("This log level is a simple warn log")
ionlog.Debug("This log level is a simple debug log")
status := "NOT OK"
for i := 0; i < 10; i++ {
ionlog.LogOnceInfo("Process Started!") // This will be logged only once
ionlog.LogOnChangeDebugf("count: %v", i) // Log every time i changes
if i == 5 {
status = "OK"
}
ionlog.LogOnChangeInfof("status: %v", status) // Log once "NOT OK", log once "OK"
}
}
ionlog.WithTargets(ionlog.DefaultOutput(), myCustomWriter)
ionlog.WithStaticFields(map[string]string{"env": "production"})
ionlog.WithLogFileRotation("logs", 100*ionlog.Mebibyte, ionlog.Hourly)
- Levels: Debug, Info, Warn, Error.
ionlog.Infof("User %s logged in", "Alice")
ionlog.Error("Connection failed")
{
"time":"2024-12-06T20:59:47.252944832-03:00",
"level":"INFO",
"msg": "User Alice logged in",
"service-id":"0xcafe",
"package":"main",
"function":"main",
"file":"main.go",
"line":42
}
ionlog.LogOnceInfo("Initialization complete")
status := "STARTING"
ionlog.LogOnChangeInfof("status: %s", status) // Logs once
ionlog.LogOnChangeInfof("status: %s", status) // Will not log
status = "RUNNING"
ionlog.LogOnChangeInfof("status: %s", status) // Logs again
ionlog.LogOnChangeInfof("status: %s", status) // Will not log
- Start() initializes the logger
- Stop() closes the logger when the program ends
- Internal logs are handled by the slog package, and outputed to the os.Stdout by default.